OSDN Git Service

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