OSDN Git Service

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