OSDN Git Service

e2732d9a8ef3b6944a39f3491ee13452fc6e53ba
[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, 1994, 1995, 1997, 1998, 1999, 2000,
3    2001, 2002, 2003, 2004, 2005, 2006, 2007 Free Software Foundation, Inc.
4
5 This file is part of GCC.
6
7 GCC is free software; you can redistribute it and/or modify it under
8 the terms of the GNU General Public License as published by the Free
9 Software Foundation; either version 2, or (at your option) any later
10 version.
11
12 GCC is distributed in the hope that it will be useful, but WITHOUT ANY
13 WARRANTY; without even the implied warranty of MERCHANTABILITY or
14 FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
15 for more details.
16
17 You should have received a copy of the GNU General Public License
18 along with GCC; see the file COPYING.  If not, write to the Free
19 Software Foundation, 51 Franklin Street, Fifth Floor, Boston, MA
20 02110-1301, USA.  */
21
22 #include "config.h"
23 #include "system.h"
24 #include "coretypes.h"
25 #include "tm.h"
26 #include "rtl.h"
27 #include "regs.h"
28 #include "insn-config.h"
29 #include "flags.h"
30 #include "hard-reg-set.h"
31 #include "recog.h"
32 #include "basic-block.h"
33 #include "reload.h"
34 #include "function.h"
35 #include "expr.h"
36 #include "toplev.h"
37 #include "tm_p.h"
38 #include "addresses.h"
39 #include "df.h"
40 #include "ggc.h"
41
42 #ifndef MAX_MOVE_MAX
43 #define MAX_MOVE_MAX MOVE_MAX
44 #endif
45
46 #ifndef MIN_UNITS_PER_WORD
47 #define MIN_UNITS_PER_WORD UNITS_PER_WORD
48 #endif
49
50 #define MOVE_MAX_WORDS (MOVE_MAX / UNITS_PER_WORD)
51
52 /* Modes for each hard register that we can save.  The smallest mode is wide
53    enough to save the entire contents of the register.  When saving the
54    register because it is live we first try to save in multi-register modes.
55    If that is not possible the save is done one register at a time.  */
56
57 static enum machine_mode
58   regno_save_mode[FIRST_PSEUDO_REGISTER][MAX_MOVE_MAX / MIN_UNITS_PER_WORD + 1];
59
60 /* For each hard register, a place on the stack where it can be saved,
61    if needed.  */
62
63 static rtx
64   regno_save_mem[FIRST_PSEUDO_REGISTER][MAX_MOVE_MAX / MIN_UNITS_PER_WORD + 1];
65
66 /* We will only make a register eligible for caller-save if it can be
67    saved in its widest mode with a simple SET insn as long as the memory
68    address is valid.  We record the INSN_CODE is those insns here since
69    when we emit them, the addresses might not be valid, so they might not
70    be recognized.  */
71
72 static int
73   cached_reg_save_code[FIRST_PSEUDO_REGISTER][MAX_MACHINE_MODE];
74 static int
75   cached_reg_restore_code[FIRST_PSEUDO_REGISTER][MAX_MACHINE_MODE];
76
77 /* Set of hard regs currently residing in save area (during insn scan).  */
78
79 static HARD_REG_SET hard_regs_saved;
80
81 /* Number of registers currently in hard_regs_saved.  */
82
83 static int n_regs_saved;
84
85 /* Computed by mark_referenced_regs, all regs referenced in a given
86    insn.  */
87 static HARD_REG_SET referenced_regs;
88
89
90 static void mark_set_regs (rtx, rtx, void *);
91 static void mark_referenced_regs (rtx);
92 static int insert_save (struct insn_chain *, int, int, HARD_REG_SET *,
93                         enum machine_mode *);
94 static int insert_restore (struct insn_chain *, int, int, int,
95                            enum machine_mode *);
96 static struct insn_chain *insert_one_insn (struct insn_chain *, int, int,
97                                            rtx);
98 static void add_stored_regs (rtx, rtx, void *);
99 \f
100 static GTY(()) rtx savepat;
101 static GTY(()) rtx restpat;
102 static GTY(()) rtx test_reg;
103 static GTY(()) rtx test_mem;
104 static GTY(()) rtx saveinsn;
105 static GTY(()) rtx restinsn;
106
107 /* Return the INSN_CODE used to save register REG in mode MODE.  */
108 static int
109 reg_save_code (int reg, enum machine_mode mode)
110 {
111   bool ok;
112   if (cached_reg_save_code[reg][mode])
113      return cached_reg_save_code[reg][mode];
114   if (!HARD_REGNO_MODE_OK (reg, mode))
115      {
116        cached_reg_save_code[reg][mode] = -1;
117        return -1;
118      }
119
120   /* Update the register number and modes of the register
121      and memory operand.  */
122   SET_REGNO (test_reg, reg);
123   PUT_MODE (test_reg, mode);
124   PUT_MODE (test_mem, mode);
125
126   /* Force re-recognition of the modified insns.  */
127   INSN_CODE (saveinsn) = -1;
128
129   cached_reg_save_code[reg][mode] = recog_memoized (saveinsn);
130   cached_reg_restore_code[reg][mode] = recog_memoized (restinsn);
131
132   /* Now extract both insns and see if we can meet their
133      constraints.  */
134   ok = (cached_reg_save_code[reg][mode] != -1
135         && cached_reg_restore_code[reg][mode] != -1);
136   if (ok)
137     {
138       extract_insn (saveinsn);
139       ok = constrain_operands (1);
140       extract_insn (restinsn);
141       ok &= constrain_operands (1);
142     }
143
144   if (! ok)
145     {
146       cached_reg_save_code[reg][mode] = -1;
147       cached_reg_restore_code[reg][mode] = -1;
148     }
149   gcc_assert (cached_reg_save_code[reg][mode]);
150   return cached_reg_save_code[reg][mode];
151 }
152
153 /* Return the INSN_CODE used to restore register REG in mode MODE.  */
154 static int
155 reg_restore_code (int reg, enum machine_mode mode)
156 {
157   if (cached_reg_restore_code[reg][mode])
158      return cached_reg_restore_code[reg][mode];
159   /* Populate our cache.  */
160   reg_save_code (reg, mode);
161   return cached_reg_restore_code[reg][mode];
162 }
163 \f
164 /* Initialize for caller-save.
165
166    Look at all the hard registers that are used by a call and for which
167    regclass.c has not already excluded from being used across a call.
168
169    Ensure that we can find a mode to save the register and that there is a
170    simple insn to save and restore the register.  This latter check avoids
171    problems that would occur if we tried to save the MQ register of some
172    machines directly into memory.  */
173
174 void
175 init_caller_save (void)
176 {
177   rtx addr_reg;
178   int offset;
179   rtx address;
180   int i, j;
181
182   /* First find all the registers that we need to deal with and all
183      the modes that they can have.  If we can't find a mode to use,
184      we can't have the register live over calls.  */
185
186   for (i = 0; i < FIRST_PSEUDO_REGISTER; i++)
187     {
188       if (call_used_regs[i] && ! call_fixed_regs[i])
189         {
190           for (j = 1; j <= MOVE_MAX_WORDS; j++)
191             {
192               regno_save_mode[i][j] = HARD_REGNO_CALLER_SAVE_MODE (i, j,
193                                                                    VOIDmode);
194               if (regno_save_mode[i][j] == VOIDmode && j == 1)
195                 {
196                   call_fixed_regs[i] = 1;
197                   SET_HARD_REG_BIT (call_fixed_reg_set, i);
198                 }
199             }
200         }
201       else
202         regno_save_mode[i][1] = VOIDmode;
203     }
204
205   /* The following code tries to approximate the conditions under which
206      we can easily save and restore a register without scratch registers or
207      other complexities.  It will usually work, except under conditions where
208      the validity of an insn operand is dependent on the address offset.
209      No such cases are currently known.
210
211      We first find a typical offset from some BASE_REG_CLASS register.
212      This address is chosen by finding the first register in the class
213      and by finding the smallest power of two that is a valid offset from
214      that register in every mode we will use to save registers.  */
215
216   for (i = 0; i < FIRST_PSEUDO_REGISTER; i++)
217     if (TEST_HARD_REG_BIT
218         (reg_class_contents
219          [(int) base_reg_class (regno_save_mode [i][1], PLUS, CONST_INT)], i))
220       break;
221
222   gcc_assert (i < FIRST_PSEUDO_REGISTER);
223
224   addr_reg = gen_rtx_REG (Pmode, i);
225
226   for (offset = 1 << (HOST_BITS_PER_INT / 2); offset; offset >>= 1)
227     {
228       address = gen_rtx_PLUS (Pmode, addr_reg, GEN_INT (offset));
229
230       for (i = 0; i < FIRST_PSEUDO_REGISTER; i++)
231         if (regno_save_mode[i][1] != VOIDmode
232           && ! strict_memory_address_p (regno_save_mode[i][1], address))
233           break;
234
235       if (i == FIRST_PSEUDO_REGISTER)
236         break;
237     }
238
239   /* If we didn't find a valid address, we must use register indirect.  */
240   if (offset == 0)
241     address = addr_reg;
242
243   /* Next we try to form an insn to save and restore the register.  We
244      see if such an insn is recognized and meets its constraints.
245
246      To avoid lots of unnecessary RTL allocation, we construct all the RTL
247      once, then modify the memory and register operands in-place.  */
248
249   test_reg = gen_rtx_REG (VOIDmode, 0);
250   test_mem = gen_rtx_MEM (VOIDmode, address);
251   savepat = gen_rtx_SET (VOIDmode, test_mem, test_reg);
252   restpat = gen_rtx_SET (VOIDmode, test_reg, test_mem);
253
254   saveinsn = gen_rtx_INSN (VOIDmode, 0, 0, 0, 0, 0, savepat, -1, 0);
255   restinsn = gen_rtx_INSN (VOIDmode, 0, 0, 0, 0, 0, restpat, -1, 0);
256
257   for (i = 0; i < FIRST_PSEUDO_REGISTER; i++)
258     for (j = 1; j <= MOVE_MAX_WORDS; j++)
259       if (reg_save_code (i,regno_save_mode[i][j]) == -1)
260         {
261           regno_save_mode[i][j] = VOIDmode;
262           if (j == 1)
263             {
264               call_fixed_regs[i] = 1;
265               SET_HARD_REG_BIT (call_fixed_reg_set, i);
266             }
267         }
268 }
269 \f
270 /* Initialize save areas by showing that we haven't allocated any yet.  */
271
272 void
273 init_save_areas (void)
274 {
275   int i, j;
276
277   for (i = 0; i < FIRST_PSEUDO_REGISTER; i++)
278     for (j = 1; j <= MOVE_MAX_WORDS; j++)
279       regno_save_mem[i][j] = 0;
280 }
281
282 /* Allocate save areas for any hard registers that might need saving.
283    We take a conservative approach here and look for call-clobbered hard
284    registers that are assigned to pseudos that cross calls.  This may
285    overestimate slightly (especially if some of these registers are later
286    used as spill registers), but it should not be significant.
287
288    Future work:
289
290      In the fallback case we should iterate backwards across all possible
291      modes for the save, choosing the largest available one instead of
292      falling back to the smallest mode immediately.  (eg TF -> DF -> SF).
293
294      We do not try to use "move multiple" instructions that exist
295      on some machines (such as the 68k moveml).  It could be a win to try
296      and use them when possible.  The hard part is doing it in a way that is
297      machine independent since they might be saving non-consecutive
298      registers. (imagine caller-saving d0,d1,a0,a1 on the 68k) */
299
300 void
301 setup_save_areas (void)
302 {
303   int i, j, k;
304   unsigned int r;
305   HARD_REG_SET hard_regs_used;
306
307   /* Allocate space in the save area for the largest multi-register
308      pseudos first, then work backwards to single register
309      pseudos.  */
310
311   /* Find and record all call-used hard-registers in this function.  */
312   CLEAR_HARD_REG_SET (hard_regs_used);
313   for (i = FIRST_PSEUDO_REGISTER; i < max_regno; i++)
314     if (reg_renumber[i] >= 0 && REG_N_CALLS_CROSSED (i) > 0)
315       {
316         unsigned int regno = reg_renumber[i];
317         unsigned int endregno
318           = end_hard_regno (GET_MODE (regno_reg_rtx[i]), regno);
319
320         for (r = regno; r < endregno; r++)
321           if (call_used_regs[r])
322             SET_HARD_REG_BIT (hard_regs_used, r);
323       }
324
325   /* Now run through all the call-used hard-registers and allocate
326      space for them in the caller-save area.  Try to allocate space
327      in a manner which allows multi-register saves/restores to be done.  */
328
329   for (i = 0; i < FIRST_PSEUDO_REGISTER; i++)
330     for (j = MOVE_MAX_WORDS; j > 0; j--)
331       {
332         int do_save = 1;
333
334         /* If no mode exists for this size, try another.  Also break out
335            if we have already saved this hard register.  */
336         if (regno_save_mode[i][j] == VOIDmode || regno_save_mem[i][1] != 0)
337           continue;
338
339         /* See if any register in this group has been saved.  */
340         for (k = 0; k < j; k++)
341           if (regno_save_mem[i + k][1])
342             {
343               do_save = 0;
344               break;
345             }
346         if (! do_save)
347           continue;
348
349         for (k = 0; k < j; k++)
350           if (! TEST_HARD_REG_BIT (hard_regs_used, i + k))
351             {
352               do_save = 0;
353               break;
354             }
355         if (! do_save)
356           continue;
357
358         /* We have found an acceptable mode to store in.  */
359         regno_save_mem[i][j]
360           = assign_stack_local (regno_save_mode[i][j],
361                                 GET_MODE_SIZE (regno_save_mode[i][j]), 0);
362
363         /* Setup single word save area just in case...  */
364         for (k = 0; k < j; k++)
365           /* This should not depend on WORDS_BIG_ENDIAN.
366              The order of words in regs is the same as in memory.  */
367           regno_save_mem[i + k][1]
368             = adjust_address_nv (regno_save_mem[i][j],
369                                  regno_save_mode[i + k][1],
370                                  k * UNITS_PER_WORD);
371       }
372
373   /* Now loop again and set the alias set of any save areas we made to
374      the alias set used to represent frame objects.  */
375   for (i = 0; i < FIRST_PSEUDO_REGISTER; i++)
376     for (j = MOVE_MAX_WORDS; j > 0; j--)
377       if (regno_save_mem[i][j] != 0)
378         set_mem_alias_set (regno_save_mem[i][j], get_frame_alias_set ());
379 }
380 \f
381 /* Find the places where hard regs are live across calls and save them.  */
382
383 void
384 save_call_clobbered_regs (void)
385 {
386   struct insn_chain *chain, *next;
387   enum machine_mode save_mode [FIRST_PSEUDO_REGISTER];
388
389   /* Computed in mark_set_regs, holds all registers set by the current
390      instruction.  */
391   HARD_REG_SET this_insn_sets;
392
393   CLEAR_HARD_REG_SET (hard_regs_saved);
394   n_regs_saved = 0;
395
396   for (chain = reload_insn_chain; chain != 0; chain = next)
397     {
398       rtx insn = chain->insn;
399       enum rtx_code code = GET_CODE (insn);
400
401       next = chain->next;
402
403       gcc_assert (!chain->is_caller_save_insn);
404
405       if (INSN_P (insn))
406         {
407           /* If some registers have been saved, see if INSN references
408              any of them.  We must restore them before the insn if so.  */
409
410           if (n_regs_saved)
411             {
412               int regno;
413
414               if (code == JUMP_INSN)
415                 /* Restore all registers if this is a JUMP_INSN.  */
416                 COPY_HARD_REG_SET (referenced_regs, hard_regs_saved);
417               else
418                 {
419                   CLEAR_HARD_REG_SET (referenced_regs);
420                   mark_referenced_regs (PATTERN (insn));
421                   AND_HARD_REG_SET (referenced_regs, hard_regs_saved);
422                 }
423
424               for (regno = 0; regno < FIRST_PSEUDO_REGISTER; regno++)
425                 if (TEST_HARD_REG_BIT (referenced_regs, regno))
426                   regno += insert_restore (chain, 1, regno, MOVE_MAX_WORDS, save_mode);
427             }
428
429           if (code == CALL_INSN
430               && ! SIBLING_CALL_P (insn)
431               && ! find_reg_note (insn, REG_NORETURN, NULL))
432             {
433               unsigned regno;
434               HARD_REG_SET hard_regs_to_save;
435               reg_set_iterator rsi;
436
437               /* Use the register life information in CHAIN to compute which
438                  regs are live during the call.  */
439               REG_SET_TO_HARD_REG_SET (hard_regs_to_save,
440                                        &chain->live_throughout);
441               /* Save hard registers always in the widest mode available.  */
442               for (regno = 0; regno < FIRST_PSEUDO_REGISTER; regno++)
443                 if (TEST_HARD_REG_BIT (hard_regs_to_save, regno))
444                   save_mode [regno] = regno_save_mode [regno][1];
445                 else
446                   save_mode [regno] = VOIDmode;
447
448               /* Look through all live pseudos, mark their hard registers
449                  and choose proper mode for saving.  */
450               EXECUTE_IF_SET_IN_REG_SET
451                 (&chain->live_throughout, FIRST_PSEUDO_REGISTER, regno, rsi)
452                 {
453                   int r = reg_renumber[regno];
454                   int nregs;
455                   enum machine_mode mode;
456
457                   gcc_assert (r >= 0);
458                   nregs = hard_regno_nregs[r][PSEUDO_REGNO_MODE (regno)];
459                   mode = HARD_REGNO_CALLER_SAVE_MODE
460                     (r, nregs, PSEUDO_REGNO_MODE (regno));
461                   if (GET_MODE_BITSIZE (mode)
462                       > GET_MODE_BITSIZE (save_mode[r]))
463                     save_mode[r] = mode;
464                   while (nregs-- > 0)
465                     SET_HARD_REG_BIT (hard_regs_to_save, r + nregs);
466                 }
467
468               /* Record all registers set in this call insn.  These don't need
469                  to be saved.  N.B. the call insn might set a subreg of a
470                  multi-hard-reg pseudo; then the pseudo is considered live
471                  during the call, but the subreg that is set isn't.  */
472               CLEAR_HARD_REG_SET (this_insn_sets);
473               note_stores (PATTERN (insn), mark_set_regs, &this_insn_sets);
474
475               /* Compute which hard regs must be saved before this call.  */
476               AND_COMPL_HARD_REG_SET (hard_regs_to_save, call_fixed_reg_set);
477               AND_COMPL_HARD_REG_SET (hard_regs_to_save, this_insn_sets);
478               AND_COMPL_HARD_REG_SET (hard_regs_to_save, hard_regs_saved);
479               AND_HARD_REG_SET (hard_regs_to_save, call_used_reg_set);
480
481               for (regno = 0; regno < FIRST_PSEUDO_REGISTER; regno++)
482                 if (TEST_HARD_REG_BIT (hard_regs_to_save, regno))
483                   regno += insert_save (chain, 1, regno, &hard_regs_to_save, save_mode);
484
485               /* Must recompute n_regs_saved.  */
486               n_regs_saved = 0;
487               for (regno = 0; regno < FIRST_PSEUDO_REGISTER; regno++)
488                 if (TEST_HARD_REG_BIT (hard_regs_saved, regno))
489                   n_regs_saved++;
490             }
491         }
492
493       if (chain->next == 0 || chain->next->block > chain->block)
494         {
495           int regno;
496           /* At the end of the basic block, we must restore any registers that
497              remain saved.  If the last insn in the block is a JUMP_INSN, put
498              the restore before the insn, otherwise, put it after the insn.  */
499
500           if (n_regs_saved)
501             for (regno = 0; regno < FIRST_PSEUDO_REGISTER; regno++)
502               if (TEST_HARD_REG_BIT (hard_regs_saved, regno))
503                 regno += insert_restore (chain, JUMP_P (insn),
504                                          regno, MOVE_MAX_WORDS, save_mode);
505         }
506     }
507 }
508
509 /* Here from note_stores, or directly from save_call_clobbered_regs, when
510    an insn stores a value in a register.
511    Set the proper bit or bits in this_insn_sets.  All pseudos that have
512    been assigned hard regs have had their register number changed already,
513    so we can ignore pseudos.  */
514 static void
515 mark_set_regs (rtx reg, rtx setter ATTRIBUTE_UNUSED, void *data)
516 {
517   int regno, endregno, i;
518   HARD_REG_SET *this_insn_sets = data;
519
520   if (GET_CODE (reg) == SUBREG)
521     {
522       rtx inner = SUBREG_REG (reg);
523       if (!REG_P (inner) || REGNO (inner) >= FIRST_PSEUDO_REGISTER)
524         return;
525       regno = subreg_regno (reg);
526       endregno = regno + subreg_nregs (reg);
527     }
528   else if (REG_P (reg)
529            && REGNO (reg) < FIRST_PSEUDO_REGISTER)
530     {
531       regno = REGNO (reg);
532       endregno = END_HARD_REGNO (reg);
533     }
534   else
535     return;
536
537   for (i = regno; i < endregno; i++)
538     SET_HARD_REG_BIT (*this_insn_sets, i);
539 }
540
541 /* Here from note_stores when an insn stores a value in a register.
542    Set the proper bit or bits in the passed regset.  All pseudos that have
543    been assigned hard regs have had their register number changed already,
544    so we can ignore pseudos.  */
545 static void
546 add_stored_regs (rtx reg, rtx setter, void *data)
547 {
548   int regno, endregno, i;
549   enum machine_mode mode = GET_MODE (reg);
550   int offset = 0;
551
552   if (GET_CODE (setter) == CLOBBER)
553     return;
554
555   if (GET_CODE (reg) == SUBREG
556       && REG_P (SUBREG_REG (reg))
557       && REGNO (SUBREG_REG (reg)) < FIRST_PSEUDO_REGISTER)
558     {
559       offset = subreg_regno_offset (REGNO (SUBREG_REG (reg)),
560                                     GET_MODE (SUBREG_REG (reg)),
561                                     SUBREG_BYTE (reg),
562                                     GET_MODE (reg));
563       regno = REGNO (SUBREG_REG (reg)) + offset;
564       endregno = regno + subreg_nregs (reg);
565     }
566   else
567     {
568       if (!REG_P (reg) || REGNO (reg) >= FIRST_PSEUDO_REGISTER)
569         return;
570
571       regno = REGNO (reg) + offset;
572       endregno = end_hard_regno (mode, regno);
573     }
574
575   for (i = regno; i < endregno; i++)
576     SET_REGNO_REG_SET ((regset) data, i);
577 }
578
579 /* Walk X and record all referenced registers in REFERENCED_REGS.  */
580 static void
581 mark_referenced_regs (rtx x)
582 {
583   enum rtx_code code = GET_CODE (x);
584   const char *fmt;
585   int i, j;
586
587   if (code == SET)
588     mark_referenced_regs (SET_SRC (x));
589   if (code == SET || code == CLOBBER)
590     {
591       x = SET_DEST (x);
592       code = GET_CODE (x);
593       if ((code == REG && REGNO (x) < FIRST_PSEUDO_REGISTER)
594           || code == PC || code == CC0
595           || (code == SUBREG && REG_P (SUBREG_REG (x))
596               && REGNO (SUBREG_REG (x)) < FIRST_PSEUDO_REGISTER
597               /* If we're setting only part of a multi-word register,
598                  we shall mark it as referenced, because the words
599                  that are not being set should be restored.  */
600               && ((GET_MODE_SIZE (GET_MODE (x))
601                    >= GET_MODE_SIZE (GET_MODE (SUBREG_REG (x))))
602                   || (GET_MODE_SIZE (GET_MODE (SUBREG_REG (x)))
603                       <= UNITS_PER_WORD))))
604         return;
605     }
606   if (code == MEM || code == SUBREG)
607     {
608       x = XEXP (x, 0);
609       code = GET_CODE (x);
610     }
611
612   if (code == REG)
613     {
614       int regno = REGNO (x);
615       int hardregno = (regno < FIRST_PSEUDO_REGISTER ? regno
616                        : reg_renumber[regno]);
617
618       if (hardregno >= 0)
619         add_to_hard_reg_set (&referenced_regs, GET_MODE (x), hardregno);
620       /* If this is a pseudo that did not get a hard register, scan its
621          memory location, since it might involve the use of another
622          register, which might be saved.  */
623       else if (reg_equiv_mem[regno] != 0)
624         mark_referenced_regs (XEXP (reg_equiv_mem[regno], 0));
625       else if (reg_equiv_address[regno] != 0)
626         mark_referenced_regs (reg_equiv_address[regno]);
627       return;
628     }
629
630   fmt = GET_RTX_FORMAT (code);
631   for (i = GET_RTX_LENGTH (code) - 1; i >= 0; i--)
632     {
633       if (fmt[i] == 'e')
634         mark_referenced_regs (XEXP (x, i));
635       else if (fmt[i] == 'E')
636         for (j = XVECLEN (x, i) - 1; j >= 0; j--)
637           mark_referenced_regs (XVECEXP (x, i, j));
638     }
639 }
640 \f
641 /* Insert a sequence of insns to restore.  Place these insns in front of
642    CHAIN if BEFORE_P is nonzero, behind the insn otherwise.  MAXRESTORE is
643    the maximum number of registers which should be restored during this call.
644    It should never be less than 1 since we only work with entire registers.
645
646    Note that we have verified in init_caller_save that we can do this
647    with a simple SET, so use it.  Set INSN_CODE to what we save there
648    since the address might not be valid so the insn might not be recognized.
649    These insns will be reloaded and have register elimination done by
650    find_reload, so we need not worry about that here.
651
652    Return the extra number of registers saved.  */
653
654 static int
655 insert_restore (struct insn_chain *chain, int before_p, int regno,
656                 int maxrestore, enum machine_mode *save_mode)
657 {
658   int i, k;
659   rtx pat = NULL_RTX;
660   int code;
661   unsigned int numregs = 0;
662   struct insn_chain *new;
663   rtx mem;
664
665   /* A common failure mode if register status is not correct in the
666      RTL is for this routine to be called with a REGNO we didn't
667      expect to save.  That will cause us to write an insn with a (nil)
668      SET_DEST or SET_SRC.  Instead of doing so and causing a crash
669      later, check for this common case here instead.  This will remove
670      one step in debugging such problems.  */
671   gcc_assert (regno_save_mem[regno][1]);
672
673   /* Get the pattern to emit and update our status.
674
675      See if we can restore `maxrestore' registers at once.  Work
676      backwards to the single register case.  */
677   for (i = maxrestore; i > 0; i--)
678     {
679       int j;
680       int ok = 1;
681
682       if (regno_save_mem[regno][i] == 0)
683         continue;
684
685       for (j = 0; j < i; j++)
686         if (! TEST_HARD_REG_BIT (hard_regs_saved, regno + j))
687           {
688             ok = 0;
689             break;
690           }
691       /* Must do this one restore at a time.  */
692       if (! ok)
693         continue;
694
695       numregs = i;
696       break;
697     }
698
699   mem = regno_save_mem [regno][numregs];
700   if (save_mode [regno] != VOIDmode
701       && save_mode [regno] != GET_MODE (mem)
702       && numregs == (unsigned int) hard_regno_nregs[regno][save_mode [regno]])
703     mem = adjust_address (mem, save_mode[regno], 0);
704   else
705     mem = copy_rtx (mem);
706   pat = gen_rtx_SET (VOIDmode,
707                      gen_rtx_REG (GET_MODE (mem),
708                                   regno), mem);
709   code = reg_restore_code (regno, GET_MODE (mem));
710   new = insert_one_insn (chain, before_p, code, pat);
711
712   /* Clear status for all registers we restored.  */
713   for (k = 0; k < i; k++)
714     {
715       CLEAR_HARD_REG_BIT (hard_regs_saved, regno + k);
716       SET_REGNO_REG_SET (&new->dead_or_set, regno + k);
717       n_regs_saved--;
718     }
719
720   /* Tell our callers how many extra registers we saved/restored.  */
721   return numregs - 1;
722 }
723
724 /* Like insert_restore above, but save registers instead.  */
725
726 static int
727 insert_save (struct insn_chain *chain, int before_p, int regno,
728              HARD_REG_SET (*to_save), enum machine_mode *save_mode)
729 {
730   int i;
731   unsigned int k;
732   rtx pat = NULL_RTX;
733   int code;
734   unsigned int numregs = 0;
735   struct insn_chain *new;
736   rtx mem;
737
738   /* A common failure mode if register status is not correct in the
739      RTL is for this routine to be called with a REGNO we didn't
740      expect to save.  That will cause us to write an insn with a (nil)
741      SET_DEST or SET_SRC.  Instead of doing so and causing a crash
742      later, check for this common case here.  This will remove one
743      step in debugging such problems.  */
744   gcc_assert (regno_save_mem[regno][1]);
745
746   /* Get the pattern to emit and update our status.
747
748      See if we can save several registers with a single instruction.
749      Work backwards to the single register case.  */
750   for (i = MOVE_MAX_WORDS; i > 0; i--)
751     {
752       int j;
753       int ok = 1;
754       if (regno_save_mem[regno][i] == 0)
755         continue;
756
757       for (j = 0; j < i; j++)
758         if (! TEST_HARD_REG_BIT (*to_save, regno + j))
759           {
760             ok = 0;
761             break;
762           }
763       /* Must do this one save at a time.  */
764       if (! ok)
765         continue;
766
767       numregs = i;
768       break;
769     }
770
771   mem = regno_save_mem [regno][numregs];
772   if (save_mode [regno] != VOIDmode
773       && save_mode [regno] != GET_MODE (mem)
774       && numregs == (unsigned int) hard_regno_nregs[regno][save_mode [regno]])
775     mem = adjust_address (mem, save_mode[regno], 0);
776   else
777     mem = copy_rtx (mem);
778   pat = gen_rtx_SET (VOIDmode, mem,
779                      gen_rtx_REG (GET_MODE (mem),
780                                   regno));
781   code = reg_save_code (regno, GET_MODE (mem));
782   new = insert_one_insn (chain, before_p, code, pat);
783
784   /* Set hard_regs_saved and dead_or_set for all the registers we saved.  */
785   for (k = 0; k < numregs; k++)
786     {
787       SET_HARD_REG_BIT (hard_regs_saved, regno + k);
788       SET_REGNO_REG_SET (&new->dead_or_set, regno + k);
789       n_regs_saved++;
790     }
791
792   /* Tell our callers how many extra registers we saved/restored.  */
793   return numregs - 1;
794 }
795
796 /* Emit a new caller-save insn and set the code.  */
797 static struct insn_chain *
798 insert_one_insn (struct insn_chain *chain, int before_p, int code, rtx pat)
799 {
800   rtx insn = chain->insn;
801   struct insn_chain *new;
802
803 #ifdef HAVE_cc0
804   /* If INSN references CC0, put our insns in front of the insn that sets
805      CC0.  This is always safe, since the only way we could be passed an
806      insn that references CC0 is for a restore, and doing a restore earlier
807      isn't a problem.  We do, however, assume here that CALL_INSNs don't
808      reference CC0.  Guard against non-INSN's like CODE_LABEL.  */
809
810   if ((NONJUMP_INSN_P (insn) || JUMP_P (insn))
811       && before_p
812       && reg_referenced_p (cc0_rtx, PATTERN (insn)))
813     chain = chain->prev, insn = chain->insn;
814 #endif
815
816   new = new_insn_chain ();
817   if (before_p)
818     {
819       rtx link;
820
821       new->prev = chain->prev;
822       if (new->prev != 0)
823         new->prev->next = new;
824       else
825         reload_insn_chain = new;
826
827       chain->prev = new;
828       new->next = chain;
829       new->insn = emit_insn_before (pat, insn);
830       /* ??? It would be nice if we could exclude the already / still saved
831          registers from the live sets.  */
832       COPY_REG_SET (&new->live_throughout, &chain->live_throughout);
833       /* Registers that die in CHAIN->INSN still live in the new insn.  */
834       for (link = REG_NOTES (chain->insn); link; link = XEXP (link, 1))
835         {
836           if (REG_NOTE_KIND (link) == REG_DEAD)
837             {
838               rtx reg = XEXP (link, 0);
839               int regno, i;
840
841               gcc_assert (REG_P (reg));
842               regno = REGNO (reg);
843               if (regno >= FIRST_PSEUDO_REGISTER)
844                 regno = reg_renumber[regno];
845               if (regno < 0)
846                 continue;
847               for (i = hard_regno_nregs[regno][GET_MODE (reg)] - 1;
848                    i >= 0; i--)
849                 SET_REGNO_REG_SET (&new->live_throughout, regno + i);
850             }
851         }
852       CLEAR_REG_SET (&new->dead_or_set);
853       if (chain->insn == BB_HEAD (BASIC_BLOCK (chain->block)))
854         BB_HEAD (BASIC_BLOCK (chain->block)) = new->insn;
855     }
856   else
857     {
858       new->next = chain->next;
859       if (new->next != 0)
860         new->next->prev = new;
861       chain->next = new;
862       new->prev = chain;
863       new->insn = emit_insn_after (pat, insn);
864       /* ??? It would be nice if we could exclude the already / still saved
865          registers from the live sets, and observe REG_UNUSED notes.  */
866       COPY_REG_SET (&new->live_throughout, &chain->live_throughout);
867       /* Registers that are set in CHAIN->INSN live in the new insn.
868          (Unless there is a REG_UNUSED note for them, but we don't
869           look for them here.) */
870       note_stores (PATTERN (chain->insn), add_stored_regs,
871                    &new->live_throughout);
872       CLEAR_REG_SET (&new->dead_or_set);
873       if (chain->insn == BB_END (BASIC_BLOCK (chain->block)))
874         BB_END (BASIC_BLOCK (chain->block)) = new->insn;
875     }
876   new->block = chain->block;
877   new->is_caller_save_insn = 1;
878
879   INSN_CODE (new->insn) = code;
880   return new;
881 }
882 #include "gt-caller-save.h"