OSDN Git Service

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