OSDN Git Service

* alias.c: Include toplev.h
[pf3gnuchains/gcc-fork.git] / gcc / caller-save.c
1 /* Save and restore call-clobbered registers which are live across a call.
2    Copyright (C) 1989, 1992, 94-95, 1997, 1998 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, 59 Temple Place - Suite 330,
19 Boston, MA 02111-1307, USA.  */
20
21 #include "config.h"
22 #include "system.h"
23 #include "rtl.h"
24 #include "insn-config.h"
25 #include "flags.h"
26 #include "regs.h"
27 #include "hard-reg-set.h"
28 #include "recog.h"
29 #include "basic-block.h"
30 #include "reload.h"
31 #include "expr.h"
32 #include "toplev.h"
33
34 #ifndef MAX_MOVE_MAX
35 #define MAX_MOVE_MAX MOVE_MAX
36 #endif
37
38 #ifndef MIN_UNITS_PER_WORD
39 #define MIN_UNITS_PER_WORD UNITS_PER_WORD
40 #endif
41
42 /* Modes for each hard register that we can save.  The smallest mode is wide
43    enough to save the entire contents of the register.  When saving the
44    register because it is live we first try to save in multi-register modes.
45    If that is not possible the save is done one register at a time.  */
46
47 static enum machine_mode 
48   regno_save_mode[FIRST_PSEUDO_REGISTER][MAX_MOVE_MAX / MIN_UNITS_PER_WORD + 1];
49
50 /* For each hard register, a place on the stack where it can be saved,
51    if needed.  */
52
53 static rtx 
54   regno_save_mem[FIRST_PSEUDO_REGISTER][MAX_MOVE_MAX / MIN_UNITS_PER_WORD + 1];
55
56 /* We will only make a register eligible for caller-save if it can be
57    saved in its widest mode with a simple SET insn as long as the memory
58    address is valid.  We record the INSN_CODE is those insns here since
59    when we emit them, the addresses might not be valid, so they might not
60    be recognized.  */
61
62 static enum insn_code 
63   reg_save_code[FIRST_PSEUDO_REGISTER][MAX_MOVE_MAX / MIN_UNITS_PER_WORD + 1];
64 static enum insn_code 
65   reg_restore_code[FIRST_PSEUDO_REGISTER][MAX_MOVE_MAX / MIN_UNITS_PER_WORD + 1];
66
67 /* Set of hard regs currently live (during scan of all insns).  */
68
69 static HARD_REG_SET hard_regs_live;
70
71 /* Set of hard regs currently residing in save area (during insn scan).  */
72
73 static HARD_REG_SET hard_regs_saved;
74
75 /* Set of hard regs which need to be restored before referenced.  */
76
77 static HARD_REG_SET hard_regs_need_restore;
78
79 /* Number of registers currently in hard_regs_saved.  */
80
81 int n_regs_saved;
82
83 static void set_reg_live                PROTO((rtx, rtx));
84 static void clear_reg_live              PROTO((rtx));
85 static void restore_referenced_regs     PROTO((rtx, rtx, enum machine_mode));
86 static int insert_save_restore          PROTO((rtx, int, int,
87                                                enum machine_mode, int));
88 \f
89 /* Initialize for caller-save.
90
91    Look at all the hard registers that are used by a call and for which
92    regclass.c has not already excluded from being used across a call.
93
94    Ensure that we can find a mode to save the register and that there is a 
95    simple insn to save and restore the register.  This latter check avoids
96    problems that would occur if we tried to save the MQ register of some
97    machines directly into memory.  */
98
99 void
100 init_caller_save ()
101 {
102   char *first_obj = (char *) oballoc (0);
103   rtx addr_reg;
104   int offset;
105   rtx address;
106   int i, j;
107
108   /* First find all the registers that we need to deal with and all
109      the modes that they can have.  If we can't find a mode to use,
110      we can't have the register live over calls.  */
111
112   for (i = 0; i < FIRST_PSEUDO_REGISTER; i++)
113     {
114       if (call_used_regs[i] && ! call_fixed_regs[i])
115         {
116           for (j = 1; j <= MOVE_MAX / UNITS_PER_WORD; j++)
117             {
118               regno_save_mode[i][j] = choose_hard_reg_mode (i, j);
119               if (regno_save_mode[i][j] == VOIDmode && j == 1)
120                 {
121                   call_fixed_regs[i] = 1;
122                   SET_HARD_REG_BIT (call_fixed_reg_set, i);
123                 }
124             }
125         }
126       else
127         regno_save_mode[i][1] = VOIDmode;
128     }
129
130   /* The following code tries to approximate the conditions under which
131      we can easily save and restore a register without scratch registers or
132      other complexities.  It will usually work, except under conditions where
133      the validity of an insn operand is dependent on the address offset.
134      No such cases are currently known.
135
136      We first find a typical offset from some BASE_REG_CLASS register.
137      This address is chosen by finding the first register in the class
138      and by finding the smallest power of two that is a valid offset from
139      that register in every mode we will use to save registers.  */
140
141   for (i = 0; i < FIRST_PSEUDO_REGISTER; i++)
142     if (TEST_HARD_REG_BIT (reg_class_contents[(int) BASE_REG_CLASS], i))
143       break;
144
145   if (i == FIRST_PSEUDO_REGISTER)
146     abort ();
147
148   addr_reg = gen_rtx_REG (Pmode, i);
149
150   for (offset = 1 << (HOST_BITS_PER_INT / 2); offset; offset >>= 1)
151     {
152       address = gen_rtx_PLUS (Pmode, addr_reg, GEN_INT (offset));
153
154       for (i = 0; i < FIRST_PSEUDO_REGISTER; i++)
155         if (regno_save_mode[i][1] != VOIDmode
156           && ! strict_memory_address_p (regno_save_mode[i][1], address))
157           break;
158
159       if (i == FIRST_PSEUDO_REGISTER)
160         break;
161     }
162
163   /* If we didn't find a valid address, we must use register indirect.  */
164   if (offset == 0)
165     address = addr_reg;
166
167   /* Next we try to form an insn to save and restore the register.  We
168      see if such an insn is recognized and meets its constraints.  */
169
170   start_sequence ();
171
172   for (i = 0; i < FIRST_PSEUDO_REGISTER; i++)
173     for (j = 1; j <= MOVE_MAX / UNITS_PER_WORD; j++)
174       if (regno_save_mode[i][j] != VOIDmode)
175         {
176           rtx mem = gen_rtx_MEM (regno_save_mode[i][j], address);
177           rtx reg = gen_rtx_REG (regno_save_mode[i][j], i);
178           rtx savepat = gen_rtx_SET (VOIDmode, mem, reg);
179           rtx restpat = gen_rtx_SET (VOIDmode, reg, mem);
180           rtx saveinsn = emit_insn (savepat);
181           rtx restinsn = emit_insn (restpat);
182           int ok;
183
184           reg_save_code[i][j] = recog_memoized (saveinsn);
185           reg_restore_code[i][j] = recog_memoized (restinsn);
186
187           /* Now extract both insns and see if we can meet their
188              constraints.  */
189           ok = (reg_save_code[i][j] != -1 && reg_restore_code[i][j] != -1);
190           if (ok)
191             {
192               insn_extract (saveinsn);
193               ok = constrain_operands (reg_save_code[i][j], 1);
194               insn_extract (restinsn);
195               ok &= constrain_operands (reg_restore_code[i][j], 1);
196             }
197
198           if (! ok)
199             {
200               regno_save_mode[i][j] = VOIDmode;
201               if (j == 1)
202                 {
203                   call_fixed_regs[i] = 1;
204                   SET_HARD_REG_BIT (call_fixed_reg_set, i);
205                 }
206             }
207       }
208
209   end_sequence ();
210
211   obfree (first_obj);
212 }
213 \f
214 /* Initialize save areas by showing that we haven't allocated any yet.  */
215
216 void
217 init_save_areas ()
218 {
219   int i, j;
220
221   for (i = 0; i < FIRST_PSEUDO_REGISTER; i++)
222     for (j = 1; j <= MOVE_MAX / UNITS_PER_WORD; j++)
223       regno_save_mem[i][j] = 0;
224 }
225
226 /* Allocate save areas for any hard registers that might need saving.
227    We take a conservative approach here and look for call-clobbered hard
228    registers that are assigned to pseudos that cross calls.  This may
229    overestimate slightly (especially if some of these registers are later
230    used as spill registers), but it should not be significant.
231
232    Then perform register elimination in the addresses of the save area
233    locations; return 1 if all eliminated addresses are strictly valid.
234    We assume that our caller has set up the elimination table to the
235    worst (largest) possible offsets.
236
237    Set *PCHANGED to 1 if we had to allocate some memory for the save area.  
238
239    Future work:
240
241      In the fallback case we should iterate backwards across all possible
242      modes for the save, choosing the largest available one instead of 
243      falling back to the smallest mode immediately.  (eg TF -> DF -> SF).
244
245      We do not try to use "move multiple" instructions that exist
246      on some machines (such as the 68k moveml).  It could be a win to try 
247      and use them when possible.  The hard part is doing it in a way that is
248      machine independent since they might be saving non-consecutive 
249      registers. (imagine caller-saving d0,d1,a0,a1 on the 68k) */
250
251 int
252 setup_save_areas (pchanged)
253      int *pchanged;
254 {
255   int i, j, k;
256   HARD_REG_SET hard_regs_used;
257   int ok = 1;
258
259
260   /* Allocate space in the save area for the largest multi-register
261      pseudos first, then work backwards to single register
262      pseudos.  */
263
264   /* Find and record all call-used hard-registers in this function.  */
265   CLEAR_HARD_REG_SET (hard_regs_used);
266   for (i = FIRST_PSEUDO_REGISTER; i < max_regno; i++)
267     if (reg_renumber[i] >= 0 && REG_N_CALLS_CROSSED (i) > 0)
268       {
269         int regno = reg_renumber[i];
270         int endregno 
271           = regno + HARD_REGNO_NREGS (regno, GET_MODE (regno_reg_rtx[i]));
272         int nregs = endregno - regno;
273
274         for (j = 0; j < nregs; j++)
275           {
276             if (call_used_regs[regno+j]) 
277               SET_HARD_REG_BIT (hard_regs_used, regno+j);
278           }
279       }
280
281   /* Now run through all the call-used hard-registers and allocate
282      space for them in the caller-save area.  Try to allocate space
283      in a manner which allows multi-register saves/restores to be done.  */
284
285   for (i = 0; i < FIRST_PSEUDO_REGISTER; i++)
286     for (j = MOVE_MAX / UNITS_PER_WORD; j > 0; j--)
287       {
288         int ok = 1;
289         int do_save;
290
291         /* If no mode exists for this size, try another.  Also break out
292            if we have already saved this hard register.  */
293         if (regno_save_mode[i][j] == VOIDmode || regno_save_mem[i][1] != 0)
294           continue;
295
296         /* See if any register in this group has been saved.  */
297         do_save = 1;
298         for (k = 0; k < j; k++)
299           if (regno_save_mem[i + k][1])
300             {
301               do_save = 0;
302               break;
303             }
304         if (! do_save)
305           continue;
306
307         for (k = 0; k < j; k++)
308             {
309               int regno = i + k;
310               ok &= (TEST_HARD_REG_BIT (hard_regs_used, regno) != 0);
311             }
312
313         /* We have found an acceptable mode to store in.  */
314         if (ok)
315           {
316
317             regno_save_mem[i][j]
318               = assign_stack_local (regno_save_mode[i][j],
319                                     GET_MODE_SIZE (regno_save_mode[i][j]), 0);
320
321             /* Setup single word save area just in case...  */
322             for (k = 0; k < j; k++)
323               {
324                 /* This should not depend on WORDS_BIG_ENDIAN.
325                    The order of words in regs is the same as in memory.  */
326                 rtx temp = gen_rtx_MEM (regno_save_mode[i+k][1], 
327                                         XEXP (regno_save_mem[i][j], 0));
328
329                 regno_save_mem[i+k][1] 
330                   = adj_offsettable_operand (temp, k * UNITS_PER_WORD);
331               }
332             *pchanged = 1;
333           }
334       }
335
336   for (i = 0; i < FIRST_PSEUDO_REGISTER; i++)
337     for (j = 1; j <= MOVE_MAX / UNITS_PER_WORD; j++)
338       if (regno_save_mem[i][j] != 0)
339         ok &= strict_memory_address_p (GET_MODE (regno_save_mem[i][j]),
340                                        XEXP (eliminate_regs (regno_save_mem[i][j], 0, NULL_RTX), 0));
341
342   return ok;
343 }
344 \f
345 /* Find the places where hard regs are live across calls and save them.
346
347    INSN_MODE is the mode to assign to any insns that we add.  This is used
348    by reload to determine whether or not reloads or register eliminations
349    need be done on these insns.  */
350
351 void
352 save_call_clobbered_regs (insn_mode)
353      enum machine_mode insn_mode;
354 {
355   rtx insn;
356   int b;
357
358   for (b = 0; b < n_basic_blocks; b++)
359     {
360       regset regs_live = basic_block_live_at_start[b];
361       rtx prev_block_last = PREV_INSN (basic_block_head[b]);
362       int i, j;
363       int regno;
364
365       /* Compute hard regs live at start of block -- this is the
366          real hard regs marked live, plus live pseudo regs that
367          have been renumbered to hard regs.  No registers have yet been
368          saved because we restore all of them before the end of the basic
369          block.  */
370
371       REG_SET_TO_HARD_REG_SET (hard_regs_live, regs_live);
372       CLEAR_HARD_REG_SET (hard_regs_saved);
373       CLEAR_HARD_REG_SET (hard_regs_need_restore);
374       n_regs_saved = 0;
375
376       EXECUTE_IF_SET_IN_REG_SET (regs_live, 0, i,
377                                  {
378                                    if ((regno = reg_renumber[i]) >= 0)
379                                      for (j = regno;
380                                           j < regno + HARD_REGNO_NREGS (regno,
381                                                                         PSEUDO_REGNO_MODE (i));
382                                           j++)
383                                        SET_HARD_REG_BIT (hard_regs_live, j);
384                                  });
385
386       /* Now scan the insns in the block, keeping track of what hard
387          regs are live as we go.  When we see a call, save the live
388          call-clobbered hard regs.  */
389
390       for (insn = basic_block_head[b]; ; insn = NEXT_INSN (insn))
391         {
392           RTX_CODE code = GET_CODE (insn);
393
394           if (GET_RTX_CLASS (code) == 'i')
395             {
396               rtx link;
397
398               /* If some registers have been saved, see if INSN references
399                  any of them.  We must restore them before the insn if so.  */
400
401               if (n_regs_saved)
402                 restore_referenced_regs (PATTERN (insn), insn, insn_mode);
403
404               /* NB: the normal procedure is to first enliven any
405                  registers set by insn, then deaden any registers that
406                  had their last use at insn.  This is incorrect now,
407                  since multiple pseudos may have been mapped to the
408                  same hard reg, and the death notes are ambiguous.  So
409                  it must be done in the other, safe, order.  */
410
411               for (link = REG_NOTES (insn); link; link = XEXP (link, 1))
412                 if (REG_NOTE_KIND (link) == REG_DEAD)
413                   clear_reg_live (XEXP (link, 0));
414
415               /* When we reach a call, we need to save all registers that are
416                  live, call-used, not fixed, and not already saved.  We must
417                  test at this point because registers that die in a CALL_INSN
418                  are not live across the call and likewise for registers that
419                  are born in the CALL_INSN.
420                  
421                  If registers are filled with parameters for this function,
422                  and some of these are also being set by this function, then
423                  they will not appear to die (no REG_DEAD note for them),
424                  to check if in fact they do, collect the set registers in
425                  hard_regs_live first.  */
426
427               if (code == CALL_INSN)
428                 {
429                   HARD_REG_SET this_call_sets;
430                   {
431                     HARD_REG_SET old_hard_regs_live;
432
433                     /* Save the hard_regs_live information.  */
434                     COPY_HARD_REG_SET (old_hard_regs_live, hard_regs_live);
435
436                     /* Now calculate hard_regs_live for this CALL_INSN
437                        only.  */
438                     CLEAR_HARD_REG_SET (hard_regs_live);
439                     note_stores (PATTERN (insn), set_reg_live);
440                     COPY_HARD_REG_SET (this_call_sets, hard_regs_live);
441
442                     /* Restore the hard_regs_live information.  */
443                     COPY_HARD_REG_SET (hard_regs_live, old_hard_regs_live);
444                   }
445
446                   for (regno = 0; regno < FIRST_PSEUDO_REGISTER; regno++)
447                     if (call_used_regs[regno] && ! call_fixed_regs[regno]
448                         && TEST_HARD_REG_BIT (hard_regs_live, regno)
449                         /* It must not be set by this instruction.  */
450                         && ! TEST_HARD_REG_BIT (this_call_sets, regno)
451                         && ! TEST_HARD_REG_BIT (hard_regs_saved, regno))
452                       regno += insert_save_restore (insn, 1, regno, 
453                                                     insn_mode, 0);
454
455                   /* Put the information for this CALL_INSN on top of what
456                      we already had.  */
457                   IOR_HARD_REG_SET (hard_regs_live, this_call_sets);
458                   COPY_HARD_REG_SET (hard_regs_need_restore, hard_regs_saved);
459
460                   /* Must recompute n_regs_saved.  */
461                   n_regs_saved = 0;
462                   for (regno = 0; regno < FIRST_PSEUDO_REGISTER; regno++)
463                     if (TEST_HARD_REG_BIT (hard_regs_saved, regno))
464                       n_regs_saved++;
465                 }
466               else
467                 {
468                   note_stores (PATTERN (insn), set_reg_live);
469 #ifdef AUTO_INC_DEC
470                   for (link = REG_NOTES (insn); link; link = XEXP (link, 1))
471                     if (REG_NOTE_KIND (link) == REG_INC)
472                       set_reg_live (XEXP (link, 0), NULL_RTX);
473 #endif
474                 }
475
476               for (link = REG_NOTES (insn); link; link = XEXP (link, 1))
477                 if (REG_NOTE_KIND (link) == REG_UNUSED)
478                   clear_reg_live (XEXP (link, 0));
479             }
480
481           if (insn == basic_block_end[b])
482             break;
483         }
484
485       /* At the end of the basic block, we must restore any registers that
486          remain saved.  If the last insn in the block is a JUMP_INSN, put
487          the restore before the insn, otherwise, put it after the insn.  */
488
489       if (n_regs_saved)
490         for (regno = 0; regno < FIRST_PSEUDO_REGISTER; regno++)
491           if (TEST_HARD_REG_BIT (hard_regs_need_restore, regno))
492             regno += insert_save_restore ((GET_CODE (insn) == JUMP_INSN
493                                   ? insn : NEXT_INSN (insn)), 0,
494                                   regno, insn_mode, MOVE_MAX / UNITS_PER_WORD);
495
496       /* If we added any insns at the start of the block, update the start
497          of the block to point at those insns.  */
498       basic_block_head[b] = NEXT_INSN (prev_block_last);
499     }
500 }
501
502 /* Here from note_stores when an insn stores a value in a register.
503    Set the proper bit or bits in hard_regs_live.  All pseudos that have
504    been assigned hard regs have had their register number changed already,
505    so we can ignore pseudos.  */
506
507 static void
508 set_reg_live (reg, setter)
509      rtx reg;
510      rtx setter ATTRIBUTE_UNUSED;
511 {
512   register int regno, endregno, i;
513   enum machine_mode mode = GET_MODE (reg);
514   int word = 0;
515
516   if (GET_CODE (reg) == SUBREG)
517     {
518       word = SUBREG_WORD (reg);
519       reg = SUBREG_REG (reg);
520     }
521
522   if (GET_CODE (reg) != REG || REGNO (reg) >= FIRST_PSEUDO_REGISTER)
523     return;
524
525   regno = REGNO (reg) + word;
526   endregno = regno + HARD_REGNO_NREGS (regno, mode);
527
528   for (i = regno; i < endregno; i++)
529     {
530       SET_HARD_REG_BIT (hard_regs_live, i);
531       CLEAR_HARD_REG_BIT (hard_regs_saved, i);
532       CLEAR_HARD_REG_BIT (hard_regs_need_restore, i);
533     }
534 }
535
536 /* Here when a REG_DEAD note records the last use of a reg.  Clear
537    the appropriate bit or bits in hard_regs_live.  Again we can ignore
538    pseudos.  */
539
540 static void
541 clear_reg_live (reg)
542      rtx reg;
543 {
544   register int regno, endregno, i;
545
546   if (GET_CODE (reg) != REG || REGNO (reg) >= FIRST_PSEUDO_REGISTER)
547     return;
548
549   regno = REGNO (reg);
550   endregno= regno + HARD_REGNO_NREGS (regno, GET_MODE (reg));
551
552   for (i = regno; i < endregno; i++)
553     {
554       CLEAR_HARD_REG_BIT (hard_regs_live, i);
555       CLEAR_HARD_REG_BIT (hard_regs_need_restore, i);
556       CLEAR_HARD_REG_BIT (hard_regs_saved, i);
557     }
558 }      
559 \f
560 /* If any register currently residing in the save area is referenced in X,
561    which is part of INSN, emit code to restore the register in front of INSN.
562    INSN_MODE is the mode to assign to any insns that we add.  */
563
564 static void
565 restore_referenced_regs (x, insn, insn_mode)
566      rtx x;
567      rtx insn;
568      enum machine_mode insn_mode;
569 {
570   enum rtx_code code = GET_CODE (x);
571   char *fmt;
572   int i, j;
573
574   if (code == CLOBBER)
575     return;
576
577   if (code == REG)
578     {
579       int regno = REGNO (x);
580
581       /* If this is a pseudo, scan its memory location, since it might
582          involve the use of another register, which might be saved.  */
583
584       if (regno >= FIRST_PSEUDO_REGISTER
585           && reg_equiv_mem[regno] != 0)
586         restore_referenced_regs (XEXP (reg_equiv_mem[regno], 0),
587                                  insn, insn_mode);
588       else if (regno >= FIRST_PSEUDO_REGISTER
589                && reg_equiv_address[regno] != 0)
590         restore_referenced_regs (reg_equiv_address[regno],
591                                  insn, insn_mode);
592
593       /* Otherwise if this is a hard register, restore any piece of it that
594          is currently saved.  */
595
596       else if (regno < FIRST_PSEUDO_REGISTER)
597         {
598           int numregs = HARD_REGNO_NREGS (regno, GET_MODE (x));
599           /* Save at most SAVEREGS at a time.  This can not be larger than
600              MOVE_MAX, because that causes insert_save_restore to fail.  */
601           int saveregs = MIN (numregs, MOVE_MAX / UNITS_PER_WORD);
602           int endregno = regno + numregs;
603
604           for (i = regno; i < endregno; i++)
605             if (TEST_HARD_REG_BIT (hard_regs_need_restore, i))
606               i += insert_save_restore (insn, 0, i, insn_mode, saveregs);
607         }
608
609       return;
610     }
611           
612   fmt = GET_RTX_FORMAT (code);
613   for (i = GET_RTX_LENGTH (code) - 1; i >= 0; i--)
614     {
615       if (fmt[i] == 'e')
616         restore_referenced_regs (XEXP (x, i), insn, insn_mode);
617       else if (fmt[i] == 'E')
618         for (j = XVECLEN (x, i) - 1; j >= 0; j--)
619           restore_referenced_regs (XVECEXP (x, i, j), insn, insn_mode);
620     }
621 }
622 \f
623 /* Insert a sequence of insns to save or restore, SAVE_P says which,
624    REGNO.  Place these insns in front of INSN.  INSN_MODE is the mode
625    to assign to these insns.   MAXRESTORE is the maximum number of registers
626    which should be restored during this call (when SAVE_P == 0).  It should
627    never be less than 1 since we only work with entire registers.
628
629    Note that we have verified in init_caller_save that we can do this
630    with a simple SET, so use it.  Set INSN_CODE to what we save there
631    since the address might not be valid so the insn might not be recognized.
632    These insns will be reloaded and have register elimination done by
633    find_reload, so we need not worry about that here.
634
635    Return the extra number of registers saved.  */
636
637 static int
638 insert_save_restore (insn, save_p, regno, insn_mode, maxrestore)
639      rtx insn;
640      int save_p;
641      int regno;
642      enum machine_mode insn_mode;
643      int maxrestore;
644 {
645   rtx pat = NULL_RTX;
646   enum insn_code code = CODE_FOR_nothing;
647   int numregs = 0;
648
649   /* A common failure mode if register status is not correct in the RTL
650      is for this routine to be called with a REGNO we didn't expect to
651      save.  That will cause us to write an insn with a (nil) SET_DEST
652      or SET_SRC.  Instead of doing so and causing a crash later, check
653      for this common case and abort here instead.  This will remove one
654      step in debugging such problems.  */
655
656   if (regno_save_mem[regno][1] == 0)
657     abort ();
658
659 #ifdef HAVE_cc0
660   /* If INSN references CC0, put our insns in front of the insn that sets
661      CC0.  This is always safe, since the only way we could be passed an
662      insn that references CC0 is for a restore, and doing a restore earlier
663      isn't a problem.  We do, however, assume here that CALL_INSNs don't
664      reference CC0.  Guard against non-INSN's like CODE_LABEL.  */
665
666   if ((GET_CODE (insn) == INSN || GET_CODE (insn) == JUMP_INSN)
667       && reg_referenced_p (cc0_rtx, PATTERN (insn)))
668     insn = prev_nonnote_insn (insn);
669 #endif
670
671   /* Get the pattern to emit and update our status.  */
672   if (save_p)
673     {
674       int i, j, k;
675       int ok;
676
677       /* See if we can save several registers with a single instruction.  
678          Work backwards to the single register case.  */
679       for (i = MOVE_MAX / UNITS_PER_WORD; i > 0; i--)
680         {
681           ok = 1;
682           if (regno_save_mem[regno][i] != 0)
683             for (j = 0; j < i; j++)
684               {
685                 if (! call_used_regs[regno + j] || call_fixed_regs[regno + j]
686                     || ! TEST_HARD_REG_BIT (hard_regs_live, regno + j)
687                     || TEST_HARD_REG_BIT (hard_regs_saved, regno + j))
688                   ok = 0;
689               }
690           else 
691             continue;
692
693           /* Must do this one save at a time */
694           if (! ok)
695             continue;
696
697           pat = gen_rtx_SET (VOIDmode, regno_save_mem[regno][i],
698                              gen_rtx_REG (GET_MODE (regno_save_mem[regno][i]),
699                                           regno));
700           code = reg_save_code[regno][i];
701
702           /* Set hard_regs_saved for all the registers we saved.  */
703           for (k = 0; k < i; k++)
704             {
705               SET_HARD_REG_BIT (hard_regs_saved, regno + k);
706               SET_HARD_REG_BIT (hard_regs_need_restore, regno + k);
707               n_regs_saved++;
708             }
709
710           numregs = i;
711           break;
712         }
713     }
714   else
715     {
716       int i, j, k;
717       int ok;
718
719       /* See if we can restore `maxrestore' registers at once.  Work
720          backwards to the single register case.  */
721       for (i = maxrestore; i > 0; i--)
722         {
723           ok = 1;
724           if (regno_save_mem[regno][i])
725             for (j = 0; j < i; j++)
726               {
727                 if (! TEST_HARD_REG_BIT (hard_regs_need_restore, regno + j))
728                   ok = 0;
729               }
730           else
731             continue;
732
733           /* Must do this one restore at a time */
734           if (! ok)
735             continue;
736             
737           pat = gen_rtx_SET (VOIDmode,
738                              gen_rtx_REG (GET_MODE (regno_save_mem[regno][i]), 
739                                           regno), 
740                          regno_save_mem[regno][i]);
741           code = reg_restore_code[regno][i];
742
743
744           /* Clear status for all registers we restored.  */
745           for (k = 0; k < i; k++)
746             {
747               CLEAR_HARD_REG_BIT (hard_regs_need_restore, regno + k);
748               n_regs_saved--;
749             }
750
751           numregs = i;
752           break;
753         }
754     }
755   /* Emit the insn and set the code and mode.  */
756
757   insn = emit_insn_before (pat, insn);
758   PUT_MODE (insn, insn_mode);
759   INSN_CODE (insn) = code;
760
761   /* Tell our callers how many extra registers we saved/restored */
762   return numregs - 1;
763 }