OSDN Git Service

PR target/45063
[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, 2008, 2009, 2010
4    Free Software Foundation, Inc.
5
6 This file is part of GCC.
7
8 GCC is free software; you can redistribute it and/or modify it under
9 the terms of the GNU General Public License as published by the Free
10 Software Foundation; either version 3, or (at your option) any later
11 version.
12
13 GCC is distributed in the hope that it will be useful, but WITHOUT ANY
14 WARRANTY; without even the implied warranty of MERCHANTABILITY or
15 FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
16 for more details.
17
18 You should have received a copy of the GNU General Public License
19 along with GCC; see the file COPYING3.  If not see
20 <http://www.gnu.org/licenses/>.  */
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 "df.h"
34 #include "reload.h"
35 #include "function.h"
36 #include "expr.h"
37 #include "diagnostic-core.h"
38 #include "toplev.h"
39 #include "tm_p.h"
40 #include "addresses.h"
41 #include "output.h"
42 #include "ggc.h"
43
44 #define MOVE_MAX_WORDS (MOVE_MAX / UNITS_PER_WORD)
45
46 #define regno_save_mode \
47   (this_target_reload->x_regno_save_mode)
48 #define cached_reg_save_code \
49   (this_target_reload->x_cached_reg_save_code)
50 #define cached_reg_restore_code \
51   (this_target_reload->x_cached_reg_restore_code)
52
53 /* For each hard register, a place on the stack where it can be saved,
54    if needed.  */
55
56 static rtx
57   regno_save_mem[FIRST_PSEUDO_REGISTER][MAX_MOVE_MAX / MIN_UNITS_PER_WORD + 1];
58
59 /* The number of elements in the subsequent array.  */
60 static int save_slots_num;
61
62 /* Allocated slots so far.  */
63 static rtx save_slots[FIRST_PSEUDO_REGISTER];
64
65 /* Set of hard regs currently residing in save area (during insn scan).  */
66
67 static HARD_REG_SET hard_regs_saved;
68
69 /* Number of registers currently in hard_regs_saved.  */
70
71 static int n_regs_saved;
72
73 /* Computed by mark_referenced_regs, all regs referenced in a given
74    insn.  */
75 static HARD_REG_SET referenced_regs;
76
77
78 typedef void refmarker_fn (rtx *loc, enum machine_mode mode, int hardregno,
79                            void *mark_arg);
80
81 static int reg_save_code (int, enum machine_mode);
82 static int reg_restore_code (int, enum machine_mode);
83
84 struct saved_hard_reg;
85 static void initiate_saved_hard_regs (void);
86 static struct saved_hard_reg *new_saved_hard_reg (int, int);
87 static void finish_saved_hard_regs (void);
88 static int saved_hard_reg_compare_func (const void *, const void *);
89
90 static void mark_set_regs (rtx, const_rtx, void *);
91 static void mark_referenced_regs (rtx *, refmarker_fn *mark, void *mark_arg);
92 static refmarker_fn mark_reg_as_referenced;
93 static refmarker_fn replace_reg_with_saved_mem;
94 static int insert_save (struct insn_chain *, int, int, HARD_REG_SET *,
95                         enum machine_mode *);
96 static int insert_restore (struct insn_chain *, int, int, int,
97                            enum machine_mode *);
98 static struct insn_chain *insert_one_insn (struct insn_chain *, int, int,
99                                            rtx);
100 static void add_stored_regs (rtx, const_rtx, void *);
101
102 \f
103
104 static GTY(()) rtx savepat;
105 static GTY(()) rtx restpat;
106 static GTY(()) rtx test_reg;
107 static GTY(()) rtx test_mem;
108 static GTY(()) rtx saveinsn;
109 static GTY(()) rtx restinsn;
110
111 /* Return the INSN_CODE used to save register REG in mode MODE.  */
112 static int
113 reg_save_code (int reg, enum machine_mode mode)
114 {
115   bool ok;
116   if (cached_reg_save_code[reg][mode])
117      return cached_reg_save_code[reg][mode];
118   if (!HARD_REGNO_MODE_OK (reg, mode))
119      {
120        cached_reg_save_code[reg][mode] = -1;
121        cached_reg_restore_code[reg][mode] = -1;
122        return -1;
123      }
124
125   /* Update the register number and modes of the register
126      and memory operand.  */
127   SET_REGNO (test_reg, reg);
128   PUT_MODE (test_reg, mode);
129   PUT_MODE (test_mem, mode);
130
131   /* Force re-recognition of the modified insns.  */
132   INSN_CODE (saveinsn) = -1;
133   INSN_CODE (restinsn) = -1;
134
135   cached_reg_save_code[reg][mode] = recog_memoized (saveinsn);
136   cached_reg_restore_code[reg][mode] = recog_memoized (restinsn);
137
138   /* Now extract both insns and see if we can meet their
139      constraints.  */
140   ok = (cached_reg_save_code[reg][mode] != -1
141         && cached_reg_restore_code[reg][mode] != -1);
142   if (ok)
143     {
144       extract_insn (saveinsn);
145       ok = constrain_operands (1);
146       extract_insn (restinsn);
147       ok &= constrain_operands (1);
148     }
149
150   if (! ok)
151     {
152       cached_reg_save_code[reg][mode] = -1;
153       cached_reg_restore_code[reg][mode] = -1;
154     }
155   gcc_assert (cached_reg_save_code[reg][mode]);
156   return cached_reg_save_code[reg][mode];
157 }
158
159 /* Return the INSN_CODE used to restore register REG in mode MODE.  */
160 static int
161 reg_restore_code (int reg, enum machine_mode mode)
162 {
163   if (cached_reg_restore_code[reg][mode])
164      return cached_reg_restore_code[reg][mode];
165   /* Populate our cache.  */
166   reg_save_code (reg, mode);
167   return cached_reg_restore_code[reg][mode];
168 }
169 \f
170 /* Initialize for caller-save.
171
172    Look at all the hard registers that are used by a call and for which
173    reginfo.c has not already excluded from being used across a call.
174
175    Ensure that we can find a mode to save the register and that there is a
176    simple insn to save and restore the register.  This latter check avoids
177    problems that would occur if we tried to save the MQ register of some
178    machines directly into memory.  */
179
180 void
181 init_caller_save (void)
182 {
183   rtx addr_reg;
184   int offset;
185   rtx address;
186   int i, j;
187
188   if (caller_save_initialized_p)
189     return;
190
191   caller_save_initialized_p = true;
192
193   CLEAR_HARD_REG_SET (no_caller_save_reg_set);
194   /* First find all the registers that we need to deal with and all
195      the modes that they can have.  If we can't find a mode to use,
196      we can't have the register live over calls.  */
197
198   for (i = 0; i < FIRST_PSEUDO_REGISTER; i++)
199     {
200       if (call_used_regs[i]
201           && !TEST_HARD_REG_BIT (call_fixed_reg_set, i))
202         {
203           for (j = 1; j <= MOVE_MAX_WORDS; j++)
204             {
205               regno_save_mode[i][j] = HARD_REGNO_CALLER_SAVE_MODE (i, j,
206                                                                    VOIDmode);
207               if (regno_save_mode[i][j] == VOIDmode && j == 1)
208                 {
209                   SET_HARD_REG_BIT (call_fixed_reg_set, i);
210                 }
211             }
212         }
213       else
214         regno_save_mode[i][1] = VOIDmode;
215     }
216
217   /* The following code tries to approximate the conditions under which
218      we can easily save and restore a register without scratch registers or
219      other complexities.  It will usually work, except under conditions where
220      the validity of an insn operand is dependent on the address offset.
221      No such cases are currently known.
222
223      We first find a typical offset from some BASE_REG_CLASS register.
224      This address is chosen by finding the first register in the class
225      and by finding the smallest power of two that is a valid offset from
226      that register in every mode we will use to save registers.  */
227
228   for (i = 0; i < FIRST_PSEUDO_REGISTER; i++)
229     if (TEST_HARD_REG_BIT
230         (reg_class_contents
231          [(int) base_reg_class (regno_save_mode[i][1], PLUS, CONST_INT)], i))
232       break;
233
234   gcc_assert (i < FIRST_PSEUDO_REGISTER);
235
236   addr_reg = gen_rtx_REG (Pmode, i);
237
238   for (offset = 1 << (HOST_BITS_PER_INT / 2); offset; offset >>= 1)
239     {
240       address = gen_rtx_PLUS (Pmode, addr_reg, GEN_INT (offset));
241
242       for (i = 0; i < FIRST_PSEUDO_REGISTER; i++)
243         if (regno_save_mode[i][1] != VOIDmode
244           && ! strict_memory_address_p (regno_save_mode[i][1], address))
245           break;
246
247       if (i == FIRST_PSEUDO_REGISTER)
248         break;
249     }
250
251   /* If we didn't find a valid address, we must use register indirect.  */
252   if (offset == 0)
253     address = addr_reg;
254
255   /* Next we try to form an insn to save and restore the register.  We
256      see if such an insn is recognized and meets its constraints.
257
258      To avoid lots of unnecessary RTL allocation, we construct all the RTL
259      once, then modify the memory and register operands in-place.  */
260
261   test_reg = gen_rtx_REG (VOIDmode, 0);
262   test_mem = gen_rtx_MEM (VOIDmode, address);
263   savepat = gen_rtx_SET (VOIDmode, test_mem, test_reg);
264   restpat = gen_rtx_SET (VOIDmode, test_reg, test_mem);
265
266   saveinsn = gen_rtx_INSN (VOIDmode, 0, 0, 0, 0, savepat, 0, -1, 0);
267   restinsn = gen_rtx_INSN (VOIDmode, 0, 0, 0, 0, restpat, 0, -1, 0);
268
269   for (i = 0; i < FIRST_PSEUDO_REGISTER; i++)
270     for (j = 1; j <= MOVE_MAX_WORDS; j++)
271       if (reg_save_code (i,regno_save_mode[i][j]) == -1)
272         {
273           regno_save_mode[i][j] = VOIDmode;
274           if (j == 1)
275             {
276               SET_HARD_REG_BIT (call_fixed_reg_set, i);
277               if (call_used_regs[i])
278                 SET_HARD_REG_BIT (no_caller_save_reg_set, i);
279             }
280         }
281 }
282
283 \f
284
285 /* Initialize save areas by showing that we haven't allocated any yet.  */
286
287 void
288 init_save_areas (void)
289 {
290   int i, j;
291
292   for (i = 0; i < FIRST_PSEUDO_REGISTER; i++)
293     for (j = 1; j <= MOVE_MAX_WORDS; j++)
294       regno_save_mem[i][j] = 0;
295   save_slots_num = 0;
296
297 }
298
299 /* The structure represents a hard register which should be saved
300    through the call.  It is used when the integrated register
301    allocator (IRA) is used and sharing save slots is on.  */
302 struct saved_hard_reg
303 {
304   /* Order number starting with 0.  */
305   int num;
306   /* The hard regno.  */
307   int hard_regno;
308   /* Execution frequency of all calls through which given hard
309      register should be saved.  */
310   int call_freq;
311   /* Stack slot reserved to save the hard register through calls.  */
312   rtx slot;
313   /* True if it is first hard register in the chain of hard registers
314      sharing the same stack slot.  */
315   int first_p;
316   /* Order number of the next hard register structure with the same
317      slot in the chain.  -1 represents end of the chain.  */
318   int next;
319 };
320
321 /* Map: hard register number to the corresponding structure.  */
322 static struct saved_hard_reg *hard_reg_map[FIRST_PSEUDO_REGISTER];
323
324 /* The number of all structures representing hard registers should be
325    saved, in order words, the number of used elements in the following
326    array.  */
327 static int saved_regs_num;
328
329 /* Pointers to all the structures.  Index is the order number of the
330    corresponding structure.  */
331 static struct saved_hard_reg *all_saved_regs[FIRST_PSEUDO_REGISTER];
332
333 /* First called function for work with saved hard registers.  */
334 static void
335 initiate_saved_hard_regs (void)
336 {
337   int i;
338
339   saved_regs_num = 0;
340   for (i = 0; i < FIRST_PSEUDO_REGISTER; i++)
341     hard_reg_map[i] = NULL;
342 }
343
344 /* Allocate and return new saved hard register with given REGNO and
345    CALL_FREQ.  */
346 static struct saved_hard_reg *
347 new_saved_hard_reg (int regno, int call_freq)
348 {
349   struct saved_hard_reg *saved_reg;
350
351   saved_reg
352     = (struct saved_hard_reg *) xmalloc (sizeof (struct saved_hard_reg));
353   hard_reg_map[regno] = all_saved_regs[saved_regs_num] = saved_reg;
354   saved_reg->num = saved_regs_num++;
355   saved_reg->hard_regno = regno;
356   saved_reg->call_freq = call_freq;
357   saved_reg->first_p = FALSE;
358   saved_reg->next = -1;
359   return saved_reg;
360 }
361
362 /* Free memory allocated for the saved hard registers.  */
363 static void
364 finish_saved_hard_regs (void)
365 {
366   int i;
367
368   for (i = 0; i < saved_regs_num; i++)
369     free (all_saved_regs[i]);
370 }
371
372 /* The function is used to sort the saved hard register structures
373    according their frequency.  */
374 static int
375 saved_hard_reg_compare_func (const void *v1p, const void *v2p)
376 {
377   const struct saved_hard_reg *p1 = *(struct saved_hard_reg * const *) v1p;
378   const struct saved_hard_reg *p2 = *(struct saved_hard_reg * const *) v2p;
379
380   if (flag_omit_frame_pointer)
381     {
382       if (p1->call_freq - p2->call_freq != 0)
383         return p1->call_freq - p2->call_freq;
384     }
385   else if (p2->call_freq - p1->call_freq != 0)
386     return p2->call_freq - p1->call_freq;
387
388   return p1->num - p2->num;
389 }
390
391 /* Allocate save areas for any hard registers that might need saving.
392    We take a conservative approach here and look for call-clobbered hard
393    registers that are assigned to pseudos that cross calls.  This may
394    overestimate slightly (especially if some of these registers are later
395    used as spill registers), but it should not be significant.
396
397    For IRA we use priority coloring to decrease stack slots needed for
398    saving hard registers through calls.  We build conflicts for them
399    to do coloring.
400
401    Future work:
402
403      In the fallback case we should iterate backwards across all possible
404      modes for the save, choosing the largest available one instead of
405      falling back to the smallest mode immediately.  (eg TF -> DF -> SF).
406
407      We do not try to use "move multiple" instructions that exist
408      on some machines (such as the 68k moveml).  It could be a win to try
409      and use them when possible.  The hard part is doing it in a way that is
410      machine independent since they might be saving non-consecutive
411      registers. (imagine caller-saving d0,d1,a0,a1 on the 68k) */
412
413 void
414 setup_save_areas (void)
415 {
416   int i, j, k;
417   unsigned int r;
418   HARD_REG_SET hard_regs_used;
419
420   /* Allocate space in the save area for the largest multi-register
421      pseudos first, then work backwards to single register
422      pseudos.  */
423
424   /* Find and record all call-used hard-registers in this function.  */
425   CLEAR_HARD_REG_SET (hard_regs_used);
426   for (i = FIRST_PSEUDO_REGISTER; i < max_regno; i++)
427     if (reg_renumber[i] >= 0 && REG_N_CALLS_CROSSED (i) > 0)
428       {
429         unsigned int regno = reg_renumber[i];
430         unsigned int endregno
431           = end_hard_regno (GET_MODE (regno_reg_rtx[i]), regno);
432         for (r = regno; r < endregno; r++)
433           if (call_used_regs[r])
434             SET_HARD_REG_BIT (hard_regs_used, r);
435       }
436
437   if (optimize && flag_ira_share_save_slots)
438     {
439       rtx insn, slot;
440       struct insn_chain *chain, *next;
441       char *saved_reg_conflicts;
442       unsigned int regno;
443       int next_k, freq;
444       struct saved_hard_reg *saved_reg, *saved_reg2, *saved_reg3;
445       int call_saved_regs_num;
446       struct saved_hard_reg *call_saved_regs[FIRST_PSEUDO_REGISTER];
447       HARD_REG_SET hard_regs_to_save, used_regs, this_insn_sets;
448       reg_set_iterator rsi;
449       int best_slot_num;
450       int prev_save_slots_num;
451       rtx prev_save_slots[FIRST_PSEUDO_REGISTER];
452
453       initiate_saved_hard_regs ();
454       /* Create hard reg saved regs.  */
455       for (chain = reload_insn_chain; chain != 0; chain = next)
456         {
457           insn = chain->insn;
458           next = chain->next;
459           if (!CALL_P (insn)
460               || find_reg_note (insn, REG_NORETURN, NULL))
461             continue;
462           freq = REG_FREQ_FROM_BB (BLOCK_FOR_INSN (insn));
463           REG_SET_TO_HARD_REG_SET (hard_regs_to_save,
464                                    &chain->live_throughout);
465           COPY_HARD_REG_SET (used_regs, call_used_reg_set);
466
467           /* Record all registers set in this call insn.  These don't
468              need to be saved.  N.B. the call insn might set a subreg
469              of a multi-hard-reg pseudo; then the pseudo is considered
470              live during the call, but the subreg that is set
471              isn't.  */
472           CLEAR_HARD_REG_SET (this_insn_sets);
473           note_stores (PATTERN (insn), mark_set_regs, &this_insn_sets);
474           /* Sibcalls are considered to set the return value.  */
475           if (SIBLING_CALL_P (insn) && crtl->return_rtx)
476             mark_set_regs (crtl->return_rtx, NULL_RTX, &this_insn_sets);
477
478           AND_COMPL_HARD_REG_SET (used_regs, call_fixed_reg_set);
479           AND_COMPL_HARD_REG_SET (used_regs, this_insn_sets);
480           AND_HARD_REG_SET (hard_regs_to_save, used_regs);
481           for (regno = 0; regno < FIRST_PSEUDO_REGISTER; regno++)
482             if (TEST_HARD_REG_BIT (hard_regs_to_save, regno))
483               {
484                 if (hard_reg_map[regno] != NULL)
485                   hard_reg_map[regno]->call_freq += freq;
486                 else
487                   saved_reg = new_saved_hard_reg (regno, freq);
488               }
489           /* Look through all live pseudos, mark their hard registers.  */
490           EXECUTE_IF_SET_IN_REG_SET
491             (&chain->live_throughout, FIRST_PSEUDO_REGISTER, regno, rsi)
492             {
493               int r = reg_renumber[regno];
494               int bound;
495
496               if (r < 0)
497                 continue;
498
499               bound = r + hard_regno_nregs[r][PSEUDO_REGNO_MODE (regno)];
500               for (; r < bound; r++)
501                 if (TEST_HARD_REG_BIT (used_regs, r))
502                   {
503                     if (hard_reg_map[r] != NULL)
504                       hard_reg_map[r]->call_freq += freq;
505                     else
506                       saved_reg = new_saved_hard_reg (r, freq);
507                     SET_HARD_REG_BIT (hard_regs_to_save, r);
508                   }
509             }
510         }
511       /* Find saved hard register conflicts.  */
512       saved_reg_conflicts = (char *) xmalloc (saved_regs_num * saved_regs_num);
513       memset (saved_reg_conflicts, 0, saved_regs_num * saved_regs_num);
514       for (chain = reload_insn_chain; chain != 0; chain = next)
515         {
516           call_saved_regs_num = 0;
517           insn = chain->insn;
518           next = chain->next;
519           if (!CALL_P (insn)
520               || find_reg_note (insn, REG_NORETURN, NULL))
521             continue;
522           REG_SET_TO_HARD_REG_SET (hard_regs_to_save,
523                                    &chain->live_throughout);
524           COPY_HARD_REG_SET (used_regs, call_used_reg_set);
525
526           /* Record all registers set in this call insn.  These don't
527              need to be saved.  N.B. the call insn might set a subreg
528              of a multi-hard-reg pseudo; then the pseudo is considered
529              live during the call, but the subreg that is set
530              isn't.  */
531           CLEAR_HARD_REG_SET (this_insn_sets);
532           note_stores (PATTERN (insn), mark_set_regs, &this_insn_sets);
533           /* Sibcalls are considered to set the return value,
534              compare df-scan.c:df_get_call_refs.  */
535           if (SIBLING_CALL_P (insn) && crtl->return_rtx)
536             mark_set_regs (crtl->return_rtx, NULL_RTX, &this_insn_sets);
537
538           AND_COMPL_HARD_REG_SET (used_regs, call_fixed_reg_set);
539           AND_COMPL_HARD_REG_SET (used_regs, this_insn_sets);
540           AND_HARD_REG_SET (hard_regs_to_save, used_regs);
541           for (regno = 0; regno < FIRST_PSEUDO_REGISTER; regno++)
542             if (TEST_HARD_REG_BIT (hard_regs_to_save, regno))
543               {
544                 gcc_assert (hard_reg_map[regno] != NULL);
545                 call_saved_regs[call_saved_regs_num++] = hard_reg_map[regno];
546               }
547           /* Look through all live pseudos, mark their hard registers.  */
548           EXECUTE_IF_SET_IN_REG_SET
549             (&chain->live_throughout, FIRST_PSEUDO_REGISTER, regno, rsi)
550             {
551               int r = reg_renumber[regno];
552               int bound;
553
554               if (r < 0)
555                 continue;
556
557               bound = r + hard_regno_nregs[r][PSEUDO_REGNO_MODE (regno)];
558               for (; r < bound; r++)
559                 if (TEST_HARD_REG_BIT (used_regs, r))
560                   call_saved_regs[call_saved_regs_num++] = hard_reg_map[r];
561             }
562           for (i = 0; i < call_saved_regs_num; i++)
563             {
564               saved_reg = call_saved_regs[i];
565               for (j = 0; j < call_saved_regs_num; j++)
566                 if (i != j)
567                   {
568                     saved_reg2 = call_saved_regs[j];
569                     saved_reg_conflicts[saved_reg->num * saved_regs_num
570                                         + saved_reg2->num]
571                       = saved_reg_conflicts[saved_reg2->num * saved_regs_num
572                                             + saved_reg->num]
573                       = TRUE;
574                   }
575             }
576         }
577       /* Sort saved hard regs.  */
578       qsort (all_saved_regs, saved_regs_num, sizeof (struct saved_hard_reg *),
579              saved_hard_reg_compare_func);
580       /* Initiate slots available from the previous reload
581          iteration.  */
582       prev_save_slots_num = save_slots_num;
583       memcpy (prev_save_slots, save_slots, save_slots_num * sizeof (rtx));
584       save_slots_num = 0;
585       /* Allocate stack slots for the saved hard registers.  */
586       for (i = 0; i < saved_regs_num; i++)
587         {
588           saved_reg = all_saved_regs[i];
589           regno = saved_reg->hard_regno;
590           for (j = 0; j < i; j++)
591             {
592               saved_reg2 = all_saved_regs[j];
593               if (! saved_reg2->first_p)
594                 continue;
595               slot = saved_reg2->slot;
596               for (k = j; k >= 0; k = next_k)
597                 {
598                   saved_reg3 = all_saved_regs[k];
599                   next_k = saved_reg3->next;
600                   if (saved_reg_conflicts[saved_reg->num * saved_regs_num
601                                           + saved_reg3->num])
602                     break;
603                 }
604               if (k < 0
605                   && (GET_MODE_SIZE (regno_save_mode[regno][1])
606                       <= GET_MODE_SIZE (regno_save_mode
607                                         [saved_reg2->hard_regno][1])))
608                 {
609                   saved_reg->slot
610                     = adjust_address_nv
611                       (slot, regno_save_mode[saved_reg->hard_regno][1], 0);
612                   regno_save_mem[regno][1] = saved_reg->slot;
613                   saved_reg->next = saved_reg2->next;
614                   saved_reg2->next = i;
615                   if (dump_file != NULL)
616                     fprintf (dump_file, "%d uses slot of %d\n",
617                              regno, saved_reg2->hard_regno);
618                   break;
619                 }
620             }
621           if (j == i)
622             {
623               saved_reg->first_p = TRUE;
624               for (best_slot_num = -1, j = 0; j < prev_save_slots_num; j++)
625                 {
626                   slot = prev_save_slots[j];
627                   if (slot == NULL_RTX)
628                     continue;
629                   if (GET_MODE_SIZE (regno_save_mode[regno][1])
630                       <= GET_MODE_SIZE (GET_MODE (slot))
631                       && best_slot_num < 0)
632                     best_slot_num = j;
633                   if (GET_MODE (slot) == regno_save_mode[regno][1])
634                     break;
635                 }
636               if (best_slot_num >= 0)
637                 {
638                   saved_reg->slot = prev_save_slots[best_slot_num];
639                   saved_reg->slot
640                     = adjust_address_nv
641                       (saved_reg->slot,
642                        regno_save_mode[saved_reg->hard_regno][1], 0);
643                   if (dump_file != NULL)
644                     fprintf (dump_file,
645                              "%d uses a slot from prev iteration\n", regno);
646                   prev_save_slots[best_slot_num] = NULL_RTX;
647                   if (best_slot_num + 1 == prev_save_slots_num)
648                     prev_save_slots_num--;
649                 }
650               else
651                 {
652                   saved_reg->slot
653                     = assign_stack_local_1
654                       (regno_save_mode[regno][1],
655                        GET_MODE_SIZE (regno_save_mode[regno][1]), 0, true);
656                   if (dump_file != NULL)
657                     fprintf (dump_file, "%d uses a new slot\n", regno);
658                 }
659               regno_save_mem[regno][1] = saved_reg->slot;
660               save_slots[save_slots_num++] = saved_reg->slot;
661             }
662         }
663       free (saved_reg_conflicts);
664       finish_saved_hard_regs ();
665     }
666   else
667     {
668       /* Now run through all the call-used hard-registers and allocate
669          space for them in the caller-save area.  Try to allocate space
670          in a manner which allows multi-register saves/restores to be done.  */
671
672       for (i = 0; i < FIRST_PSEUDO_REGISTER; i++)
673         for (j = MOVE_MAX_WORDS; j > 0; j--)
674           {
675             int do_save = 1;
676
677             /* If no mode exists for this size, try another.  Also break out
678                if we have already saved this hard register.  */
679             if (regno_save_mode[i][j] == VOIDmode || regno_save_mem[i][1] != 0)
680               continue;
681
682             /* See if any register in this group has been saved.  */
683             for (k = 0; k < j; k++)
684               if (regno_save_mem[i + k][1])
685                 {
686                   do_save = 0;
687                   break;
688                 }
689             if (! do_save)
690               continue;
691
692             for (k = 0; k < j; k++)
693               if (! TEST_HARD_REG_BIT (hard_regs_used, i + k))
694                 {
695                   do_save = 0;
696                   break;
697                 }
698             if (! do_save)
699               continue;
700
701             /* We have found an acceptable mode to store in.  Since
702                hard register is always saved in the widest mode
703                available, the mode may be wider than necessary, it is
704                OK to reduce the alignment of spill space.  We will
705                verify that it is equal to or greater than required
706                when we restore and save the hard register in
707                insert_restore and insert_save.  */
708             regno_save_mem[i][j]
709               = assign_stack_local_1 (regno_save_mode[i][j],
710                                       GET_MODE_SIZE (regno_save_mode[i][j]),
711                                       0, true);
712
713             /* Setup single word save area just in case...  */
714             for (k = 0; k < j; k++)
715               /* This should not depend on WORDS_BIG_ENDIAN.
716                  The order of words in regs is the same as in memory.  */
717               regno_save_mem[i + k][1]
718                 = adjust_address_nv (regno_save_mem[i][j],
719                                      regno_save_mode[i + k][1],
720                                      k * UNITS_PER_WORD);
721           }
722     }
723
724   /* Now loop again and set the alias set of any save areas we made to
725      the alias set used to represent frame objects.  */
726   for (i = 0; i < FIRST_PSEUDO_REGISTER; i++)
727     for (j = MOVE_MAX_WORDS; j > 0; j--)
728       if (regno_save_mem[i][j] != 0)
729         set_mem_alias_set (regno_save_mem[i][j], get_frame_alias_set ());
730 }
731
732 \f
733
734 /* Find the places where hard regs are live across calls and save them.  */
735
736 void
737 save_call_clobbered_regs (void)
738 {
739   struct insn_chain *chain, *next, *last = NULL;
740   enum machine_mode save_mode [FIRST_PSEUDO_REGISTER];
741
742   /* Computed in mark_set_regs, holds all registers set by the current
743      instruction.  */
744   HARD_REG_SET this_insn_sets;
745
746   CLEAR_HARD_REG_SET (hard_regs_saved);
747   n_regs_saved = 0;
748
749   for (chain = reload_insn_chain; chain != 0; chain = next)
750     {
751       rtx insn = chain->insn;
752       enum rtx_code code = GET_CODE (insn);
753
754       next = chain->next;
755
756       gcc_assert (!chain->is_caller_save_insn);
757
758       if (NONDEBUG_INSN_P (insn))
759         {
760           /* If some registers have been saved, see if INSN references
761              any of them.  We must restore them before the insn if so.  */
762
763           if (n_regs_saved)
764             {
765               int regno;
766               HARD_REG_SET this_insn_sets;
767
768               if (code == JUMP_INSN)
769                 /* Restore all registers if this is a JUMP_INSN.  */
770                 COPY_HARD_REG_SET (referenced_regs, hard_regs_saved);
771               else
772                 {
773                   CLEAR_HARD_REG_SET (referenced_regs);
774                   mark_referenced_regs (&PATTERN (insn),
775                                         mark_reg_as_referenced, NULL);
776                   AND_HARD_REG_SET (referenced_regs, hard_regs_saved);
777                 }
778
779               for (regno = 0; regno < FIRST_PSEUDO_REGISTER; regno++)
780                 if (TEST_HARD_REG_BIT (referenced_regs, regno))
781                   regno += insert_restore (chain, 1, regno, MOVE_MAX_WORDS,
782                                            save_mode);
783               /* If a saved register is set after the call, this means we no
784                  longer should restore it.  This can happen when parts of a
785                  multi-word pseudo do not conflict with other pseudos, so
786                  IRA may allocate the same hard register for both.  One may
787                  be live across the call, while the other is set
788                  afterwards.  */
789               CLEAR_HARD_REG_SET (this_insn_sets);
790               note_stores (PATTERN (insn), mark_set_regs, &this_insn_sets);
791               AND_COMPL_HARD_REG_SET (hard_regs_saved, this_insn_sets);
792             }
793
794           if (code == CALL_INSN
795               && ! SIBLING_CALL_P (insn)
796               && ! find_reg_note (insn, REG_NORETURN, NULL))
797             {
798               unsigned regno;
799               HARD_REG_SET hard_regs_to_save;
800               reg_set_iterator rsi;
801
802               /* Use the register life information in CHAIN to compute which
803                  regs are live during the call.  */
804               REG_SET_TO_HARD_REG_SET (hard_regs_to_save,
805                                        &chain->live_throughout);
806               /* Save hard registers always in the widest mode available.  */
807               for (regno = 0; regno < FIRST_PSEUDO_REGISTER; regno++)
808                 if (TEST_HARD_REG_BIT (hard_regs_to_save, regno))
809                   save_mode [regno] = regno_save_mode [regno][1];
810                 else
811                   save_mode [regno] = VOIDmode;
812
813               /* Look through all live pseudos, mark their hard registers
814                  and choose proper mode for saving.  */
815               EXECUTE_IF_SET_IN_REG_SET
816                 (&chain->live_throughout, FIRST_PSEUDO_REGISTER, regno, rsi)
817                 {
818                   int r = reg_renumber[regno];
819                   int nregs;
820                   enum machine_mode mode;
821
822                   if (r < 0)
823                     continue;
824                   nregs = hard_regno_nregs[r][PSEUDO_REGNO_MODE (regno)];
825                   mode = HARD_REGNO_CALLER_SAVE_MODE
826                     (r, nregs, PSEUDO_REGNO_MODE (regno));
827                   if (GET_MODE_BITSIZE (mode)
828                       > GET_MODE_BITSIZE (save_mode[r]))
829                     save_mode[r] = mode;
830                   while (nregs-- > 0)
831                     SET_HARD_REG_BIT (hard_regs_to_save, r + nregs);
832                 }
833
834               /* Record all registers set in this call insn.  These don't need
835                  to be saved.  N.B. the call insn might set a subreg of a
836                  multi-hard-reg pseudo; then the pseudo is considered live
837                  during the call, but the subreg that is set isn't.  */
838               CLEAR_HARD_REG_SET (this_insn_sets);
839               note_stores (PATTERN (insn), mark_set_regs, &this_insn_sets);
840
841               /* Compute which hard regs must be saved before this call.  */
842               AND_COMPL_HARD_REG_SET (hard_regs_to_save, call_fixed_reg_set);
843               AND_COMPL_HARD_REG_SET (hard_regs_to_save, this_insn_sets);
844               AND_COMPL_HARD_REG_SET (hard_regs_to_save, hard_regs_saved);
845               AND_HARD_REG_SET (hard_regs_to_save, call_used_reg_set);
846
847               for (regno = 0; regno < FIRST_PSEUDO_REGISTER; regno++)
848                 if (TEST_HARD_REG_BIT (hard_regs_to_save, regno))
849                   regno += insert_save (chain, 1, regno, &hard_regs_to_save, save_mode);
850
851               /* Must recompute n_regs_saved.  */
852               n_regs_saved = 0;
853               for (regno = 0; regno < FIRST_PSEUDO_REGISTER; regno++)
854                 if (TEST_HARD_REG_BIT (hard_regs_saved, regno))
855                   n_regs_saved++;
856             }
857           last = chain;
858         }
859       else if (DEBUG_INSN_P (insn) && n_regs_saved)
860         mark_referenced_regs (&PATTERN (insn),
861                               replace_reg_with_saved_mem,
862                               save_mode);
863
864       if (chain->next == 0 || chain->next->block != chain->block)
865         {
866           int regno;
867           /* At the end of the basic block, we must restore any registers that
868              remain saved.  If the last insn in the block is a JUMP_INSN, put
869              the restore before the insn, otherwise, put it after the insn.  */
870
871           if (DEBUG_INSN_P (insn) && last && last->block == chain->block)
872             {
873               rtx ins, prev;
874               basic_block bb = BLOCK_FOR_INSN (insn);
875
876               /* When adding hard reg restores after a DEBUG_INSN, move
877                  all notes between last real insn and this DEBUG_INSN after
878                  the DEBUG_INSN, otherwise we could get code
879                  -g/-g0 differences.  */
880               for (ins = PREV_INSN (insn); ins != last->insn; ins = prev)
881                 {
882                   prev = PREV_INSN (ins);
883                   if (NOTE_P (ins))
884                     {
885                       NEXT_INSN (prev) = NEXT_INSN (ins);
886                       PREV_INSN (NEXT_INSN (ins)) = prev;
887                       PREV_INSN (ins) = insn;
888                       NEXT_INSN (ins) = NEXT_INSN (insn);
889                       NEXT_INSN (insn) = ins;
890                       if (NEXT_INSN (ins))
891                         PREV_INSN (NEXT_INSN (ins)) = ins;
892                       if (BB_END (bb) == insn)
893                         BB_END (bb) = ins;
894                     }
895                   else
896                     gcc_assert (DEBUG_INSN_P (ins));
897                 }
898             }
899           last = NULL;
900
901           if (n_regs_saved)
902             for (regno = 0; regno < FIRST_PSEUDO_REGISTER; regno++)
903               if (TEST_HARD_REG_BIT (hard_regs_saved, regno))
904                 regno += insert_restore (chain, JUMP_P (insn),
905                                          regno, MOVE_MAX_WORDS, save_mode);
906         }
907     }
908 }
909
910 /* Here from note_stores, or directly from save_call_clobbered_regs, when
911    an insn stores a value in a register.
912    Set the proper bit or bits in this_insn_sets.  All pseudos that have
913    been assigned hard regs have had their register number changed already,
914    so we can ignore pseudos.  */
915 static void
916 mark_set_regs (rtx reg, const_rtx setter ATTRIBUTE_UNUSED, void *data)
917 {
918   int regno, endregno, i;
919   HARD_REG_SET *this_insn_sets = (HARD_REG_SET *) data;
920
921   if (GET_CODE (reg) == SUBREG)
922     {
923       rtx inner = SUBREG_REG (reg);
924       if (!REG_P (inner) || REGNO (inner) >= FIRST_PSEUDO_REGISTER)
925         return;
926       regno = subreg_regno (reg);
927       endregno = regno + subreg_nregs (reg);
928     }
929   else if (REG_P (reg)
930            && REGNO (reg) < FIRST_PSEUDO_REGISTER)
931     {
932       regno = REGNO (reg);
933       endregno = END_HARD_REGNO (reg);
934     }
935   else
936     return;
937
938   for (i = regno; i < endregno; i++)
939     SET_HARD_REG_BIT (*this_insn_sets, i);
940 }
941
942 /* Here from note_stores when an insn stores a value in a register.
943    Set the proper bit or bits in the passed regset.  All pseudos that have
944    been assigned hard regs have had their register number changed already,
945    so we can ignore pseudos.  */
946 static void
947 add_stored_regs (rtx reg, const_rtx setter, void *data)
948 {
949   int regno, endregno, i;
950   enum machine_mode mode = GET_MODE (reg);
951   int offset = 0;
952
953   if (GET_CODE (setter) == CLOBBER)
954     return;
955
956   if (GET_CODE (reg) == SUBREG
957       && REG_P (SUBREG_REG (reg))
958       && REGNO (SUBREG_REG (reg)) < FIRST_PSEUDO_REGISTER)
959     {
960       offset = subreg_regno_offset (REGNO (SUBREG_REG (reg)),
961                                     GET_MODE (SUBREG_REG (reg)),
962                                     SUBREG_BYTE (reg),
963                                     GET_MODE (reg));
964       regno = REGNO (SUBREG_REG (reg)) + offset;
965       endregno = regno + subreg_nregs (reg);
966     }
967   else
968     {
969       if (!REG_P (reg) || REGNO (reg) >= FIRST_PSEUDO_REGISTER)
970         return;
971
972       regno = REGNO (reg) + offset;
973       endregno = end_hard_regno (mode, regno);
974     }
975
976   for (i = regno; i < endregno; i++)
977     SET_REGNO_REG_SET ((regset) data, i);
978 }
979
980 /* Walk X and record all referenced registers in REFERENCED_REGS.  */
981 static void
982 mark_referenced_regs (rtx *loc, refmarker_fn *mark, void *arg)
983 {
984   enum rtx_code code = GET_CODE (*loc);
985   const char *fmt;
986   int i, j;
987
988   if (code == SET)
989     mark_referenced_regs (&SET_SRC (*loc), mark, arg);
990   if (code == SET || code == CLOBBER)
991     {
992       loc = &SET_DEST (*loc);
993       code = GET_CODE (*loc);
994       if ((code == REG && REGNO (*loc) < FIRST_PSEUDO_REGISTER)
995           || code == PC || code == CC0
996           || (code == SUBREG && REG_P (SUBREG_REG (*loc))
997               && REGNO (SUBREG_REG (*loc)) < FIRST_PSEUDO_REGISTER
998               /* If we're setting only part of a multi-word register,
999                  we shall mark it as referenced, because the words
1000                  that are not being set should be restored.  */
1001               && ((GET_MODE_SIZE (GET_MODE (*loc))
1002                    >= GET_MODE_SIZE (GET_MODE (SUBREG_REG (*loc))))
1003                   || (GET_MODE_SIZE (GET_MODE (SUBREG_REG (*loc)))
1004                       <= UNITS_PER_WORD))))
1005         return;
1006     }
1007   if (code == MEM || code == SUBREG)
1008     {
1009       loc = &XEXP (*loc, 0);
1010       code = GET_CODE (*loc);
1011     }
1012
1013   if (code == REG)
1014     {
1015       int regno = REGNO (*loc);
1016       int hardregno = (regno < FIRST_PSEUDO_REGISTER ? regno
1017                        : reg_renumber[regno]);
1018
1019       if (hardregno >= 0)
1020         mark (loc, GET_MODE (*loc), hardregno, arg);
1021       else if (arg)
1022         /* ??? Will we ever end up with an equiv expression in a debug
1023            insn, that would have required restoring a reg, or will
1024            reload take care of it for us?  */
1025         return;
1026       /* If this is a pseudo that did not get a hard register, scan its
1027          memory location, since it might involve the use of another
1028          register, which might be saved.  */
1029       else if (reg_equiv_mem[regno] != 0)
1030         mark_referenced_regs (&XEXP (reg_equiv_mem[regno], 0), mark, arg);
1031       else if (reg_equiv_address[regno] != 0)
1032         mark_referenced_regs (&reg_equiv_address[regno], mark, arg);
1033       return;
1034     }
1035
1036   fmt = GET_RTX_FORMAT (code);
1037   for (i = GET_RTX_LENGTH (code) - 1; i >= 0; i--)
1038     {
1039       if (fmt[i] == 'e')
1040         mark_referenced_regs (&XEXP (*loc, i), mark, arg);
1041       else if (fmt[i] == 'E')
1042         for (j = XVECLEN (*loc, i) - 1; j >= 0; j--)
1043           mark_referenced_regs (&XVECEXP (*loc, i, j), mark, arg);
1044     }
1045 }
1046
1047 /* Parameter function for mark_referenced_regs() that adds registers
1048    present in the insn and in equivalent mems and addresses to
1049    referenced_regs.  */
1050
1051 static void
1052 mark_reg_as_referenced (rtx *loc ATTRIBUTE_UNUSED,
1053                         enum machine_mode mode,
1054                         int hardregno,
1055                         void *arg ATTRIBUTE_UNUSED)
1056 {
1057   add_to_hard_reg_set (&referenced_regs, mode, hardregno);
1058 }
1059
1060 /* Parameter function for mark_referenced_regs() that replaces
1061    registers referenced in a debug_insn that would have been restored,
1062    should it be a non-debug_insn, with their save locations.  */
1063
1064 static void
1065 replace_reg_with_saved_mem (rtx *loc,
1066                             enum machine_mode mode,
1067                             int regno,
1068                             void *arg)
1069 {
1070   unsigned int i, nregs = hard_regno_nregs [regno][mode];
1071   rtx mem;
1072   enum machine_mode *save_mode = (enum machine_mode *)arg;
1073
1074   for (i = 0; i < nregs; i++)
1075     if (TEST_HARD_REG_BIT (hard_regs_saved, regno + i))
1076       break;
1077
1078   /* If none of the registers in the range would need restoring, we're
1079      all set.  */
1080   if (i == nregs)
1081     return;
1082
1083   while (++i < nregs)
1084     if (!TEST_HARD_REG_BIT (hard_regs_saved, regno + i))
1085       break;
1086
1087   if (i == nregs
1088       && regno_save_mem[regno][nregs])
1089     {
1090       mem = copy_rtx (regno_save_mem[regno][nregs]);
1091
1092       if (nregs == (unsigned int) hard_regno_nregs[regno][save_mode[regno]])
1093         mem = adjust_address_nv (mem, save_mode[regno], 0);
1094
1095       if (GET_MODE (mem) != mode)
1096         {
1097           /* This is gen_lowpart_if_possible(), but without validating
1098              the newly-formed address.  */
1099           int offset = 0;
1100
1101           if (WORDS_BIG_ENDIAN)
1102             offset = (MAX (GET_MODE_SIZE (GET_MODE (mem)), UNITS_PER_WORD)
1103                       - MAX (GET_MODE_SIZE (mode), UNITS_PER_WORD));
1104           if (BYTES_BIG_ENDIAN)
1105             /* Adjust the address so that the address-after-the-data is
1106                unchanged.  */
1107             offset -= (MIN (UNITS_PER_WORD, GET_MODE_SIZE (mode))
1108                        - MIN (UNITS_PER_WORD, GET_MODE_SIZE (GET_MODE (mem))));
1109
1110           mem = adjust_address_nv (mem, mode, offset);
1111         }
1112     }
1113   else
1114     {
1115       mem = gen_rtx_CONCATN (mode, rtvec_alloc (nregs));
1116       for (i = 0; i < nregs; i++)
1117         if (TEST_HARD_REG_BIT (hard_regs_saved, regno + i))
1118           {
1119             gcc_assert (regno_save_mem[regno + i][1]);
1120             XVECEXP (mem, 0, i) = copy_rtx (regno_save_mem[regno + i][1]);
1121           }
1122         else
1123           {
1124             gcc_assert (save_mode[regno] != VOIDmode);
1125             XVECEXP (mem, 0, i) = gen_rtx_REG (save_mode [regno],
1126                                                regno + i);
1127           }
1128     }
1129
1130   gcc_assert (GET_MODE (mem) == mode);
1131   *loc = mem;
1132 }
1133
1134 \f
1135 /* Insert a sequence of insns to restore.  Place these insns in front of
1136    CHAIN if BEFORE_P is nonzero, behind the insn otherwise.  MAXRESTORE is
1137    the maximum number of registers which should be restored during this call.
1138    It should never be less than 1 since we only work with entire registers.
1139
1140    Note that we have verified in init_caller_save that we can do this
1141    with a simple SET, so use it.  Set INSN_CODE to what we save there
1142    since the address might not be valid so the insn might not be recognized.
1143    These insns will be reloaded and have register elimination done by
1144    find_reload, so we need not worry about that here.
1145
1146    Return the extra number of registers saved.  */
1147
1148 static int
1149 insert_restore (struct insn_chain *chain, int before_p, int regno,
1150                 int maxrestore, enum machine_mode *save_mode)
1151 {
1152   int i, k;
1153   rtx pat = NULL_RTX;
1154   int code;
1155   unsigned int numregs = 0;
1156   struct insn_chain *new_chain;
1157   rtx mem;
1158
1159   /* A common failure mode if register status is not correct in the
1160      RTL is for this routine to be called with a REGNO we didn't
1161      expect to save.  That will cause us to write an insn with a (nil)
1162      SET_DEST or SET_SRC.  Instead of doing so and causing a crash
1163      later, check for this common case here instead.  This will remove
1164      one step in debugging such problems.  */
1165   gcc_assert (regno_save_mem[regno][1]);
1166
1167   /* Get the pattern to emit and update our status.
1168
1169      See if we can restore `maxrestore' registers at once.  Work
1170      backwards to the single register case.  */
1171   for (i = maxrestore; i > 0; i--)
1172     {
1173       int j;
1174       int ok = 1;
1175
1176       if (regno_save_mem[regno][i] == 0)
1177         continue;
1178
1179       for (j = 0; j < i; j++)
1180         if (! TEST_HARD_REG_BIT (hard_regs_saved, regno + j))
1181           {
1182             ok = 0;
1183             break;
1184           }
1185       /* Must do this one restore at a time.  */
1186       if (! ok)
1187         continue;
1188
1189       numregs = i;
1190       break;
1191     }
1192
1193   mem = regno_save_mem [regno][numregs];
1194   if (save_mode [regno] != VOIDmode
1195       && save_mode [regno] != GET_MODE (mem)
1196       && numregs == (unsigned int) hard_regno_nregs[regno][save_mode [regno]]
1197       /* Check that insn to restore REGNO in save_mode[regno] is
1198          correct.  */
1199       && reg_save_code (regno, save_mode[regno]) >= 0)
1200     mem = adjust_address_nv (mem, save_mode[regno], 0);
1201   else
1202     mem = copy_rtx (mem);
1203
1204   /* Verify that the alignment of spill space is equal to or greater
1205      than required.  */
1206   gcc_assert (MIN (MAX_SUPPORTED_STACK_ALIGNMENT,
1207                    GET_MODE_ALIGNMENT (GET_MODE (mem))) <= MEM_ALIGN (mem));
1208
1209   pat = gen_rtx_SET (VOIDmode,
1210                      gen_rtx_REG (GET_MODE (mem),
1211                                   regno), mem);
1212   code = reg_restore_code (regno, GET_MODE (mem));
1213   new_chain = insert_one_insn (chain, before_p, code, pat);
1214
1215   /* Clear status for all registers we restored.  */
1216   for (k = 0; k < i; k++)
1217     {
1218       CLEAR_HARD_REG_BIT (hard_regs_saved, regno + k);
1219       SET_REGNO_REG_SET (&new_chain->dead_or_set, regno + k);
1220       n_regs_saved--;
1221     }
1222
1223   /* Tell our callers how many extra registers we saved/restored.  */
1224   return numregs - 1;
1225 }
1226
1227 /* Like insert_restore above, but save registers instead.  */
1228
1229 static int
1230 insert_save (struct insn_chain *chain, int before_p, int regno,
1231              HARD_REG_SET (*to_save), enum machine_mode *save_mode)
1232 {
1233   int i;
1234   unsigned int k;
1235   rtx pat = NULL_RTX;
1236   int code;
1237   unsigned int numregs = 0;
1238   struct insn_chain *new_chain;
1239   rtx mem;
1240
1241   /* A common failure mode if register status is not correct in the
1242      RTL is for this routine to be called with a REGNO we didn't
1243      expect to save.  That will cause us to write an insn with a (nil)
1244      SET_DEST or SET_SRC.  Instead of doing so and causing a crash
1245      later, check for this common case here.  This will remove one
1246      step in debugging such problems.  */
1247   gcc_assert (regno_save_mem[regno][1]);
1248
1249   /* Get the pattern to emit and update our status.
1250
1251      See if we can save several registers with a single instruction.
1252      Work backwards to the single register case.  */
1253   for (i = MOVE_MAX_WORDS; i > 0; i--)
1254     {
1255       int j;
1256       int ok = 1;
1257       if (regno_save_mem[regno][i] == 0)
1258         continue;
1259
1260       for (j = 0; j < i; j++)
1261         if (! TEST_HARD_REG_BIT (*to_save, regno + j))
1262           {
1263             ok = 0;
1264             break;
1265           }
1266       /* Must do this one save at a time.  */
1267       if (! ok)
1268         continue;
1269
1270       numregs = i;
1271       break;
1272     }
1273
1274   mem = regno_save_mem [regno][numregs];
1275   if (save_mode [regno] != VOIDmode
1276       && save_mode [regno] != GET_MODE (mem)
1277       && numregs == (unsigned int) hard_regno_nregs[regno][save_mode [regno]]
1278       /* Check that insn to save REGNO in save_mode[regno] is
1279          correct.  */
1280       && reg_save_code (regno, save_mode[regno]) >= 0)
1281     mem = adjust_address_nv (mem, save_mode[regno], 0);
1282   else
1283     mem = copy_rtx (mem);
1284
1285   /* Verify that the alignment of spill space is equal to or greater
1286      than required.  */
1287   gcc_assert (MIN (MAX_SUPPORTED_STACK_ALIGNMENT,
1288                    GET_MODE_ALIGNMENT (GET_MODE (mem))) <= MEM_ALIGN (mem));
1289
1290   pat = gen_rtx_SET (VOIDmode, mem,
1291                      gen_rtx_REG (GET_MODE (mem),
1292                                   regno));
1293   code = reg_save_code (regno, GET_MODE (mem));
1294   new_chain = insert_one_insn (chain, before_p, code, pat);
1295
1296   /* Set hard_regs_saved and dead_or_set for all the registers we saved.  */
1297   for (k = 0; k < numregs; k++)
1298     {
1299       SET_HARD_REG_BIT (hard_regs_saved, regno + k);
1300       SET_REGNO_REG_SET (&new_chain->dead_or_set, regno + k);
1301       n_regs_saved++;
1302     }
1303
1304   /* Tell our callers how many extra registers we saved/restored.  */
1305   return numregs - 1;
1306 }
1307
1308 /* A for_each_rtx callback used by add_used_regs.  Add the hard-register
1309    equivalent of each REG to regset DATA.  */
1310
1311 static int
1312 add_used_regs_1 (rtx *loc, void *data)
1313 {
1314   int regno, i;
1315   regset live;
1316   rtx x;
1317
1318   x = *loc;
1319   live = (regset) data;
1320   if (REG_P (x))
1321     {
1322       regno = REGNO (x);
1323       if (!HARD_REGISTER_NUM_P (regno))
1324         regno = reg_renumber[regno];
1325       if (regno >= 0)
1326         for (i = hard_regno_nregs[regno][GET_MODE (x)] - 1; i >= 0; i--)
1327           SET_REGNO_REG_SET (live, regno + i);
1328     }
1329   return 0;
1330 }
1331
1332 /* A note_uses callback used by insert_one_insn.  Add the hard-register
1333    equivalent of each REG to regset DATA.  */
1334
1335 static void
1336 add_used_regs (rtx *loc, void *data)
1337 {
1338   for_each_rtx (loc, add_used_regs_1, data);
1339 }
1340
1341 /* Emit a new caller-save insn and set the code.  */
1342 static struct insn_chain *
1343 insert_one_insn (struct insn_chain *chain, int before_p, int code, rtx pat)
1344 {
1345   rtx insn = chain->insn;
1346   struct insn_chain *new_chain;
1347
1348 #ifdef HAVE_cc0
1349   /* If INSN references CC0, put our insns in front of the insn that sets
1350      CC0.  This is always safe, since the only way we could be passed an
1351      insn that references CC0 is for a restore, and doing a restore earlier
1352      isn't a problem.  We do, however, assume here that CALL_INSNs don't
1353      reference CC0.  Guard against non-INSN's like CODE_LABEL.  */
1354
1355   if ((NONJUMP_INSN_P (insn) || JUMP_P (insn))
1356       && before_p
1357       && reg_referenced_p (cc0_rtx, PATTERN (insn)))
1358     chain = chain->prev, insn = chain->insn;
1359 #endif
1360
1361   new_chain = new_insn_chain ();
1362   if (before_p)
1363     {
1364       rtx link;
1365
1366       new_chain->prev = chain->prev;
1367       if (new_chain->prev != 0)
1368         new_chain->prev->next = new_chain;
1369       else
1370         reload_insn_chain = new_chain;
1371
1372       chain->prev = new_chain;
1373       new_chain->next = chain;
1374       new_chain->insn = emit_insn_before (pat, insn);
1375       /* ??? It would be nice if we could exclude the already / still saved
1376          registers from the live sets.  */
1377       COPY_REG_SET (&new_chain->live_throughout, &chain->live_throughout);
1378       note_uses (&PATTERN (chain->insn), add_used_regs,
1379                  &new_chain->live_throughout);
1380       /* If CHAIN->INSN is a call, then the registers which contain
1381          the arguments to the function are live in the new insn.  */
1382       if (CALL_P (chain->insn))
1383         for (link = CALL_INSN_FUNCTION_USAGE (chain->insn);
1384              link != NULL_RTX;
1385              link = XEXP (link, 1))
1386           note_uses (&XEXP (link, 0), add_used_regs,
1387                      &new_chain->live_throughout);
1388
1389       CLEAR_REG_SET (&new_chain->dead_or_set);
1390       if (chain->insn == BB_HEAD (BASIC_BLOCK (chain->block)))
1391         BB_HEAD (BASIC_BLOCK (chain->block)) = new_chain->insn;
1392     }
1393   else
1394     {
1395       new_chain->next = chain->next;
1396       if (new_chain->next != 0)
1397         new_chain->next->prev = new_chain;
1398       chain->next = new_chain;
1399       new_chain->prev = chain;
1400       new_chain->insn = emit_insn_after (pat, insn);
1401       /* ??? It would be nice if we could exclude the already / still saved
1402          registers from the live sets, and observe REG_UNUSED notes.  */
1403       COPY_REG_SET (&new_chain->live_throughout, &chain->live_throughout);
1404       /* Registers that are set in CHAIN->INSN live in the new insn.
1405          (Unless there is a REG_UNUSED note for them, but we don't
1406           look for them here.) */
1407       note_stores (PATTERN (chain->insn), add_stored_regs,
1408                    &new_chain->live_throughout);
1409       CLEAR_REG_SET (&new_chain->dead_or_set);
1410       if (chain->insn == BB_END (BASIC_BLOCK (chain->block)))
1411         BB_END (BASIC_BLOCK (chain->block)) = new_chain->insn;
1412     }
1413   new_chain->block = chain->block;
1414   new_chain->is_caller_save_insn = 1;
1415
1416   INSN_CODE (new_chain->insn) = code;
1417   return new_chain;
1418 }
1419 #include "gt-caller-save.h"