home *** CD-ROM | disk | FTP | other *** search
/ Education Sampler 1992 [NeXTSTEP] / Education_1992_Sampler.iso / NeXT / GnuSource / cc-61.0.1 / cc / caller-save.c < prev    next >
C/C++ Source or Header  |  1991-06-03  |  21KB  |  676 lines

  1. /* Save and restore call-clobbered registers which are live across a call.
  2.    Copyright (C) 1989 Free Software Foundation, Inc.
  3.  
  4. This file is part of GNU CC.
  5.  
  6. GNU CC is free software; you can redistribute it and/or modify
  7. it under the terms of the GNU General Public License as published by
  8. the Free Software Foundation; either version 2, or (at your option)
  9. any later version.
  10.  
  11. GNU CC is distributed in the hope that it will be useful,
  12. but WITHOUT ANY WARRANTY; without even the implied warranty of
  13. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  14. GNU General Public License for more details.
  15.  
  16. You should have received a copy of the GNU General Public License
  17. along with GNU CC; see the file COPYING.  If not, write to
  18. the Free Software Foundation, 675 Mass Ave, Cambridge, MA 02139, USA.  */
  19.  
  20. #include "config.h"
  21. #include "rtl.h"
  22. #include "insn-config.h"
  23. #include "flags.h"
  24. #include "regs.h"
  25. #include "hard-reg-set.h"
  26. #include "reload.h"
  27. #include "recog.h"
  28. #include "basic-block.h"
  29.  
  30. /* Set of hard regs currently live (during scan of all insns).  */
  31.  
  32. static HARD_REG_SET hard_regs_live;
  33.  
  34. /* The block of storage on the stack where regs are saved */
  35.  
  36. static rtx save_block_addr;
  37. static int save_block_size;
  38.  
  39. /* A REG rtx for each hard register that has been saved.  */
  40.  
  41. static rtx save_reg_rtx[FIRST_PSEUDO_REGISTER];
  42.  
  43. static void set_reg_live ();
  44. static void clear_reg_live ();
  45. static void insert_call_saves ();
  46. static void emit_mult_save ();
  47. static void emit_mult_restore ();
  48. static rtx grow_save_block ();
  49. static enum machine_mode choose_hard_reg_mode ();
  50.  
  51. /* Find the places where hard regs are live across calls and save them.  */
  52.  
  53. void
  54. save_call_clobbered_regs ()
  55. {
  56.   rtx insn;
  57.   int b;
  58.  
  59.   if (obey_regdecls)
  60.     return;
  61.   
  62.   save_block_size = 0;
  63.   save_block_addr = 0;
  64.   bzero (save_reg_rtx, sizeof save_reg_rtx);
  65.  
  66.   for (b = 0; b < n_basic_blocks; b++)
  67.     {
  68.       regset regs_live = basic_block_live_at_start[b];
  69.       int offset, bit, i, j;
  70.       int regno;
  71.  
  72.       /* Compute hard regs live at start of block -- this is the
  73.      real hard regs marked live, plus live pseudo regs that
  74.      have been renumbered to hard regs.  */
  75.  
  76. #ifdef HARD_REG_SET
  77.       hard_regs_live = *regs_live;
  78. #else
  79.       COPY_HARD_REG_SET (hard_regs_live, regs_live);
  80. #endif
  81.  
  82.       for (offset = 0, i = 0; offset < regset_size; offset++)
  83.     {
  84.       if (regs_live[offset] == 0)
  85.         i += HOST_BITS_PER_INT;
  86.       else
  87.         for (bit = 1; bit && i < max_regno; bit <<= 1, i++)
  88.           if ((regs_live[offset] & bit)
  89.           && (regno = reg_renumber[i]) >= 0)
  90.         for (j = regno;
  91.              j < regno + HARD_REGNO_NREGS (regno,
  92.                            PSEUDO_REGNO_MODE (i));
  93.              j++)
  94.           SET_HARD_REG_BIT (hard_regs_live, j);
  95.     }
  96.  
  97.       /* Now scan the insns in the block, keeping track of what hard
  98.      regs are live as we go.  When we see a call, save the live
  99.      call-clobbered hard regs.  */
  100.  
  101.       for (insn = basic_block_head[b]; TRUE; insn = NEXT_INSN (insn))
  102.     {
  103.       RTX_CODE code = GET_CODE (insn);
  104.  
  105.       if (code == CALL_INSN)
  106.         insert_call_saves (insn);
  107.  
  108.       if (code == INSN || code == CALL_INSN || code == JUMP_INSN)
  109.         {
  110.           rtx link;
  111.  
  112.           /* NB: the normal procedure is to first enliven any
  113.          registers set by insn, then deaden any registers that
  114.          had their last use at insn.  This is incorrect now,
  115.          since multiple pseudos may have been mapped to the
  116.          same hard reg, and the death notes are ambiguous.  So
  117.          it must be done in the other, safe, order.  */
  118.  
  119.           for (link = REG_NOTES (insn); link; link = XEXP (link, 1))
  120.         if (REG_NOTE_KIND (link) == REG_DEAD)
  121.           clear_reg_live (XEXP (link, 0));
  122.  
  123.           note_stores (PATTERN (insn), set_reg_live);
  124.         }
  125.  
  126.       if (insn == basic_block_end[b])
  127.         break;
  128.     }
  129.     }
  130. }
  131.  
  132. /* Here from note_stores when an insn stores a value in a register.
  133.    Set the proper bit or bits in hard_regs_live.  */
  134.  
  135. static void
  136. set_reg_live (reg, setter)
  137.      rtx reg, setter;
  138. {
  139.   register int regno;
  140.  
  141.   /* WORD is which word of a multi-register group is being stored.
  142.      For the case where the store is actually into a SUBREG of REG.
  143.      Except we don't use it; I believe the entire REG needs to be
  144.      live.  */
  145.   int word = 0;
  146.  
  147.   if (GET_CODE (reg) == SUBREG)
  148.     {
  149.       word = SUBREG_WORD (reg);
  150.       reg = SUBREG_REG (reg);
  151.     }
  152.  
  153.   if (GET_CODE (reg) != REG)
  154.     return;
  155.  
  156.   regno = REGNO (reg);
  157.  
  158.   /* For pseudo reg, see if it has been assigned a hardware reg.  */
  159.   if (reg_renumber[regno] >= 0)
  160.     regno = reg_renumber[regno] /* + word */;
  161.  
  162.   /* Handle hardware regs (and pseudos allocated to hard regs).  */
  163.   if (regno < FIRST_PSEUDO_REGISTER && ! call_fixed_regs[regno])
  164.     {
  165.       register int last = regno + HARD_REGNO_NREGS (regno, GET_MODE (reg));
  166.       while (regno < last)
  167.     {
  168.       SET_HARD_REG_BIT (hard_regs_live, regno);
  169.       regno++;
  170.     }
  171.     }
  172. }
  173.  
  174. /* Here when a REG_DEAD note records the last use of a reg.  Clear
  175.    the appropriate bit or bits in hard_regs_live.  */
  176.  
  177. static void
  178. clear_reg_live (reg)
  179.      rtx reg;
  180. {
  181.   register int regno = REGNO (reg);
  182.  
  183.   /* For pseudo reg, see if it has been assigned a hardware reg.  */
  184.   if (reg_renumber[regno] >= 0)
  185.     regno = reg_renumber[regno];
  186.  
  187.   /* Handle hardware regs (and pseudos allocated to hard regs).  */
  188.   if (regno < FIRST_PSEUDO_REGISTER && ! call_fixed_regs[regno])
  189.     {
  190.       /* Pseudo regs already assigned hardware regs are treated
  191.      almost the same as explicit hardware regs.  */
  192.       register int last = regno + HARD_REGNO_NREGS (regno, GET_MODE (reg));
  193.       while (regno < last)
  194.     {
  195.       CLEAR_HARD_REG_BIT (hard_regs_live, regno);
  196.       regno++;
  197.     }
  198.     }
  199. }      
  200.  
  201. /* Insert insns to save and restore live call-clobbered regs around
  202.    call insn INSN.  */
  203.  
  204. static void
  205. insert_call_saves (insn)
  206.      rtx insn;
  207. {
  208.   int regno;
  209.   int save_block_size_needed;
  210.   int save_block_offset[FIRST_PSEUDO_REGISTER];
  211.  
  212.   save_block_size_needed = 0;
  213.   
  214.   for (regno = 0; regno < FIRST_PSEUDO_REGISTER; ++regno)
  215.     {
  216.       save_block_offset[regno] = -1;
  217.       if (call_used_regs[regno] && ! call_fixed_regs[regno]
  218.       && TEST_HARD_REG_BIT (hard_regs_live, regno))
  219.     {
  220.       enum machine_mode mode = choose_hard_reg_mode (regno);
  221.       int align = GET_MODE_UNIT_SIZE (mode);
  222.       if (align > BIGGEST_ALIGNMENT / BITS_PER_UNIT)
  223.         align = BIGGEST_ALIGNMENT / BITS_PER_UNIT;
  224.       save_block_size_needed =
  225.         ((save_block_size_needed + align - 1) / align) * align;
  226.       save_block_offset[regno] = save_block_size_needed;
  227.       save_block_size_needed += GET_MODE_SIZE (mode);
  228.       if (! save_reg_rtx[regno])
  229.         save_reg_rtx[regno] = gen_rtx (REG, mode, regno);
  230.     }
  231.     }
  232.  
  233.   if (save_block_size < save_block_size_needed)
  234.     save_block_addr = grow_save_block (save_block_addr,
  235.                        save_block_size_needed);
  236.   emit_mult_save (insn, save_block_addr, save_block_offset);
  237.   emit_mult_restore (insn, save_block_addr, save_block_offset);
  238. }
  239.  
  240. /* Emit a string of stores to save the hard regs listed in
  241.    OFFSET[] at address ADDR.  Emit them before INSN.
  242.    OFFSET[reg] is -1 if reg should not be saved, or a
  243.    suitably-aligned offset from ADDR.  
  244.    The offsets actually used do not have to be those listed
  245.    in OFFSET, but should fit in a block of the same size.  */
  246.  
  247. static void
  248. emit_mult_save (insn, addr, offset)
  249.      rtx insn, addr;
  250.      int offset[];
  251. {
  252.   int regno;
  253.   /* A register to use as a temporary for address calculations.  */
  254.   rtx tempreg;
  255.   /* A register that could be used as that temp if we save and restore it.  */
  256.   rtx can_push_reg;
  257.   /* Nonzero means we need to save a register to use it as TEMPREG.  */
  258.   int needpush;
  259.   /* The amount the stack is decremented to save that register (if we do).  */
  260.   int decrement;
  261.   /* Record which regs we save, in case we branch to retry.  */
  262.   char already_saved[FIRST_PSEUDO_REGISTER];
  263.  
  264.   bzero (already_saved, sizeof already_saved);
  265.  
  266.   /* Hair is needed because sometimes the addresses to save in are
  267.      not valid (offsets too big).
  268.      So we need a reg, TEMPREG, to compute addresses in.
  269.  
  270.      We look first for an empty reg to use.
  271.      Sometimes no reg is empty.  Then we push a reg, use it, and pop it.
  272.  
  273.      Sometimes the only reg to push and pop this way is one we want to save.
  274.      We can't save it while using it as a temporary.
  275.      So we save all the other registers, pop it, and go back to `retry'.
  276.      At that point, only this reg remains to be saved;
  277.      all the others already saved are empty.
  278.      So one of them can be the temporary for this one.  */
  279.  
  280.   /* Sometimes we can't save all the regs conveniently at once, just some.
  281.      If that happens, we branch back here to save the rest.  */
  282.  retry:
  283.   needpush = 0;
  284.   tempreg = 0;
  285.   can_push_reg = 0;
  286.  
  287.   /* Set NEEDPUSH if any save-addresses are not valid memory addresses.
  288.      If any register is available, record it in TEMPREG.
  289.      If any register doesn't need saving here, record it in CAN_PUSH_REG.  */
  290.   for (regno = 0; regno < FIRST_PSEUDO_REGISTER; ++regno)
  291.     {
  292.       if (offset[regno] >= 0 && ! already_saved[regno])
  293.     {
  294.       rtx reg = save_reg_rtx[regno];
  295.       rtx addr1 = plus_constant (addr, offset[regno]);
  296.       if (memory_address_p (GET_MODE (reg), addr1))
  297.         needpush = 1;
  298.     }
  299.  
  300.       /* A call-clobbered reg that is dead, or already saved,
  301.      can be used as a temporary for sure, at no extra cost.  */
  302.       if (tempreg == 0 && call_used_regs[regno] && ! fixed_regs[regno]
  303.       && !(offset[regno] >= 0 && ! already_saved[regno])
  304.       && HARD_REGNO_MODE_OK (regno, Pmode))
  305.     {
  306.       tempreg = gen_rtx (REG, Pmode, regno);
  307.       /* Don't use it if not valid for addressing.  */
  308.       if (! strict_memory_address_p (QImode, tempreg))
  309.         tempreg = 0;
  310.     }
  311.  
  312.       /* A call-saved reg can be a temporary if we push and pop it.  */
  313.       if (can_push_reg == 0 && ! call_used_regs[regno]
  314.       && HARD_REGNO_MODE_OK (regno, Pmode))
  315.     {
  316.       can_push_reg = gen_rtx (REG, Pmode, regno);
  317.       /* Don't use it if not valid for addressing.  */
  318.       if (! strict_memory_address_p (QImode, can_push_reg))
  319.         can_push_reg = 0;
  320.     }
  321.     }
  322.  
  323.   /* Clear NEEDPUSH if we already found an empty reg.  */
  324.   if (tempreg != 0)
  325.     needpush = 0;
  326.  
  327.   /* If we need a temp reg and none is free, make one free.  */
  328.   if (needpush)
  329.     {
  330.       /* Choose a reg, preferably not among those it is our job to save.  */
  331.       if (can_push_reg != 0)
  332.     tempreg = can_push_reg;
  333.       else
  334.     {
  335.       for (regno = 0; regno < FIRST_PSEUDO_REGISTER; ++regno)
  336.         if (offset[regno] >= 0 && !already_saved[regno]
  337.         && HARD_REGNO_MODE_OK (regno, Pmode))
  338.           {
  339.         tempreg = gen_rtx (REG, Pmode, regno);
  340.         /* Don't use it if not valid for addressing.  */
  341.         if (! strict_memory_address_p (QImode, tempreg))
  342.           tempreg = 0;
  343.         else
  344.           break;
  345.           }
  346.     }
  347.  
  348.       /* Push it on the stack.  */
  349. #ifdef STACK_GROWS_DOWNWARD
  350.       decrement = UNITS_PER_WORD;
  351. #else
  352.       decrement = - UNITS_PER_WORD;
  353. #endif
  354.  
  355.       emit_insn_before (gen_add2_insn (stack_pointer_rtx,
  356.                        gen_rtx (CONST_INT, VOIDmode, -decrement)),
  357.             insn);
  358.       emit_insn_before (gen_move_insn (gen_rtx (MEM, Pmode, stack_pointer_rtx),
  359.                        tempreg),
  360.             insn);
  361.     }
  362.  
  363.   /* Save the regs we are supposed to save, aside from TEMPREG.
  364.      Use TEMPREG for address calculations when needed.  */
  365.   for (regno = 0; regno < FIRST_PSEUDO_REGISTER; ++regno)
  366.     if (offset[regno] >= 0 && ! already_saved[regno]
  367.     && tempreg != 0 && REGNO (tempreg) != regno)
  368.       {
  369.     rtx reg = save_reg_rtx[regno];
  370.     rtx addr1 = plus_constant (addr, offset[regno]);
  371.     rtx temp;
  372.     if (! memory_address_p (GET_MODE (reg), addr1))
  373.       {
  374.         if (GET_CODE (addr1) != PLUS)
  375.           abort ();
  376.         if (GET_CODE (XEXP (addr1, 1)) != CONST_INT
  377.         || GET_CODE (XEXP (addr1, 0)) != REG)
  378.           abort ();
  379.         emit_insn_before (gen_move_insn (tempreg, XEXP (addr1, 0)), insn);
  380.         emit_insn_before (gen_add2_insn (tempreg, XEXP (addr1, 1)), insn);
  381.         addr1 = tempreg;
  382.       }
  383.     temp = gen_rtx (MEM, GET_MODE (reg), addr1);
  384.     emit_insn_before (gen_move_insn (temp, reg), insn);
  385.     already_saved[regno] = 1;
  386.       }
  387.  
  388.   /* If we pushed TEMPREG to make it free, pop it.  */
  389.   if (needpush)
  390.     {
  391.       emit_insn_before (gen_move_insn (tempreg,
  392.                        gen_rtx (MEM, Pmode, stack_pointer_rtx)),
  393.             insn);
  394.       emit_insn_before (gen_add2_insn (stack_pointer_rtx,
  395.                        gen_rtx (CONST_INT, VOIDmode, decrement)),
  396.             insn);
  397.     }
  398.  
  399.   /* If TEMPREG itself needs saving, go back and save it.
  400.      There are plenty of free regs now, those already saved.  */
  401.   if (tempreg != 0
  402.       && offset[REGNO (tempreg)] >= 0 && ! already_saved[REGNO (tempreg)])
  403.     goto retry;
  404. }
  405.  
  406. /* Emit a string of loads to restore the hard regs listed in
  407.    OFFSET[] from address ADDR; insert the loads after INSN.
  408.    OFFSET[reg] is -1 if reg should not be loaded, or a
  409.    suitably-aligned offset from ADDR.  
  410.    The offsets actually used do not need to be those provided in
  411.    OFFSET, but should agree with whatever emit_mult_save does.  */
  412.  
  413. static void
  414. emit_mult_restore (insn, addr, offset)
  415.      rtx insn, addr;
  416.      int offset[];
  417. {
  418.   int regno;
  419.  
  420.   /* Number of regs now needing to be restored.  */
  421.   int restore_count;
  422.   /* A register to use as a temporary for address calculations.  */
  423.   rtx tempreg;
  424.   /* A register available for that purpose but less desirable.  */
  425.   rtx maybe_tempreg;
  426.   /* A register that could be used as that temp if we push and pop it.  */
  427.   rtx can_push_reg;
  428.   /* Nonzero means we need to push and pop a register to use it as TEMPREG.  */
  429.   int needpush;
  430.   /* The amount the stack is decremented to save that register (if we do).  */
  431.   int decrement;
  432.   /* Record which regs we restore, in case we branch to retry.  */
  433.   char already_restored[FIRST_PSEUDO_REGISTER];
  434.  
  435.   bzero (already_restored, sizeof already_restored);
  436.  
  437.   /* Note: INSN can't be the last insn, since if it were,
  438.      no regs would live across it.  */
  439.   insn = NEXT_INSN (insn);
  440.   if (insn == 0)
  441.     abort ();
  442.   /* Now we can insert before INSN.
  443.      That is convenient because we can insert them in the order
  444.      that they should ultimately appear.  */
  445.  
  446.   /* Hair is needed because sometimes the addresses to restore from are
  447.      not valid (offsets too big).
  448.      So we need a reg, TEMPREG, to compute addresses in.
  449.  
  450.      We look first for an empty reg to use.
  451.      Sometimes no reg is empty.  Then we push a reg, use it, and pop it.
  452.  
  453.      If all the suitable regs need to be restored,
  454.      that strategy won't work.  So we restore all but one, using that one
  455.      as a temporary.  Then we jump to `retry' to restore that one,
  456.      pushing and popping another (already restored) as a temporary.  */
  457.  
  458.  retry:
  459.   needpush = 0;
  460.   tempreg = 0;
  461.   can_push_reg = 0;
  462.   restore_count = 0;
  463.  
  464.   /* Set NEEDPUSH if any restore-addresses are not valid memory addresses.
  465.      If any register is available, record it in TEMPREG.
  466.      Otherwise, one register yet to be restored goes in MAYBE_TEMPREG,
  467.      and can be used as TEMPREG for any other regs to be restored.
  468.      If any register doesn't need restoring, record it in CAN_PUSH_REG.  */
  469.   for (regno = 0; regno < FIRST_PSEUDO_REGISTER; ++regno)
  470.     {
  471.       if (offset[regno] >= 0 && ! already_restored[regno])
  472.     {
  473.       rtx reg = save_reg_rtx[regno];
  474.       rtx addr1 = plus_constant (addr, offset[regno]);
  475.  
  476.       restore_count++;
  477.  
  478.       if (memory_address_p (GET_MODE (reg), addr1))
  479.         needpush = 1;
  480.  
  481.       /* Find a call-clobbered reg that needs restoring.
  482.          We can use it as a temporary if we defer restoring it.  */
  483.       if (maybe_tempreg == 0)
  484.         {
  485.           maybe_tempreg = gen_rtx (REG, Pmode, regno);
  486.           /* Don't use it if not valid for addressing.  */
  487.           if (! strict_memory_address_p (QImode, maybe_tempreg))
  488.         maybe_tempreg = 0;
  489.         }
  490.     }
  491.  
  492.       /* If any call-clobbered reg is dead, put it in TEMPREG.
  493.      It can be used as a temporary at no extra cost.  */
  494.       if (tempreg == 0 && call_used_regs[regno] && ! fixed_regs[regno]
  495.       && offset[regno] < 0
  496.       && HARD_REGNO_MODE_OK (regno, Pmode))
  497.     {
  498.       tempreg = gen_rtx (REG, Pmode, regno);
  499.       /* Don't use it if not valid for addressing.  */
  500.       if (! strict_memory_address_p (QImode, tempreg))
  501.         tempreg = 0;
  502.     }
  503.  
  504.       /* Any non-call-clobbered reg, put in CAN_PUSH_REG.
  505.      It can be used as a temporary if we push and pop it.  */
  506.       if (can_push_reg == 0 && ! call_used_regs[regno]
  507.       && HARD_REGNO_MODE_OK (regno, Pmode))
  508.     {
  509.       can_push_reg = gen_rtx (REG, Pmode, regno);
  510.       /* Don't use it if not valid for addressing.  */
  511.       if (! strict_memory_address_p (QImode, can_push_reg))
  512.         can_push_reg = 0;
  513.     }
  514.       /* Any reg we already restored can be a temporary
  515.      if we push and pop it.  */
  516.       if (can_push_reg == 0 && already_restored[regno]
  517.       && HARD_REGNO_MODE_OK (regno, Pmode))
  518.     {
  519.       can_push_reg = gen_rtx (REG, Pmode, regno);
  520.       /* Don't use it if not valid for addressing.  */
  521.       if (! strict_memory_address_p (QImode, can_push_reg))
  522.         can_push_reg = 0;
  523.     }
  524.     }
  525.  
  526.   /* If 2 or more regs need to be restored, use one as a temp reg
  527.      for the rest (if we need a tempreg).  */
  528.   if (tempreg == 0 && maybe_tempreg != 0 && restore_count > 1)
  529.     tempreg = maybe_tempreg;
  530.  
  531.   /* Clear NEEDPUSH if we already found an empty reg.  */
  532.   if (tempreg != 0)
  533.     needpush = 0;
  534.  
  535.   /* If we need a temp reg and none is free, make one free.  */
  536.   if (needpush)
  537.     {
  538.       tempreg = can_push_reg;
  539.  
  540.       /* Push it on the stack.  */
  541. #ifdef STACK_GROWS_DOWNWARD
  542.       decrement = UNITS_PER_WORD;
  543. #else
  544.       decrement = - UNITS_PER_WORD;
  545. #endif
  546.  
  547.       emit_insn_before (gen_add2_insn (stack_pointer_rtx,
  548.                        gen_rtx (CONST_INT, VOIDmode, -decrement)),
  549.             insn);
  550.       emit_insn_before (gen_move_insn (gen_rtx (MEM, Pmode, stack_pointer_rtx),
  551.                        tempreg),
  552.             insn);
  553.     }
  554.  
  555.   /* Restore the regs we are supposed to restore, aside from TEMPREG.
  556.      Use TEMPREG for address calculations when needed.  */
  557.   for (regno = 0; regno < FIRST_PSEUDO_REGISTER; ++regno)
  558.     if (offset[regno] >= 0 && ! already_restored[regno]
  559.     && tempreg != 0 && REGNO (tempreg) != regno)
  560.       {
  561.     rtx reg = save_reg_rtx[regno];
  562.     rtx addr1 = plus_constant (addr, offset[regno]);
  563.     rtx temp;
  564.     if (! memory_address_p (GET_MODE (reg), addr1))
  565.       {
  566.         if (GET_CODE (addr1) != PLUS)
  567.           abort ();
  568.         if (GET_CODE (XEXP (addr1, 1)) != CONST_INT
  569.         || GET_CODE (XEXP (addr1, 0)) != REG)
  570.           abort ();
  571.         emit_insn_before (gen_move_insn (tempreg, XEXP (addr1, 0)), insn);
  572.         emit_insn_before (gen_add2_insn (tempreg, XEXP (addr1, 1)), insn);
  573.         addr1 = tempreg;
  574.       }
  575.     temp = gen_rtx (MEM, GET_MODE (reg), addr1);
  576.     emit_insn_before (gen_move_insn (reg, temp), insn);
  577.     already_restored[regno] = 1;
  578.       }
  579.  
  580.   /* If we pushed TEMPREG to make it free, pop it.  */
  581.   if (needpush)
  582.     {
  583.       emit_insn_before (gen_move_insn (tempreg,
  584.                        gen_rtx (MEM, Pmode, stack_pointer_rtx)),
  585.             insn);
  586.       emit_insn_before (gen_add2_insn (stack_pointer_rtx,
  587.                        gen_rtx (CONST_INT, VOIDmode, decrement)),
  588.             insn);
  589.     }
  590.  
  591.   /* If TEMPREG itself needs restoring, go back and restore it.
  592.      We can find a reg already restored to push and use as a temporary.  */
  593.   if (tempreg != 0
  594.       && offset[REGNO (tempreg)] >= 0 && ! already_restored[REGNO (tempreg)])
  595.     goto retry;
  596. }
  597.  
  598. /* Return the address of a new block of size SIZE on the stack.
  599.    The old save block is at ADDR; ADDR is 0 if no block exists yet.  */
  600.  
  601. static rtx
  602. grow_save_block (addr, size)
  603.      rtx addr;
  604.      int size;
  605. {
  606.   rtx newaddr;
  607.  
  608.   /* Keep the size a multiple of the main allocation unit.
  609.      (This keeps in sync with what assign_stack_local does
  610.      given mode BLKmode.)  */
  611.   size = (((size + (BIGGEST_ALIGNMENT / BITS_PER_UNIT) - 1)
  612.        / (BIGGEST_ALIGNMENT / BITS_PER_UNIT))
  613.       * (BIGGEST_ALIGNMENT / BITS_PER_UNIT));
  614.  
  615.   /* If no save block exists yet, create one and return it.  */
  616.   if (! addr)
  617.     {
  618.       save_block_size = size;
  619.       return XEXP (assign_stack_local (BLKmode, size, 0), 0);
  620.     }
  621.  
  622.   /* Get a new block and coalesce it with the old one.  */
  623.   newaddr = XEXP (assign_stack_local (BLKmode, size - save_block_size, 0), 0);
  624.   if (GET_CODE (newaddr) == PLUS
  625.       && XEXP (newaddr, 0) == frame_pointer_rtx
  626.       && GET_CODE (XEXP (newaddr, 1)) == CONST_INT
  627.       && GET_CODE (addr) == PLUS
  628.       && XEXP (addr, 0) == frame_pointer_rtx
  629.       && GET_CODE (XEXP (addr, 1)) == CONST_INT
  630.       && ((INTVAL (XEXP (newaddr, 1)) - INTVAL (XEXP (addr, 1))
  631.        == size - save_block_size)
  632.       || (INTVAL (XEXP (addr, 1)) - INTVAL (XEXP (newaddr, 1))
  633.           == size - save_block_size)))
  634.     {
  635.       save_block_size = size;
  636.       if (INTVAL (XEXP (newaddr, 1)) < INTVAL (XEXP (addr, 1)))
  637.     return newaddr;
  638.       else
  639.     return addr;
  640.     }
  641.  
  642.   /* They didn't coalesce, find out why */
  643.   abort ();            
  644.  
  645.   save_block_size = size;
  646.   return XEXP (assign_stack_local (BLKmode, size, 0), 0);
  647. }
  648.  
  649. /* Return a machine mode that is legitimate for hard reg REGNO
  650.    and large enough to save the whole register.  */
  651.  
  652. static enum machine_mode
  653. choose_hard_reg_mode (regno)
  654.      int regno;
  655. {
  656.   enum reg_class class = REGNO_REG_CLASS (regno);
  657.  
  658.   if (CLASS_MAX_NREGS (class, DImode) == 1
  659.       && HARD_REGNO_MODE_OK (regno, DImode))
  660.     return DImode;
  661.   else if (CLASS_MAX_NREGS (class, DFmode) == 1
  662.        && HARD_REGNO_MODE_OK (regno, DFmode))
  663.     return DFmode;
  664.   else if (CLASS_MAX_NREGS (class, SImode) == 1
  665.        && HARD_REGNO_MODE_OK (regno, SImode))
  666.     return SImode;
  667.   else if (CLASS_MAX_NREGS (class, SFmode) == 1
  668.        && HARD_REGNO_MODE_OK (regno, SFmode))
  669.     return SFmode;
  670.   else if (CLASS_MAX_NREGS (class, HImode) == 1
  671.        && HARD_REGNO_MODE_OK (regno, HImode))
  672.     return HImode;
  673.   else
  674.     abort ();
  675. }
  676.