home *** CD-ROM | disk | FTP | other *** search
/ SPACE 2 / SPACE - Library 2 - Volume 1.iso / program / 320 / compsrc2 / local-al.c < prev    next >
Encoding:
C/C++ Source or Header  |  1988-10-20  |  35.6 KB  |  1,108 lines

  1. /* Allocate registers within a basic block, for GNU compiler.
  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. /* Allocation of hard register numbers to pseudo registers is done in
  23.    two passes.  In this pass we consider only regs that are born and
  24.    die once within one basic block.  We do this one basic block at a
  25.    time.  Then the next pass allocates the registers that remain.
  26.    Two passes are used because this pass uses methods that work only
  27.    on linear code, but that do a better job than the general methods
  28.    used in global_alloc, and more quickly too.
  29.  
  30.    The assignments made are recorded in the vector reg_renumber
  31.    whose space is allocated here.  The rtl code itself is not altered.
  32.  
  33.    We assign each instruction in the basic block a number
  34.    which is its order from the beginning of the block.
  35.    Then we can represent the lifetime of a pseudo register with
  36.    a pair of numbers, and check for conflicts easily.
  37.    We can record the availability of hard registers with a
  38.    HARD_REG_SET for each instruction.  The HARD_REG_SET
  39.    contains 0 or 1 for each hard reg.
  40.  
  41.    To avoid register shuffling, we tie registers together when one
  42.    dies by being copied into another, or dies in an instruction that
  43.    does arithmetic to produce another.  The tied registers are
  44.    allocated as one.  Registers with different reg class preferences
  45.    can never be tied unless the class preferred by one is a subclass
  46.    of the one preferred by the other.
  47.  
  48.    Tying is represented with "quantity numbers".
  49.    A non-tied register is given a new quantity number.
  50.    Tied registers have the same quantity number.
  51.    
  52.    We have provision to exempt registers, even when they are contained
  53.    within the block, that can be tied to others that are not contained in it.
  54.    This is so that global_alloc could process them both and tie them then.
  55.    But this is currently disabled since tying in global_alloc is not
  56.    yet implemented.  */
  57.  
  58. #include <stdio.h>
  59. #include "config.h"
  60. #include "rtl.h"
  61. #include "flags.h"
  62. #include "basic-block.h"
  63. #include "regs.h"
  64. #include "hard-reg-set.h"
  65. #include "insn-config.h"
  66. #include "recog.h"
  67.  
  68. /* What about hardware registers used and set within same insn?
  69.    Will that ever happen for a non-fixed register?
  70.    Our lifetime-tracking for hardware registers would lose.
  71.    [This caution is an old comment that may be obsolete;
  72.     I think there is no longer a problem, but I'm not sure.]  */
  73.  
  74. /* Next quantity number available for allocation.  */
  75.  
  76. static int next_qty;
  77.  
  78. /* In all the following vectors indexed by quantity number,
  79.    only elements at indices >= FIRST_PSEUDO_REGISTER are actually used.  */
  80.  
  81. /* Element Q is the hard reg number chosen for quantity Q,
  82.    or -1 if none was found.  */
  83.  
  84. static short *qty_phys_reg;
  85.  
  86. /* Element Q is the hard reg number suggested for quantity Q,
  87.    or -1 if no specific suggestion.  */
  88.  
  89. static short *qty_phys_sugg;
  90.  
  91. /* Element Q is a reg class contained in (smaller than) the
  92.    preferred classes of all the pseudo regs that are tied in quantity Q.
  93.    This is the preferred class for allocating that quantity.  */
  94.  
  95. static enum reg_class *qty_min_class;
  96.  
  97. /* Insn number (counting from head of basic block)
  98.    where quantity Q was born.  */
  99.  
  100. static int *qty_birth;
  101.  
  102. /* Insn number (counting from head of basic block)
  103.    where quantity Q died.  Due to the way tying is done,
  104.    and the fact that we consider in this pass only regs that die but once,
  105.    a quantity can die only once.  Each quantity's life span
  106.    is a set of consecutive insns.  */
  107.  
  108. static int *qty_death;
  109.  
  110. /* Number of words needed to hold the data in quantity Q.
  111.    This depends on its machine mode.  It is used for these purposes:
  112.    1. If it is 0, the qty is not really in use and is not allocated.
  113.    2. It is used in computing the relative importances of qtys,
  114.       which determines the order in which we look for regs for them.
  115.    3. It is used in rules that prevent tying several registers of
  116.       different sizes in a way that is geometrically impossible
  117.       (see combine_regs).  */
  118.  
  119. static int *qty_size;
  120.  
  121. /* This holds the mode of the registers that are tied to qty Q,
  122.    or VOIDmode if registers with differing modes are tied together.  */
  123.  
  124. static enum machine_mode *qty_mode;
  125.  
  126. /* Nonzero if any of the regs tied to qty Q lives across a CALL_INSN.  */
  127.  
  128. static char *qty_crosses_call;
  129.  
  130. /* Nonzero means don't allocate qty Q if we can't get its preferred class.  */
  131.  
  132. static char *qty_preferred_or_nothing;
  133.  
  134. /* reg_qty[N] (where N is a pseudo reg number)
  135.    is the qty number of that reg (which is >= FIRST_PSEUDO_REGISTER),
  136.    or -1 if (REG N) is not local to the current basic block,
  137.    or -2 if not known yet.
  138.  
  139.    If N is < FIRST_PSEUDO_REGISTER, reg_qty[N] is -1.  */
  140.  
  141. static int *reg_qty;
  142.  
  143. /* The offset (in words) of register N within its quantity.
  144.    This can be nonzero if register N is SImode, and has been tied
  145.    to a subreg of a DImode register.  */
  146.  
  147. static int *reg_offset;
  148.  
  149. /* Vector of substitutions of register numbers,
  150.    used to map pseudo regs into hardware regs.
  151.    This is set up as a result of register allocation.
  152.    Element N is the hard reg assigned to pseudo reg N,
  153.    or is -1 if no hard reg was assigned.
  154.    If N is a hard reg number, element N is N.  */
  155.  
  156. short *reg_renumber;
  157.  
  158. /* Set of hard registers live at the current point in the scan
  159.    of the instructions in a basic block.  */
  160.  
  161. static HARD_REG_SET regs_live;
  162.  
  163. /* Indexed by insn-number-within-basic-block,
  164.    a set or hard registers live *after* that insn.  */
  165.  
  166. static HARD_REG_SET *regs_live_at;
  167.  
  168. /* Nonzero if a CALL_INSN has been scanned
  169.    but we have not yet seen a reference to the value returned.  */
  170.  
  171. static int call_seen;
  172.  
  173. /* Communicate local vars `insn_number' and `insn'
  174.    from `block_alloc' to `reg_is_set'.  */
  175. static int this_insn_number;
  176. static rtx this_insn;
  177.  
  178. static void block_alloc ();
  179. static int combine_regs ();
  180. static void wipe_dead_reg ();
  181. static int find_free_reg ();
  182. static void reg_is_born ();
  183. static void reg_is_set ();
  184. static void mark_life ();
  185. static void post_mark_life ();
  186. static int qty_compare ();
  187. static int qty_compare_1 ();
  188. static int reg_meets_class_p ();
  189. static int reg_class_subset_p ();
  190. static void update_qty_class ();
  191.  
  192. /* Allocate a new quantity (new within current basic block)
  193.    for register number REGNO which is born in insn number INSN_NUMBER
  194.    within the block.  MODE and SIZE are info on reg REGNO.  */
  195.  
  196. static void
  197. alloc_qty (regno, mode, size, insn_number)
  198.      int regno;
  199.      enum machine_mode mode;
  200.      int size, insn_number;
  201. {
  202.   register int qty = next_qty++;
  203.   reg_qty[regno] = qty;
  204.   reg_offset[regno] = 0;
  205.   qty_size[qty] = size;
  206.   qty_mode[qty] = mode;
  207.   qty_birth[qty] = insn_number;
  208.   qty_crosses_call[qty] = reg_crosses_call[regno];
  209.   qty_min_class[qty] = reg_preferred_class (regno);
  210.   qty_preferred_or_nothing[qty] = reg_preferred_or_nothing (regno);
  211. }
  212.  
  213. /* Main entry point of this file.  */
  214.  
  215. void
  216. local_alloc ()
  217. {
  218.   register int b, i;
  219.  
  220.   /* Allocate vectors of temporary data.
  221.      See the declarations of these variables, above,
  222.      for what they mean.  */
  223.  
  224.   qty_phys_reg = (short *) alloca (max_regno * sizeof (short));
  225.   qty_phys_sugg = (short *) alloca (max_regno * sizeof (short));
  226.   qty_birth = (int *) alloca (max_regno * sizeof (int));
  227.   qty_death = (int *) alloca (max_regno * sizeof (int));
  228.   qty_size = (int *) alloca (max_regno * sizeof (int));
  229.   qty_mode = (enum machine_mode *) alloca (max_regno * sizeof (enum machine_mode));
  230.   qty_crosses_call = (char *) alloca (max_regno);
  231.   qty_min_class = (enum reg_class *) alloca (max_regno * sizeof (enum reg_class));
  232.   qty_preferred_or_nothing = (char *) alloca (max_regno);
  233.  
  234.   reg_qty = (int *) alloca (max_regno * sizeof (int));
  235.   reg_offset = (int *) alloca (max_regno * sizeof (int));
  236.  
  237.   reg_renumber = (short *) oballoc (max_regno * sizeof (short));
  238.   for (i = 0; i < max_regno; i++)
  239.     reg_renumber[i] = -1;
  240.  
  241.   /* This controls only how many elts of the `qty_...' vectors
  242.      need to be zero for the first basic block.  */
  243.   next_qty = max_regno;
  244.  
  245.   /* Allocate each block's local registers, block by block.  */
  246.  
  247.   for (b = 0; b < n_basic_blocks; b++)
  248.     {
  249.       for (i = 0; i < FIRST_PSEUDO_REGISTER; i++)
  250.     {
  251.       reg_qty[i] = -1;
  252.     }
  253.       for (i = FIRST_PSEUDO_REGISTER; i < max_regno; i++)
  254.     {
  255.       qty_phys_sugg[i] = -1;
  256.       /* Set reg_qty to -2 for pseudos in this block, -1 for others.  */
  257.       if (reg_basic_block[i] == b && reg_n_deaths[i] == 1)
  258.         reg_qty[i] = -2;
  259.       else
  260.         reg_qty[i] = -1;
  261.     }
  262.  
  263.       bzero (reg_offset, max_regno * sizeof (int));
  264.  
  265.       /* NEXT_QTY indicates which elements of the `qty_...'
  266.      vectors might need to be initialized.  Initialize those,
  267.      with explicit loop if there are few, else with bzero.  */
  268.  
  269.       if (next_qty < FIRST_PSEUDO_REGISTER + 6)
  270.     {
  271.       for (i = FIRST_PSEUDO_REGISTER; i < next_qty; i++)
  272.         {
  273.           qty_birth[i] = 0;
  274.           qty_death[i] = 0;
  275.           qty_size[i] = 0;
  276.           qty_mode[i] = VOIDmode;
  277.           qty_min_class[i] = NO_REGS;
  278.           qty_preferred_or_nothing[i] = 0;
  279.           qty_crosses_call[i] = 0;
  280.         }
  281.     }
  282.       else
  283.     {
  284.       int clear_length = next_qty - FIRST_PSEUDO_REGISTER;
  285.  
  286. #define CLEAR(vector)  \
  287.    bzero ((vector) + FIRST_PSEUDO_REGISTER,    \
  288.       (sizeof (*(vector))) * clear_length)
  289.  
  290.       CLEAR (qty_birth);
  291.       CLEAR (qty_death);
  292.       CLEAR (qty_size);
  293.       CLEAR (qty_mode);
  294.       CLEAR (qty_min_class);
  295.       CLEAR (qty_preferred_or_nothing);
  296.       CLEAR (qty_crosses_call);
  297.     }
  298.  
  299.       next_qty = FIRST_PSEUDO_REGISTER;
  300.  
  301.       block_alloc (b);
  302.     }
  303. }
  304.  
  305. /* Allocate hard regs to the pseudo regs used only within block number B.
  306.    Only the pseudos that die but once can be handled.  */
  307.  
  308. static void
  309. block_alloc (b)
  310.      int b;
  311. {
  312.   register int i, q;
  313.   register rtx insn;
  314.   int insn_number = 0;
  315.   int insn_count = 0;
  316.   short *qty_order;
  317.  
  318.   call_seen = 0;
  319.  
  320.   /* Count the instructions in the basic block.  */
  321.  
  322.   insn = basic_block_end[b];
  323.   while (1)
  324.     {
  325.       insn_count++;
  326.       if (insn == basic_block_head[b])
  327.     break;
  328.       insn = PREV_INSN (insn);
  329.     }
  330.  
  331.   /* +1 to leave room for a post_mark_life at the last insn.  */
  332.   regs_live_at = (HARD_REG_SET *) alloca ((insn_count + 1)
  333.                       * sizeof (HARD_REG_SET));
  334.   bzero (regs_live_at, insn_count * sizeof (HARD_REG_SET));
  335.  
  336.   /* Initialize table of hardware registers currently live.  */
  337.  
  338. #ifdef HARD_REG_SET
  339.   regs_live = *basic_block_live_at_start[b];
  340. #else
  341.   COPY_HARD_REG_SET (regs_live, basic_block_live_at_start[b]);
  342. #endif
  343.  
  344.   /* This loop scans the instructions of the basic block
  345.      and assigns quantities to registers.
  346.      It computes which registers to tie.  */
  347.  
  348.   insn = basic_block_head[b];
  349.   while (1)
  350.     {
  351.       register rtx body = PATTERN (insn);
  352.       insn_number++;
  353.  
  354.       if (GET_CODE (insn) == INSN || GET_CODE (insn) == JUMP_INSN
  355.       || GET_CODE (insn) == CALL_INSN)
  356.     {
  357.       register rtx link;
  358.       register int win = 0;
  359.       register rtx r0, r1;
  360.       int combined_regno = -1;
  361.       int insn_code_number = recog_memoized (insn);
  362.       int commutative = 0;
  363.  
  364.       /* Set COMMUTATIVE if operands 1 and 2 are commutative.  */
  365.       if (insn_code_number >= 0
  366.           && insn_n_operands[insn_code_number] > 2
  367.           && insn_operand_constraint[insn_code_number][1][0] == '%')
  368.         commutative = 1;
  369.  
  370.       /* Is this insn suitable for tying two registers?
  371.          If so, try doing that.
  372.          Suitable insns are (set reg0 reg1) and
  373.          (set reg0 (arithop reg1 ...)).
  374.          For a commutative operation, try (set reg0 (arithop ... reg1)).
  375.          Subregs in place of regs are also ok.
  376.          An insn with parallel sets is ok if the first set is suitable.
  377.  
  378.          If tying is done, WIN is set nonzero.  */
  379.  
  380.       if (GET_CODE (body) == SET
  381.           && (r0 = SET_DEST (body),
  382.           GET_CODE (r0) == REG || GET_CODE (r0) == SUBREG)
  383.           && (r1 = SET_SRC (body),
  384.           GET_CODE (r1) == REG || GET_CODE (r1) == SUBREG))
  385.         win = combine_regs (r1, r0, b, insn_number, insn);
  386.       else if (GET_CODE (body) == SET)
  387.         {
  388.           r0 = SET_DEST (body);
  389.           if (GET_CODE (r0) == REG || GET_CODE (r0) == SUBREG)
  390.         {
  391.           if (GET_RTX_FORMAT (GET_CODE (SET_SRC (body)))[0] == 'e'
  392.               && (r1 = XEXP (SET_SRC (body), 0),
  393.               GET_CODE (r1) == REG || GET_CODE (r1) == SUBREG))
  394.             win = combine_regs (r1, r0, b, insn_number, insn);
  395.           if (win == 0 && commutative
  396.               && GET_RTX_FORMAT (GET_CODE (SET_SRC (body)))[1] == 'e'
  397.               && (r1 = XEXP (SET_SRC (body), 1),
  398.               GET_CODE (r1) == REG || GET_CODE (r1) == SUBREG))
  399.             win = combine_regs (r1, r0, b, insn_number, insn);
  400.         }
  401.         }
  402.       else if (GET_CODE (body) == PARALLEL)
  403.         {
  404.           rtx set1 = XVECEXP (body, 0, 0);
  405.           if (GET_CODE (set1) == SET 
  406.           && (r0 = SET_DEST (set1),
  407.               GET_CODE (r0) == REG || GET_CODE (r0) == SUBREG)
  408.           && GET_RTX_FORMAT (GET_CODE (SET_SRC (set1)))[0] == 'e'
  409.           && (r1 = XEXP (SET_SRC (set1), 0),
  410.               GET_CODE (r1) == REG || GET_CODE (r1) == SUBREG))
  411.         win = combine_regs (r1, r0, b, insn_number, insn);
  412.           if (win == 0 && commutative && GET_CODE (set1) == SET 
  413.           && (r0 = SET_DEST (set1),
  414.               GET_CODE (r0) == REG || GET_CODE (r0) == SUBREG)
  415.           && GET_RTX_FORMAT (GET_CODE (SET_SRC (set1)))[1] == 'e'
  416.           && (r1 = XEXP (SET_SRC (set1), 1),
  417.               GET_CODE (r1) == REG || GET_CODE (r1) == SUBREG))
  418.         win = combine_regs (r1, r0, b, insn_number, insn);
  419.         }
  420.  
  421.       /* If registers were just tied, set COMBINED_REGNO
  422.          to the number of the register used in this insn
  423.          that was tied to the register set in this insn.
  424.          This register's qty should not be "killed".  */
  425.  
  426.       if (win)
  427.         {
  428.           while (GET_CODE (r1) == SUBREG)
  429.         r1 = SUBREG_REG (r1);
  430.           combined_regno = REGNO (r1);
  431.         }
  432.  
  433.       for (link = REG_NOTES (insn); link; link = XEXP (link, 1))
  434.         {
  435.           /* Mark the death of everything that dies in this instruction,
  436.          except for anything that was just combined.  */
  437.           if (XEXP (link, 0)
  438.           && REG_NOTE_KIND (link) == REG_DEAD
  439.           && combined_regno != REGNO (XEXP (link, 0)))
  440.         {
  441. #if 0  /* The mechanism in reg_is_set that checks whether the qty dies here
  442.       ought to handle this case properly.  */
  443.           if (combined_regno >= 0 &&
  444.               reg_qty[combined_regno] == reg_qty[REGNO (XEXP (link, 0))])
  445.             /* Here for the death of the quotient in a divmod insn:
  446.                something that was born and dead in this insn
  447.                but combined with something else that also dies here.
  448.                Mark the qty as dying one instruction later.  */
  449.             wipe_dead_reg (XEXP (link, 0), insn_number,
  450.                    insn_number + 1);
  451.           else
  452. #endif
  453.             wipe_dead_reg (XEXP (link, 0), insn_number, insn_number);
  454.         }
  455.           /* Also, if this insn introduces a "constant" register,
  456.          that could just be replaced by the value it is given here
  457.          (which can legitimately be an immediate operand),
  458.          tell global-alloc not to allocate it
  459.          unless it is used at least twice more.  */
  460.  
  461.           else if (REG_NOTE_KIND (link) == REG_EQUIV
  462.                && GET_CODE (SET_DEST (body)) == REG
  463.                && general_operand (XEXP (link, 0), VOIDmode)
  464.                /* Don't inhibit allocation of a "constant" register
  465.               that we have already tied to something else!  */
  466.                && combined_regno < 0)
  467.         {
  468.           i = REGNO (SET_DEST (body));
  469.           if (reg_n_sets[i] > 1)
  470.             {
  471.               /* Register is set in another place => not really constant.
  472.              cse or flow can cause this to happen.
  473.              Ok, forget we ever thought it was constant.  */
  474.               GET_MODE (link) = VOIDmode;
  475.             }
  476.           else if (reg_n_refs[i] <= 2)
  477.             {
  478.               /* For a parameter copy, do let global-alloc
  479.              allocate it; otherwise we would be forced to
  480.              have a frame pointer.  */
  481.               if (! frame_pointer_needed
  482.               && GET_CODE (SET_SRC (PATTERN (insn))) == MEM)
  483.             reg_live_length[i] = -2;
  484.               else
  485.             reg_live_length[i] = -1;
  486.  
  487.               /* If value is not constant, we have a parameter
  488.              or a static chain pointer.  Tell local-alloc
  489.              as well not to allocate it.  */
  490.               if (! CONSTANT_P (SET_SRC (PATTERN (insn))))
  491.             {
  492.               reg_basic_block[i] = -2;
  493.               reg_qty[i] = -1;
  494.             }
  495.             }
  496.           else
  497.             /* In any case, lower its priority for global-alloc.  */
  498.             reg_live_length[i] *= 2;
  499.         }
  500.         }
  501.  
  502.       /* Allocate qty numbers for all registers local to this block
  503.          that are born (set) in this instruction.
  504.          A pseudo that already has a qty is not changed.  */
  505.  
  506.       this_insn_number = insn_number;
  507.       this_insn = insn;
  508.       note_stores (PATTERN (insn), reg_is_set);
  509.     }
  510.       if (GET_CODE (insn) == CALL_INSN)
  511.     call_seen = 1;
  512.       if (insn == basic_block_end[b])
  513.     break;
  514.       /* We don't need this for the block's first instruction
  515.      since no regs we care about are live before that instruction.
  516.      Also we do not allocate space in regs_live_at for that instruction. */
  517.       IOR_HARD_REG_SET (regs_live_at[insn_number], regs_live);
  518.       insn = NEXT_INSN (insn);
  519.     }
  520.  
  521.   /* Now every register that is local to this basic block
  522.      should have been given a quantity, or else -1 meaning ignore it.
  523.      Every quantity should have a known birth (verify this now).
  524.  
  525.      If a qty's death has not been established, it indicates a dead store.
  526.      That is ok if the insn is not entirely dead.
  527.      So set the qty'd death to just after its birth.  */
  528.  
  529.   for (i = FIRST_PSEUDO_REGISTER; i < next_qty; i++)
  530.     {
  531.       if (qty_birth[i] == 0)
  532.     abort ();
  533.       if (qty_death[i] == 0)
  534.     qty_death[i] = qty_birth[i] + 1;
  535.     }
  536.  
  537.   /* Now order the qtys so we assign them registers
  538.      in order of decreasing length of life.  */
  539.   qty_order = (short *) alloca (next_qty * sizeof (short));
  540.   for (i = FIRST_PSEUDO_REGISTER; i < next_qty; i++)
  541.     qty_order[i] = i;
  542.  
  543. #define EXCHANGE(I1, I2)  \
  544.   { i = qty_order[I1]; qty_order[I1] = qty_order[I2]; qty_order[I2] = i; }
  545.  
  546.   if (next_qty == 2 + FIRST_PSEUDO_REGISTER)
  547.     {
  548.       if (qty_compare (FIRST_PSEUDO_REGISTER + 1, FIRST_PSEUDO_REGISTER) > 0)
  549.     EXCHANGE (FIRST_PSEUDO_REGISTER, FIRST_PSEUDO_REGISTER + 1);
  550.     }
  551.   else if (next_qty == 3 + FIRST_PSEUDO_REGISTER)
  552.     {
  553.       if (qty_compare (FIRST_PSEUDO_REGISTER + 1, FIRST_PSEUDO_REGISTER) > 0)
  554.     EXCHANGE (FIRST_PSEUDO_REGISTER, FIRST_PSEUDO_REGISTER + 1);
  555.       if (qty_compare (FIRST_PSEUDO_REGISTER + 2, FIRST_PSEUDO_REGISTER + 1) > 0)
  556.     EXCHANGE (FIRST_PSEUDO_REGISTER + 2, FIRST_PSEUDO_REGISTER + 1);
  557.       if (qty_compare (FIRST_PSEUDO_REGISTER + 1, FIRST_PSEUDO_REGISTER) > 0)
  558.     EXCHANGE (FIRST_PSEUDO_REGISTER, FIRST_PSEUDO_REGISTER + 1);
  559.     }
  560.   else if (next_qty > 3 + FIRST_PSEUDO_REGISTER)
  561.     qsort (qty_order + FIRST_PSEUDO_REGISTER,
  562.        next_qty - FIRST_PSEUDO_REGISTER, sizeof (short), qty_compare_1);
  563.  
  564.   /* Now for each qty that is not a hardware register,
  565.      look for a hardware register to put it in.
  566.      First try the register class that is cheapest for this qty,
  567.      if there is more than one class.  */
  568.  
  569.   for (i = FIRST_PSEUDO_REGISTER; i < next_qty; i++)
  570.     {
  571.       q = qty_order[i];
  572.       if (qty_size[q] >= 0)
  573.     {
  574.       if (N_REG_CLASSES > 1)
  575.         {
  576.           qty_phys_reg[q] = find_free_reg (qty_crosses_call[q],
  577.                            qty_min_class[q],
  578.                            qty_mode[q], q,
  579.                            qty_birth[q], qty_death[q]);
  580.           if (qty_phys_reg[q] >= 0)
  581.         continue;
  582.         }
  583.  
  584.       if (!qty_preferred_or_nothing[q])
  585.         qty_phys_reg[q] = find_free_reg (qty_crosses_call[q], GENERAL_REGS,
  586.                          qty_mode[q], q,
  587.                          qty_birth[q], qty_death[q]);
  588.     }
  589.     }
  590.  
  591.   /* Now propagate the register assignments
  592.      to the pseudo regs belonging to the qtys.  */
  593.  
  594.   for (i = FIRST_PSEUDO_REGISTER; i < max_regno; i++)
  595.     if (reg_qty[i] >= 0 && qty_phys_reg[reg_qty[i]] >= 0)
  596.       {
  597.     reg_renumber[i] = qty_phys_reg[reg_qty[i]] + reg_offset[i];
  598.       }
  599. }
  600.  
  601. /* Compare two quantities' priority for getting real registers.
  602.    We give quantities with hard-reg suggestions priority over all others.
  603.    We give longer-lived quantities higher priority
  604.    so that the shorter-lived ones will tend to be in the same places
  605.    which gives in general the maximum room for the regs to
  606.    be allocated by global-alloc.  */
  607.  
  608. static int
  609. qty_compare (q1, q2)
  610.      int q1, q2;
  611. {
  612.   register int tem = (qty_phys_sugg[q2] >= 0) - (qty_phys_sugg[q1] >= 0);
  613.   if (tem != 0) return tem;
  614.   return -((qty_death[q1] - qty_birth[q1]) * qty_size[q2]
  615.        - (qty_death[q2] - qty_birth[q2]) * qty_size[q1]);
  616. }
  617.  
  618. static int
  619. qty_compare_1 (q1, q2)
  620.      short *q1, *q2;
  621. {
  622.   register int tem = (qty_phys_sugg[*q2] >= 0) - (qty_phys_sugg[*q1] >= 0);
  623.   if (tem != 0) return tem;
  624.   return -((qty_death[*q1] - qty_birth[*q1]) * qty_size[*q2]
  625.        - (qty_death[*q2] - qty_birth[*q2]) * qty_size[*q1]);
  626. }
  627.  
  628. /* Attempt to combine the two registers (rtx's) USEDREG and SETREG.
  629.    Returns 1 if have done so, or 0 if cannot.
  630.  
  631.    Combining registers means marking them as having the same quantity
  632.    and adjusting the offsets within the quantity if either of
  633.    them is a SUBREG).
  634.  
  635.    We don't actually combine a hard reg with a pseudo; instead
  636.    we just record the hard reg as the suggestion for the pseudo's quantity.
  637.    If we really combined them, we could lose if the pseudo lives
  638.    across an insn that clobbers the hard reg (eg, movstr).
  639.  
  640.    There are elaborate checks for the validity of combining.  */
  641.  
  642.    
  643. static int
  644. combine_regs (usedreg, setreg, b, insn_number, insn)
  645.      rtx usedreg, setreg;
  646.      int b;
  647.      int insn_number;
  648.      rtx insn;
  649. {
  650.   register int ureg, sreg;
  651.   register int offset = 0;
  652.   int usize, ssize;
  653.   register int sqty;
  654.  
  655.   /* Determine the numbers and sizes of registers being used.  */
  656.  
  657.   while (GET_CODE (usedreg) == SUBREG)
  658.     {
  659.       offset += SUBREG_WORD (usedreg);
  660.       usedreg = SUBREG_REG (usedreg);
  661.     }
  662.   if (GET_CODE (usedreg) != REG)
  663.     return 0;
  664.   ureg = REGNO (usedreg);
  665.   usize = REG_SIZE (usedreg);
  666.  
  667.   while (GET_CODE (setreg) == SUBREG)
  668.     {
  669.       offset -= SUBREG_WORD (setreg);
  670.       setreg = SUBREG_REG (setreg);
  671.     }
  672.   if (GET_CODE (setreg) != REG)
  673.     return 0;
  674.   sreg = REGNO (setreg);
  675.   ssize = REG_SIZE (setreg);
  676.  
  677.   /* Do not combine registers unless one fits within the other.  */
  678.   if (offset > 0 && usize + offset > ssize)
  679.     return 0;
  680.   if (offset < 0 && usize + offset < ssize)
  681.     return 0;
  682.   /* Do not combine with a smaller already-assigned object
  683.      if that smaller object is already combined with something bigger
  684.      or if that smaller object is a hard reg.
  685.      In the latter case, we would implicitly be using consecutive
  686.      hard regs, and there is no code to keep track of that.
  687.      (This is overcautious; we could check that ssize actually
  688.      requires more hard regs at this spot.)  */
  689.   if (ssize > usize && reg_qty[ureg] >= FIRST_PSEUDO_REGISTER
  690.       && usize < qty_size[reg_qty[ureg]])
  691.     return 0;
  692.  
  693.   /* Don't do anything with the non-allocatable registers.
  694.      Also, don't suggest a call-clobberable register
  695.      for something that must live across calls.
  696.      Also, don't suggest a hardware register for anything larger than it.  */
  697.   if (ureg < FIRST_PSEUDO_REGISTER)
  698.     {
  699.       if (fixed_regs[ureg])
  700.     return 0;
  701.       if (reg_crosses_call[sreg] && call_used_regs[ureg])
  702.     return 0;
  703.       if (usize < ssize)
  704.     return 0;
  705.     }
  706.  
  707.   if (sreg < FIRST_PSEUDO_REGISTER)
  708.     {
  709.       if (fixed_regs[sreg])
  710.     return 0;
  711.       if (reg_crosses_call[ureg] && call_used_regs[sreg])
  712.     return 0;
  713.       if (ssize < usize)
  714.     return 0;
  715.     }
  716.  
  717.   /* Tying something to itself is ok iff no offset involved.  */
  718.  
  719.   if (ureg == sreg)
  720.     return offset == 0;
  721.  
  722.   /* Don't try to connect two different hardware registers.  */
  723.  
  724.   if (ureg < FIRST_PSEUDO_REGISTER && sreg < FIRST_PSEUDO_REGISTER)
  725.     return 0;
  726.  
  727.   /* Don't connect two different machine modes if they have different
  728.      implications as to which registers may be used.  */
  729.  
  730.   if (!MODES_TIEABLE_P (GET_MODE (usedreg), GET_MODE (setreg)))
  731.     return 0;
  732.  
  733.   /* Now, if one of UREG and SREG is a hard reg and the other is
  734.      a pseudo, record the hard reg as the qty_phys_sugg for the pseudo
  735.      instead of tying them.  */
  736.   /* Return "failure" so that the lifespan of UREG is terminated here;
  737.      that way the two lifespans will be disjoint and nothing will prevent
  738.      the pseudo reg from being given this hard reg.  */
  739.  
  740.   if (ureg < FIRST_PSEUDO_REGISTER)
  741.     {
  742.       if (reg_qty[sreg] == -2)
  743.     reg_is_born (setreg, insn_number);
  744.       if (reg_qty[ureg] == -2)
  745.     reg_is_born (usedreg, insn_number);
  746.       if (reg_qty[sreg] >= 0)
  747.     qty_phys_sugg[reg_qty[sreg]] = ureg;
  748.       return 0;
  749.     }
  750.   if (sreg < FIRST_PSEUDO_REGISTER)
  751.     {
  752.       if (reg_qty[sreg] == -2)
  753.     reg_is_born (setreg, insn_number);
  754.       if (reg_qty[ureg] == -2)
  755.     reg_is_born (usedreg, insn_number);
  756.       /* If UREG already has a suggested hard reg, don't override it,
  757.      since the most likely case is on a risc machine
  758.      when a pseudo gets a subroutine result and is then returned by
  759.      this function.  In this case, the outgoing register window
  760.      is probably a better place to use.  */
  761.       if (reg_qty[ureg] >= 0
  762.       && (qty_phys_sugg[reg_qty[ureg]] < 0
  763.           /* If the old suggestion is no good, override it.  */
  764.           || (qty_crosses_call[reg_qty[ureg]]
  765.           && call_used_regs[qty_phys_sugg[reg_qty[ureg]]])))
  766.     qty_phys_sugg[reg_qty[ureg]] = sreg;
  767.       return 0;
  768.     }
  769.  
  770.   /* Do nothing if SREG is a pseudo that already has a quantity
  771.      or if it isn't local to this basic block or dies more than once.  */
  772.  
  773.   if (reg_qty[sreg] != -2)
  774.     return 0;
  775.  
  776.   /* Do nothing if UREG isn't local to this block or dies more than once.
  777.      We do this because global_alloc has no idea of tying,
  778.      so there is no use noting those local pseudos that could
  779.      profitably be delayed till global_alloc and get tied to global ones.  */
  780.  
  781.   if (reg_qty[ureg] == -1)
  782.     return 0;
  783.  
  784.   /* We don't already know about SREG, so tie it to UREG
  785.      if this is the last use of UREG, provided the classes they want
  786.      are compatible.  */
  787.  
  788.   if (find_regno_note (insn, REG_DEAD, ureg)
  789.       && (reg_qty[ureg] >= FIRST_PSEUDO_REGISTER
  790.       ? reg_meets_class_p (sreg, qty_min_class[reg_qty[ureg]])
  791.       : reg_meets_class_p (sreg, reg_preferred_class (ureg))))
  792.     {
  793.       if (reg_qty[ureg] == -2)
  794.     reg_is_born (usedreg, insn_number);
  795.       sqty = reg_qty[sreg] = reg_qty[ureg];
  796.       if (sqty < FIRST_PSEUDO_REGISTER) abort ();
  797.       /* If SREG's reg class is smaller, set qty_min_class[SQTY].  */
  798.       update_qty_class (sqty, sreg);
  799.       reg_offset[sreg] = reg_offset[ureg] + offset;
  800.       if (sqty >= 0)
  801.     {
  802.       qty_crosses_call[sqty] |= reg_crosses_call[sreg];
  803.       qty_preferred_or_nothing[sqty] = 0;
  804.       if (usize < ssize)
  805.         {
  806.           register int i;
  807.           for (i = FIRST_PSEUDO_REGISTER; i < max_regno; i++)
  808.         if (reg_qty[i] == sqty)
  809.           reg_offset[i] -= offset;
  810.           qty_size[sqty] = ssize;
  811.           qty_mode[sqty] = GET_MODE (setreg);
  812.         }
  813.     }
  814.     }
  815.   else
  816.     return 0;
  817.  
  818.   return 1;
  819. }
  820.  
  821. /* Return 1 if the preferred class of REG allows it to be tied
  822.    to a quantity or register whose class is CLASS.
  823.    True if REG's reg class either contains or is contained in CLASS.  */
  824.  
  825. static int
  826. reg_meets_class_p (reg, class)
  827.      int reg;
  828.      enum reg_class class;
  829. {
  830.   register enum reg_class rclass = reg_preferred_class (reg);
  831.   return (reg_class_subset_p (rclass, class)
  832.       || reg_class_subset_p (class, rclass));
  833. }
  834.  
  835. /* Return nonzero if R2's preferred class is the same as or contains
  836.    R1's preferred class.  R1 and R2 are pseudo-register numbers.  */
  837.  
  838. static int
  839. reg_class_subset_p (c1, c2)
  840.      register enum reg_class c1;
  841.      register enum reg_class c2;
  842. {
  843.   if (c1 == c2) return 1;
  844.  
  845.   if (c2 == ALL_REGS)
  846.   win:
  847.     return 1;
  848.   GO_IF_HARD_REG_SUBSET (reg_class_contents[(int)c1],
  849.              reg_class_contents[(int)c2],
  850.              win);
  851.   return 0;
  852. }
  853.  
  854. /* Update the class of QTY assuming that REG is being tied to it.  */
  855.  
  856. static void
  857. update_qty_class (qty, reg)
  858.      int qty;
  859.      int reg;
  860. {
  861.   enum reg_class rclass = reg_preferred_class (reg);
  862.   if (reg_class_subset_p (rclass, qty_min_class[qty]))
  863.     qty_min_class[qty] = rclass;
  864. }
  865.  
  866. /* Handle something which alters the value of an rtx REG.
  867.    REG is whatever is set or clobbered.  (CLOBBER_FLAG says which.)
  868.    If it is not really a register, we do nothing.
  869.    The file-global variables `this_insn' and `this_insn_number'
  870.    carry info from `block_alloc'.  */
  871.  
  872. static void
  873. reg_is_set (reg, clobber_flag)
  874.      rtx reg;
  875.      int clobber_flag;
  876. {
  877.   register int regno;
  878.  
  879.   if (reg == 0 || GET_CODE (reg) != REG)
  880.     return;
  881.  
  882.   regno = REGNO (reg);
  883.  
  884.   if (regno < FIRST_PSEUDO_REGISTER)
  885.     {
  886.       /* A hard reg is set or clobbered.
  887.      Mark it as live at the moment immediately following this insn
  888.      so that no pseudo can live here at that time.  */
  889.  
  890.       register int lim = regno + HARD_REGNO_NREGS (regno, GET_MODE (reg));
  891.       register int i;
  892.       for (i = regno; i < lim; i++)
  893.     SET_HARD_REG_BIT (regs_live_at[this_insn_number], i);
  894.  
  895.       /* If the hard reg is given a useful value
  896.      and it does not die in this insn,
  897.      mark it as live indefinitely afterward.  */
  898.       if (! clobber_flag
  899.       && ! find_regno_note (this_insn, REG_DEAD, regno))
  900.     reg_is_born (reg, this_insn_number);
  901.     }
  902.   else if (! clobber_flag)
  903.     {
  904.       /* A pseudo-reg is set (not just clobbered).  */
  905.  
  906.       reg_is_born (reg, this_insn_number);
  907.  
  908.       /* If a pseudo register dies in the same insn that sets it,
  909.      say it dies in the following insn instead,
  910.      because it will have to be live right after this insn.  */
  911.       if (qty_death[reg_qty[regno]] == this_insn_number)
  912.     {
  913.       /* It is live right after this insn */
  914.       post_mark_life (reg_qty[regno], GET_MODE (reg), 1,
  915.               this_insn_number, this_insn_number+1);
  916.       /* But dead later.  */
  917.       mark_life (reg_qty[regno], GET_MODE (reg), 0);
  918.       qty_death[reg_qty[regno]]++;
  919.     }
  920.     }
  921. }
  922.  
  923. /* Handle beginning of the life of register REG.
  924.    INSN_NUMBER is the insn at which this is happening.  */
  925.  
  926. static void
  927. reg_is_born (reg, insn_number)
  928.      rtx reg;
  929.      int insn_number;
  930. {
  931.   register int regno = REGNO (reg);
  932.      
  933.   if (regno < FIRST_PSEUDO_REGISTER)
  934.     mark_life (regno, GET_MODE (reg), 1);
  935.   else if (reg_qty[regno] == -2)
  936.     alloc_qty (regno, GET_MODE (reg), PSEUDO_REGNO_SIZE (regno), insn_number);
  937. }
  938.  
  939. /* Record the death in insn DEATH_INSN_NUMBER for the register REG.  */
  940.  
  941. static void
  942. wipe_dead_reg (reg, this_insn_number, death_insn_number)
  943.      register rtx reg;
  944.      int this_insn_number;
  945.      int death_insn_number;
  946. {
  947.   register int regno = REGNO (reg);
  948.  
  949.   if (regno < FIRST_PSEUDO_REGISTER)
  950.     {
  951.       mark_life (regno, GET_MODE (reg), 0);
  952.       if (this_insn_number != death_insn_number)
  953.     abort ();
  954. #if 0                /* Should never get here */
  955.     post_mark_life (regno, GET_MODE (reg), 1,
  956.             this_insn_number, death_insn_number);
  957. #endif
  958.     }
  959.   else
  960.     {
  961.       /* If a pseudo reg is referred to but was never set,
  962.      we will find here that its qty is -2.
  963.      Since these regs do not conflict with anything,
  964.      mark them as born and dead in the same place.  */
  965.       if (reg_qty[regno] == -2)
  966.     alloc_qty (regno, GET_MODE (reg), REG_SIZE (reg), this_insn_number);
  967.  
  968.       if (reg_qty[regno] >= 0)
  969.     qty_death[reg_qty[regno]] = death_insn_number;
  970.     }
  971. }
  972.  
  973. /* Find a block of SIZE words of hard regs in reg_class CLASS
  974.    that can hold something of machine-mode MODE
  975.      (but actually we test only the first of the block for holding MODE)
  976.    and still free between insn BORN_INSN and insn DEAD_INSN,
  977.    and return the number of the first of them.
  978.    Return -1 if such a block cannot be found.
  979.    If CALL_PRESERVED is nonzero, insist on registers preserved
  980.    over subroutine calls, and return -1 if cannot find such.  */
  981.  
  982. static int
  983. find_free_reg (call_preserved, class, mode, qty, born_insn, dead_insn)
  984.      int call_preserved;
  985.      enum reg_class class;
  986.      enum machine_mode mode;
  987.      int qty;
  988.      int born_insn, dead_insn;
  989. {
  990.   register int i, ins;
  991. #ifdef HARD_REG_SET
  992.   register        /* Declare it register if it's a scalar.  */
  993. #endif
  994.     HARD_REG_SET used;
  995.  
  996.   COPY_HARD_REG_SET (used,
  997.              call_preserved ? call_used_reg_set : fixed_reg_set);
  998.  
  999.   for (ins = born_insn; ins < dead_insn; ins++)
  1000.     IOR_HARD_REG_SET (used, regs_live_at[ins]);
  1001.  
  1002.   IOR_COMPL_HARD_REG_SET (used, reg_class_contents[(int) class]);
  1003.   /* Don't use the frame pointer reg in local-alloc even if
  1004.      we may omit the frame pointer, because if we do that and then we
  1005.      need a frame pointer, reload won't know how to move the pseudo
  1006.      to another hard reg.  It can move only regs made by global-alloc.  */
  1007.   SET_HARD_REG_BIT (used, FRAME_POINTER_REGNUM);
  1008.  
  1009.   /* If quantity QTY has a suggested physical register,
  1010.      try that one first.  */
  1011.  
  1012.   if (qty_phys_sugg[qty] >= 0)
  1013.     {
  1014.       i = qty_phys_sugg[qty];
  1015.       if (! TEST_HARD_REG_BIT (used, i)
  1016.       && HARD_REGNO_MODE_OK (i, mode))
  1017.     {
  1018.       register int j;
  1019.       register int size1 = HARD_REGNO_NREGS (i, mode);
  1020.       for (j = 1; j < size1 && ! TEST_HARD_REG_BIT (used, i + j); j++);
  1021.       if (j == size1)
  1022.         {
  1023.           post_mark_life (i, mode, 1, born_insn, dead_insn);
  1024.           return i;
  1025.         }
  1026.     }
  1027.     }
  1028.  
  1029.   /* If that doesn't find one, test each hard reg.  */
  1030.  
  1031.   for (i = 0; i < FIRST_PSEUDO_REGISTER; i++)
  1032.     if (! TEST_HARD_REG_BIT (used, i)
  1033.     && HARD_REGNO_MODE_OK (i, mode))
  1034.       {
  1035.     register int j;
  1036.     register int size1 = HARD_REGNO_NREGS (i, mode);
  1037.     for (j = 1; j < size1 && ! TEST_HARD_REG_BIT (used, i + j); j++);
  1038.     if (j == size1)
  1039.       {
  1040.         post_mark_life (i, mode, 1, born_insn, dead_insn);
  1041.         return i;
  1042.       }
  1043.     i += j;            /* Skip starting points we know will lose */
  1044.       }
  1045.   return -1;
  1046. }
  1047.  
  1048. static void
  1049. mark_life (regno, mode, life)
  1050.      register int regno;
  1051.      enum machine_mode mode;
  1052.      int life;
  1053. {
  1054.   register int j = HARD_REGNO_NREGS (regno, mode);
  1055.   if (life)
  1056.     while (--j >= 0)
  1057.       SET_HARD_REG_BIT (regs_live, regno + j);
  1058.   else
  1059.     while (--j >= 0)
  1060.       CLEAR_HARD_REG_BIT (regs_live, regno + j);
  1061. }
  1062.  
  1063. static void
  1064. post_mark_life (regno, mode, life, birth, death)
  1065.      register int regno, life, birth;
  1066.      enum machine_mode mode;
  1067.      int death;
  1068. {
  1069.   register int j = HARD_REGNO_NREGS (regno, mode);
  1070. #ifdef HARD_REG_SET
  1071.   register        /* Declare it register if it's a scalar.  */
  1072. #endif
  1073.     HARD_REG_SET this_reg;
  1074.  
  1075.   CLEAR_HARD_REG_SET (this_reg);
  1076.   while (--j >= 0)
  1077.     SET_HARD_REG_BIT (this_reg, regno + j);
  1078.  
  1079.   /* If a reg is born and dies in one insn,
  1080.      consider it live after that insn.  */
  1081.  
  1082.   if (birth == death)
  1083.     death++;
  1084.  
  1085.   if (life)
  1086.     while (birth < death)
  1087.       {
  1088.     IOR_HARD_REG_SET (regs_live_at[birth], this_reg);
  1089.     birth++;
  1090.       }
  1091.   else
  1092.     while (birth < death)
  1093.       {
  1094.     AND_COMPL_HARD_REG_SET (regs_live_at[birth], this_reg);
  1095.     birth++;
  1096.       }
  1097. }
  1098.  
  1099. void
  1100. dump_local_alloc (file)
  1101.      FILE *file;
  1102. {
  1103.   register int i;
  1104.   for (i = FIRST_PSEUDO_REGISTER; i < max_regno; i++)
  1105.     if (reg_renumber[i] != -1)
  1106.       fprintf (file, ";; Register %d in %d.\n", i, reg_renumber[i]);
  1107. }
  1108.