* c-ubsan.c (ubsan_instrument_shift): Use type0.
[official-gcc.git] / gcc / caller-save.c
blob673a47051da184e80bb9fb9046b93ef10cb764a6
1 /* Save and restore call-clobbered registers which are live across a call.
2 Copyright (C) 1989-2015 Free Software Foundation, Inc.
4 This file is part of GCC.
6 GCC is free software; you can redistribute it and/or modify it under
7 the terms of the GNU General Public License as published by the Free
8 Software Foundation; either version 3, or (at your option) any later
9 version.
11 GCC is distributed in the hope that it will be useful, but WITHOUT ANY
12 WARRANTY; without even the implied warranty of MERCHANTABILITY or
13 FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
14 for more details.
16 You should have received a copy of the GNU General Public License
17 along with GCC; see the file COPYING3. If not see
18 <http://www.gnu.org/licenses/>. */
20 #include "config.h"
21 #include "system.h"
22 #include "coretypes.h"
23 #include "tm.h"
24 #include "rtl.h"
25 #include "regs.h"
26 #include "insn-config.h"
27 #include "flags.h"
28 #include "hard-reg-set.h"
29 #include "recog.h"
30 #include "predict.h"
31 #include "vec.h"
32 #include "hashtab.h"
33 #include "hash-set.h"
34 #include "machmode.h"
35 #include "input.h"
36 #include "function.h"
37 #include "dominance.h"
38 #include "cfg.h"
39 #include "basic-block.h"
40 #include "df.h"
41 #include "reload.h"
42 #include "symtab.h"
43 #include "statistics.h"
44 #include "double-int.h"
45 #include "real.h"
46 #include "fixed-value.h"
47 #include "alias.h"
48 #include "wide-int.h"
49 #include "inchash.h"
50 #include "tree.h"
51 #include "expmed.h"
52 #include "dojump.h"
53 #include "explow.h"
54 #include "calls.h"
55 #include "emit-rtl.h"
56 #include "varasm.h"
57 #include "stmt.h"
58 #include "expr.h"
59 #include "diagnostic-core.h"
60 #include "tm_p.h"
61 #include "addresses.h"
62 #include "ggc.h"
63 #include "dumpfile.h"
64 #include "rtl-iter.h"
66 #define MOVE_MAX_WORDS (MOVE_MAX / UNITS_PER_WORD)
68 #define regno_save_mode \
69 (this_target_reload->x_regno_save_mode)
70 #define cached_reg_save_code \
71 (this_target_reload->x_cached_reg_save_code)
72 #define cached_reg_restore_code \
73 (this_target_reload->x_cached_reg_restore_code)
75 /* For each hard register, a place on the stack where it can be saved,
76 if needed. */
78 static rtx
79 regno_save_mem[FIRST_PSEUDO_REGISTER][MAX_MOVE_MAX / MIN_UNITS_PER_WORD + 1];
81 /* The number of elements in the subsequent array. */
82 static int save_slots_num;
84 /* Allocated slots so far. */
85 static rtx save_slots[FIRST_PSEUDO_REGISTER];
87 /* Set of hard regs currently residing in save area (during insn scan). */
89 static HARD_REG_SET hard_regs_saved;
91 /* Number of registers currently in hard_regs_saved. */
93 static int n_regs_saved;
95 /* Computed by mark_referenced_regs, all regs referenced in a given
96 insn. */
97 static HARD_REG_SET referenced_regs;
100 typedef void refmarker_fn (rtx *loc, machine_mode mode, int hardregno,
101 void *mark_arg);
103 static int reg_save_code (int, machine_mode);
104 static int reg_restore_code (int, machine_mode);
106 struct saved_hard_reg;
107 static void initiate_saved_hard_regs (void);
108 static void new_saved_hard_reg (int, int);
109 static void finish_saved_hard_regs (void);
110 static int saved_hard_reg_compare_func (const void *, const void *);
112 static void mark_set_regs (rtx, const_rtx, void *);
113 static void mark_referenced_regs (rtx *, refmarker_fn *mark, void *mark_arg);
114 static refmarker_fn mark_reg_as_referenced;
115 static refmarker_fn replace_reg_with_saved_mem;
116 static int insert_save (struct insn_chain *, int, int, HARD_REG_SET *,
117 machine_mode *);
118 static int insert_restore (struct insn_chain *, int, int, int,
119 machine_mode *);
120 static struct insn_chain *insert_one_insn (struct insn_chain *, int, int,
121 rtx);
122 static void add_stored_regs (rtx, const_rtx, void *);
126 static GTY(()) rtx savepat;
127 static GTY(()) rtx restpat;
128 static GTY(()) rtx test_reg;
129 static GTY(()) rtx test_mem;
130 static GTY(()) rtx_insn *saveinsn;
131 static GTY(()) rtx_insn *restinsn;
133 /* Return the INSN_CODE used to save register REG in mode MODE. */
134 static int
135 reg_save_code (int reg, machine_mode mode)
137 bool ok;
138 if (cached_reg_save_code[reg][mode])
139 return cached_reg_save_code[reg][mode];
140 if (!HARD_REGNO_MODE_OK (reg, mode))
142 /* Depending on how HARD_REGNO_MODE_OK is defined, range propagation
143 might deduce here that reg >= FIRST_PSEUDO_REGISTER. So the assert
144 below silences a warning. */
145 gcc_assert (reg < FIRST_PSEUDO_REGISTER);
146 cached_reg_save_code[reg][mode] = -1;
147 cached_reg_restore_code[reg][mode] = -1;
148 return -1;
151 /* Update the register number and modes of the register
152 and memory operand. */
153 set_mode_and_regno (test_reg, mode, reg);
154 PUT_MODE (test_mem, mode);
156 /* Force re-recognition of the modified insns. */
157 INSN_CODE (saveinsn) = -1;
158 INSN_CODE (restinsn) = -1;
160 cached_reg_save_code[reg][mode] = recog_memoized (saveinsn);
161 cached_reg_restore_code[reg][mode] = recog_memoized (restinsn);
163 /* Now extract both insns and see if we can meet their
164 constraints. We don't know here whether the save and restore will
165 be in size- or speed-tuned code, so just use the set of enabled
166 alternatives. */
167 ok = (cached_reg_save_code[reg][mode] != -1
168 && cached_reg_restore_code[reg][mode] != -1);
169 if (ok)
171 extract_insn (saveinsn);
172 ok = constrain_operands (1, get_enabled_alternatives (saveinsn));
173 extract_insn (restinsn);
174 ok &= constrain_operands (1, get_enabled_alternatives (restinsn));
177 if (! ok)
179 cached_reg_save_code[reg][mode] = -1;
180 cached_reg_restore_code[reg][mode] = -1;
182 gcc_assert (cached_reg_save_code[reg][mode]);
183 return cached_reg_save_code[reg][mode];
186 /* Return the INSN_CODE used to restore register REG in mode MODE. */
187 static int
188 reg_restore_code (int reg, machine_mode mode)
190 if (cached_reg_restore_code[reg][mode])
191 return cached_reg_restore_code[reg][mode];
192 /* Populate our cache. */
193 reg_save_code (reg, mode);
194 return cached_reg_restore_code[reg][mode];
197 /* Initialize for caller-save.
199 Look at all the hard registers that are used by a call and for which
200 reginfo.c has not already excluded from being used across a call.
202 Ensure that we can find a mode to save the register and that there is a
203 simple insn to save and restore the register. This latter check avoids
204 problems that would occur if we tried to save the MQ register of some
205 machines directly into memory. */
207 void
208 init_caller_save (void)
210 rtx addr_reg;
211 int offset;
212 rtx address;
213 int i, j;
215 if (caller_save_initialized_p)
216 return;
218 caller_save_initialized_p = true;
220 CLEAR_HARD_REG_SET (no_caller_save_reg_set);
221 /* First find all the registers that we need to deal with and all
222 the modes that they can have. If we can't find a mode to use,
223 we can't have the register live over calls. */
225 for (i = 0; i < FIRST_PSEUDO_REGISTER; i++)
227 if (call_used_regs[i]
228 && !TEST_HARD_REG_BIT (call_fixed_reg_set, i))
230 for (j = 1; j <= MOVE_MAX_WORDS; j++)
232 regno_save_mode[i][j] = HARD_REGNO_CALLER_SAVE_MODE (i, j,
233 VOIDmode);
234 if (regno_save_mode[i][j] == VOIDmode && j == 1)
236 SET_HARD_REG_BIT (call_fixed_reg_set, i);
240 else
241 regno_save_mode[i][1] = VOIDmode;
244 /* The following code tries to approximate the conditions under which
245 we can easily save and restore a register without scratch registers or
246 other complexities. It will usually work, except under conditions where
247 the validity of an insn operand is dependent on the address offset.
248 No such cases are currently known.
250 We first find a typical offset from some BASE_REG_CLASS register.
251 This address is chosen by finding the first register in the class
252 and by finding the smallest power of two that is a valid offset from
253 that register in every mode we will use to save registers. */
255 for (i = 0; i < FIRST_PSEUDO_REGISTER; i++)
256 if (TEST_HARD_REG_BIT
257 (reg_class_contents
258 [(int) base_reg_class (regno_save_mode[i][1], ADDR_SPACE_GENERIC,
259 PLUS, CONST_INT)], i))
260 break;
262 gcc_assert (i < FIRST_PSEUDO_REGISTER);
264 addr_reg = gen_rtx_REG (Pmode, i);
266 for (offset = 1 << (HOST_BITS_PER_INT / 2); offset; offset >>= 1)
268 address = gen_rtx_PLUS (Pmode, addr_reg, gen_int_mode (offset, Pmode));
270 for (i = 0; i < FIRST_PSEUDO_REGISTER; i++)
271 if (regno_save_mode[i][1] != VOIDmode
272 && ! strict_memory_address_p (regno_save_mode[i][1], address))
273 break;
275 if (i == FIRST_PSEUDO_REGISTER)
276 break;
279 /* If we didn't find a valid address, we must use register indirect. */
280 if (offset == 0)
281 address = addr_reg;
283 /* Next we try to form an insn to save and restore the register. We
284 see if such an insn is recognized and meets its constraints.
286 To avoid lots of unnecessary RTL allocation, we construct all the RTL
287 once, then modify the memory and register operands in-place. */
289 test_reg = gen_rtx_REG (word_mode, FIRST_PSEUDO_REGISTER);
290 test_mem = gen_rtx_MEM (word_mode, address);
291 savepat = gen_rtx_SET (test_mem, test_reg);
292 restpat = gen_rtx_SET (test_reg, test_mem);
294 saveinsn = gen_rtx_INSN (VOIDmode, 0, 0, 0, savepat, 0, -1, 0);
295 restinsn = gen_rtx_INSN (VOIDmode, 0, 0, 0, restpat, 0, -1, 0);
297 for (i = 0; i < FIRST_PSEUDO_REGISTER; i++)
298 for (j = 1; j <= MOVE_MAX_WORDS; j++)
299 if (reg_save_code (i,regno_save_mode[i][j]) == -1)
301 regno_save_mode[i][j] = VOIDmode;
302 if (j == 1)
304 SET_HARD_REG_BIT (call_fixed_reg_set, i);
305 if (call_used_regs[i])
306 SET_HARD_REG_BIT (no_caller_save_reg_set, i);
313 /* Initialize save areas by showing that we haven't allocated any yet. */
315 void
316 init_save_areas (void)
318 int i, j;
320 for (i = 0; i < FIRST_PSEUDO_REGISTER; i++)
321 for (j = 1; j <= MOVE_MAX_WORDS; j++)
322 regno_save_mem[i][j] = 0;
323 save_slots_num = 0;
327 /* The structure represents a hard register which should be saved
328 through the call. It is used when the integrated register
329 allocator (IRA) is used and sharing save slots is on. */
330 struct saved_hard_reg
332 /* Order number starting with 0. */
333 int num;
334 /* The hard regno. */
335 int hard_regno;
336 /* Execution frequency of all calls through which given hard
337 register should be saved. */
338 int call_freq;
339 /* Stack slot reserved to save the hard register through calls. */
340 rtx slot;
341 /* True if it is first hard register in the chain of hard registers
342 sharing the same stack slot. */
343 int first_p;
344 /* Order number of the next hard register structure with the same
345 slot in the chain. -1 represents end of the chain. */
346 int next;
349 /* Map: hard register number to the corresponding structure. */
350 static struct saved_hard_reg *hard_reg_map[FIRST_PSEUDO_REGISTER];
352 /* The number of all structures representing hard registers should be
353 saved, in order words, the number of used elements in the following
354 array. */
355 static int saved_regs_num;
357 /* Pointers to all the structures. Index is the order number of the
358 corresponding structure. */
359 static struct saved_hard_reg *all_saved_regs[FIRST_PSEUDO_REGISTER];
361 /* First called function for work with saved hard registers. */
362 static void
363 initiate_saved_hard_regs (void)
365 int i;
367 saved_regs_num = 0;
368 for (i = 0; i < FIRST_PSEUDO_REGISTER; i++)
369 hard_reg_map[i] = NULL;
372 /* Allocate and return new saved hard register with given REGNO and
373 CALL_FREQ. */
374 static void
375 new_saved_hard_reg (int regno, int call_freq)
377 struct saved_hard_reg *saved_reg;
379 saved_reg
380 = (struct saved_hard_reg *) xmalloc (sizeof (struct saved_hard_reg));
381 hard_reg_map[regno] = all_saved_regs[saved_regs_num] = saved_reg;
382 saved_reg->num = saved_regs_num++;
383 saved_reg->hard_regno = regno;
384 saved_reg->call_freq = call_freq;
385 saved_reg->first_p = FALSE;
386 saved_reg->next = -1;
389 /* Free memory allocated for the saved hard registers. */
390 static void
391 finish_saved_hard_regs (void)
393 int i;
395 for (i = 0; i < saved_regs_num; i++)
396 free (all_saved_regs[i]);
399 /* The function is used to sort the saved hard register structures
400 according their frequency. */
401 static int
402 saved_hard_reg_compare_func (const void *v1p, const void *v2p)
404 const struct saved_hard_reg *p1 = *(struct saved_hard_reg * const *) v1p;
405 const struct saved_hard_reg *p2 = *(struct saved_hard_reg * const *) v2p;
407 if (flag_omit_frame_pointer)
409 if (p1->call_freq - p2->call_freq != 0)
410 return p1->call_freq - p2->call_freq;
412 else if (p2->call_freq - p1->call_freq != 0)
413 return p2->call_freq - p1->call_freq;
415 return p1->num - p2->num;
418 /* Allocate save areas for any hard registers that might need saving.
419 We take a conservative approach here and look for call-clobbered hard
420 registers that are assigned to pseudos that cross calls. This may
421 overestimate slightly (especially if some of these registers are later
422 used as spill registers), but it should not be significant.
424 For IRA we use priority coloring to decrease stack slots needed for
425 saving hard registers through calls. We build conflicts for them
426 to do coloring.
428 Future work:
430 In the fallback case we should iterate backwards across all possible
431 modes for the save, choosing the largest available one instead of
432 falling back to the smallest mode immediately. (eg TF -> DF -> SF).
434 We do not try to use "move multiple" instructions that exist
435 on some machines (such as the 68k moveml). It could be a win to try
436 and use them when possible. The hard part is doing it in a way that is
437 machine independent since they might be saving non-consecutive
438 registers. (imagine caller-saving d0,d1,a0,a1 on the 68k) */
440 void
441 setup_save_areas (void)
443 int i, j, k, freq;
444 HARD_REG_SET hard_regs_used;
445 struct saved_hard_reg *saved_reg;
446 rtx_insn *insn;
447 struct insn_chain *chain, *next;
448 unsigned int regno;
449 HARD_REG_SET hard_regs_to_save, used_regs, this_insn_sets;
450 reg_set_iterator rsi;
452 CLEAR_HARD_REG_SET (hard_regs_used);
454 /* Find every CALL_INSN and record which hard regs are live across the
455 call into HARD_REG_MAP and HARD_REGS_USED. */
456 initiate_saved_hard_regs ();
457 /* Create hard reg saved regs. */
458 for (chain = reload_insn_chain; chain != 0; chain = next)
460 rtx cheap;
462 insn = chain->insn;
463 next = chain->next;
464 if (!CALL_P (insn)
465 || find_reg_note (insn, REG_NORETURN, NULL))
466 continue;
467 freq = REG_FREQ_FROM_BB (BLOCK_FOR_INSN (insn));
468 REG_SET_TO_HARD_REG_SET (hard_regs_to_save,
469 &chain->live_throughout);
470 get_call_reg_set_usage (insn, &used_regs, call_used_reg_set);
472 /* Record all registers set in this call insn. These don't
473 need to be saved. N.B. the call insn might set a subreg
474 of a multi-hard-reg pseudo; then the pseudo is considered
475 live during the call, but the subreg that is set
476 isn't. */
477 CLEAR_HARD_REG_SET (this_insn_sets);
478 note_stores (PATTERN (insn), mark_set_regs, &this_insn_sets);
479 /* Sibcalls are considered to set the return value. */
480 if (SIBLING_CALL_P (insn) && crtl->return_rtx)
481 mark_set_regs (crtl->return_rtx, NULL_RTX, &this_insn_sets);
483 AND_COMPL_HARD_REG_SET (used_regs, call_fixed_reg_set);
484 AND_COMPL_HARD_REG_SET (used_regs, this_insn_sets);
485 AND_HARD_REG_SET (hard_regs_to_save, used_regs);
486 for (regno = 0; regno < FIRST_PSEUDO_REGISTER; regno++)
487 if (TEST_HARD_REG_BIT (hard_regs_to_save, regno))
489 if (hard_reg_map[regno] != NULL)
490 hard_reg_map[regno]->call_freq += freq;
491 else
492 new_saved_hard_reg (regno, freq);
493 SET_HARD_REG_BIT (hard_regs_used, regno);
495 cheap = find_reg_note (insn, REG_RETURNED, NULL);
496 if (cheap)
497 cheap = XEXP (cheap, 0);
498 /* Look through all live pseudos, mark their hard registers. */
499 EXECUTE_IF_SET_IN_REG_SET
500 (&chain->live_throughout, FIRST_PSEUDO_REGISTER, regno, rsi)
502 int r = reg_renumber[regno];
503 int bound;
505 if (r < 0 || regno_reg_rtx[regno] == cheap)
506 continue;
508 bound = r + hard_regno_nregs[r][PSEUDO_REGNO_MODE (regno)];
509 for (; r < bound; r++)
510 if (TEST_HARD_REG_BIT (used_regs, r))
512 if (hard_reg_map[r] != NULL)
513 hard_reg_map[r]->call_freq += freq;
514 else
515 new_saved_hard_reg (r, freq);
516 SET_HARD_REG_BIT (hard_regs_to_save, r);
517 SET_HARD_REG_BIT (hard_regs_used, r);
522 /* If requested, figure out which hard regs can share save slots. */
523 if (optimize && flag_ira_share_save_slots)
525 rtx slot;
526 char *saved_reg_conflicts;
527 int next_k;
528 struct saved_hard_reg *saved_reg2, *saved_reg3;
529 int call_saved_regs_num;
530 struct saved_hard_reg *call_saved_regs[FIRST_PSEUDO_REGISTER];
531 int best_slot_num;
532 int prev_save_slots_num;
533 rtx prev_save_slots[FIRST_PSEUDO_REGISTER];
535 /* Find saved hard register conflicts. */
536 saved_reg_conflicts = (char *) xmalloc (saved_regs_num * saved_regs_num);
537 memset (saved_reg_conflicts, 0, saved_regs_num * saved_regs_num);
538 for (chain = reload_insn_chain; chain != 0; chain = next)
540 rtx cheap;
541 call_saved_regs_num = 0;
542 insn = chain->insn;
543 next = chain->next;
544 if (!CALL_P (insn)
545 || find_reg_note (insn, REG_NORETURN, NULL))
546 continue;
548 cheap = find_reg_note (insn, REG_RETURNED, NULL);
549 if (cheap)
550 cheap = XEXP (cheap, 0);
552 REG_SET_TO_HARD_REG_SET (hard_regs_to_save,
553 &chain->live_throughout);
554 get_call_reg_set_usage (insn, &used_regs, call_used_reg_set);
556 /* Record all registers set in this call insn. These don't
557 need to be saved. N.B. the call insn might set a subreg
558 of a multi-hard-reg pseudo; then the pseudo is considered
559 live during the call, but the subreg that is set
560 isn't. */
561 CLEAR_HARD_REG_SET (this_insn_sets);
562 note_stores (PATTERN (insn), mark_set_regs, &this_insn_sets);
563 /* Sibcalls are considered to set the return value,
564 compare df-scan.c:df_get_call_refs. */
565 if (SIBLING_CALL_P (insn) && crtl->return_rtx)
566 mark_set_regs (crtl->return_rtx, NULL_RTX, &this_insn_sets);
568 AND_COMPL_HARD_REG_SET (used_regs, call_fixed_reg_set);
569 AND_COMPL_HARD_REG_SET (used_regs, this_insn_sets);
570 AND_HARD_REG_SET (hard_regs_to_save, used_regs);
571 for (regno = 0; regno < FIRST_PSEUDO_REGISTER; regno++)
572 if (TEST_HARD_REG_BIT (hard_regs_to_save, regno))
574 gcc_assert (hard_reg_map[regno] != NULL);
575 call_saved_regs[call_saved_regs_num++] = hard_reg_map[regno];
577 /* Look through all live pseudos, mark their hard registers. */
578 EXECUTE_IF_SET_IN_REG_SET
579 (&chain->live_throughout, FIRST_PSEUDO_REGISTER, regno, rsi)
581 int r = reg_renumber[regno];
582 int bound;
584 if (r < 0 || regno_reg_rtx[regno] == cheap)
585 continue;
587 bound = r + hard_regno_nregs[r][PSEUDO_REGNO_MODE (regno)];
588 for (; r < bound; r++)
589 if (TEST_HARD_REG_BIT (used_regs, r))
590 call_saved_regs[call_saved_regs_num++] = hard_reg_map[r];
592 for (i = 0; i < call_saved_regs_num; i++)
594 saved_reg = call_saved_regs[i];
595 for (j = 0; j < call_saved_regs_num; j++)
596 if (i != j)
598 saved_reg2 = call_saved_regs[j];
599 saved_reg_conflicts[saved_reg->num * saved_regs_num
600 + saved_reg2->num]
601 = saved_reg_conflicts[saved_reg2->num * saved_regs_num
602 + saved_reg->num]
603 = TRUE;
607 /* Sort saved hard regs. */
608 qsort (all_saved_regs, saved_regs_num, sizeof (struct saved_hard_reg *),
609 saved_hard_reg_compare_func);
610 /* Initiate slots available from the previous reload
611 iteration. */
612 prev_save_slots_num = save_slots_num;
613 memcpy (prev_save_slots, save_slots, save_slots_num * sizeof (rtx));
614 save_slots_num = 0;
615 /* Allocate stack slots for the saved hard registers. */
616 for (i = 0; i < saved_regs_num; i++)
618 saved_reg = all_saved_regs[i];
619 regno = saved_reg->hard_regno;
620 for (j = 0; j < i; j++)
622 saved_reg2 = all_saved_regs[j];
623 if (! saved_reg2->first_p)
624 continue;
625 slot = saved_reg2->slot;
626 for (k = j; k >= 0; k = next_k)
628 saved_reg3 = all_saved_regs[k];
629 next_k = saved_reg3->next;
630 if (saved_reg_conflicts[saved_reg->num * saved_regs_num
631 + saved_reg3->num])
632 break;
634 if (k < 0
635 && (GET_MODE_SIZE (regno_save_mode[regno][1])
636 <= GET_MODE_SIZE (regno_save_mode
637 [saved_reg2->hard_regno][1])))
639 saved_reg->slot
640 = adjust_address_nv
641 (slot, regno_save_mode[saved_reg->hard_regno][1], 0);
642 regno_save_mem[regno][1] = saved_reg->slot;
643 saved_reg->next = saved_reg2->next;
644 saved_reg2->next = i;
645 if (dump_file != NULL)
646 fprintf (dump_file, "%d uses slot of %d\n",
647 regno, saved_reg2->hard_regno);
648 break;
651 if (j == i)
653 saved_reg->first_p = TRUE;
654 for (best_slot_num = -1, j = 0; j < prev_save_slots_num; j++)
656 slot = prev_save_slots[j];
657 if (slot == NULL_RTX)
658 continue;
659 if (GET_MODE_SIZE (regno_save_mode[regno][1])
660 <= GET_MODE_SIZE (GET_MODE (slot))
661 && best_slot_num < 0)
662 best_slot_num = j;
663 if (GET_MODE (slot) == regno_save_mode[regno][1])
664 break;
666 if (best_slot_num >= 0)
668 saved_reg->slot = prev_save_slots[best_slot_num];
669 saved_reg->slot
670 = adjust_address_nv
671 (saved_reg->slot,
672 regno_save_mode[saved_reg->hard_regno][1], 0);
673 if (dump_file != NULL)
674 fprintf (dump_file,
675 "%d uses a slot from prev iteration\n", regno);
676 prev_save_slots[best_slot_num] = NULL_RTX;
677 if (best_slot_num + 1 == prev_save_slots_num)
678 prev_save_slots_num--;
680 else
682 saved_reg->slot
683 = assign_stack_local_1
684 (regno_save_mode[regno][1],
685 GET_MODE_SIZE (regno_save_mode[regno][1]), 0,
686 ASLK_REDUCE_ALIGN);
687 if (dump_file != NULL)
688 fprintf (dump_file, "%d uses a new slot\n", regno);
690 regno_save_mem[regno][1] = saved_reg->slot;
691 save_slots[save_slots_num++] = saved_reg->slot;
694 free (saved_reg_conflicts);
695 finish_saved_hard_regs ();
697 else
699 /* We are not sharing slots.
701 Run through all the call-used hard-registers and allocate
702 space for each in the caller-save area. Try to allocate space
703 in a manner which allows multi-register saves/restores to be done. */
705 for (i = 0; i < FIRST_PSEUDO_REGISTER; i++)
706 for (j = MOVE_MAX_WORDS; j > 0; j--)
708 int do_save = 1;
710 /* If no mode exists for this size, try another. Also break out
711 if we have already saved this hard register. */
712 if (regno_save_mode[i][j] == VOIDmode || regno_save_mem[i][1] != 0)
713 continue;
715 /* See if any register in this group has been saved. */
716 for (k = 0; k < j; k++)
717 if (regno_save_mem[i + k][1])
719 do_save = 0;
720 break;
722 if (! do_save)
723 continue;
725 for (k = 0; k < j; k++)
726 if (! TEST_HARD_REG_BIT (hard_regs_used, i + k))
728 do_save = 0;
729 break;
731 if (! do_save)
732 continue;
734 /* We have found an acceptable mode to store in. Since
735 hard register is always saved in the widest mode
736 available, the mode may be wider than necessary, it is
737 OK to reduce the alignment of spill space. We will
738 verify that it is equal to or greater than required
739 when we restore and save the hard register in
740 insert_restore and insert_save. */
741 regno_save_mem[i][j]
742 = assign_stack_local_1 (regno_save_mode[i][j],
743 GET_MODE_SIZE (regno_save_mode[i][j]),
744 0, ASLK_REDUCE_ALIGN);
746 /* Setup single word save area just in case... */
747 for (k = 0; k < j; k++)
748 /* This should not depend on WORDS_BIG_ENDIAN.
749 The order of words in regs is the same as in memory. */
750 regno_save_mem[i + k][1]
751 = adjust_address_nv (regno_save_mem[i][j],
752 regno_save_mode[i + k][1],
753 k * UNITS_PER_WORD);
757 /* Now loop again and set the alias set of any save areas we made to
758 the alias set used to represent frame objects. */
759 for (i = 0; i < FIRST_PSEUDO_REGISTER; i++)
760 for (j = MOVE_MAX_WORDS; j > 0; j--)
761 if (regno_save_mem[i][j] != 0)
762 set_mem_alias_set (regno_save_mem[i][j], get_frame_alias_set ());
767 /* Find the places where hard regs are live across calls and save them. */
769 void
770 save_call_clobbered_regs (void)
772 struct insn_chain *chain, *next, *last = NULL;
773 machine_mode save_mode [FIRST_PSEUDO_REGISTER];
775 /* Computed in mark_set_regs, holds all registers set by the current
776 instruction. */
777 HARD_REG_SET this_insn_sets;
779 CLEAR_HARD_REG_SET (hard_regs_saved);
780 n_regs_saved = 0;
782 for (chain = reload_insn_chain; chain != 0; chain = next)
784 rtx_insn *insn = chain->insn;
785 enum rtx_code code = GET_CODE (insn);
787 next = chain->next;
789 gcc_assert (!chain->is_caller_save_insn);
791 if (NONDEBUG_INSN_P (insn))
793 /* If some registers have been saved, see if INSN references
794 any of them. We must restore them before the insn if so. */
796 if (n_regs_saved)
798 int regno;
799 HARD_REG_SET this_insn_sets;
801 if (code == JUMP_INSN)
802 /* Restore all registers if this is a JUMP_INSN. */
803 COPY_HARD_REG_SET (referenced_regs, hard_regs_saved);
804 else
806 CLEAR_HARD_REG_SET (referenced_regs);
807 mark_referenced_regs (&PATTERN (insn),
808 mark_reg_as_referenced, NULL);
809 AND_HARD_REG_SET (referenced_regs, hard_regs_saved);
812 for (regno = 0; regno < FIRST_PSEUDO_REGISTER; regno++)
813 if (TEST_HARD_REG_BIT (referenced_regs, regno))
814 regno += insert_restore (chain, 1, regno, MOVE_MAX_WORDS,
815 save_mode);
816 /* If a saved register is set after the call, this means we no
817 longer should restore it. This can happen when parts of a
818 multi-word pseudo do not conflict with other pseudos, so
819 IRA may allocate the same hard register for both. One may
820 be live across the call, while the other is set
821 afterwards. */
822 CLEAR_HARD_REG_SET (this_insn_sets);
823 note_stores (PATTERN (insn), mark_set_regs, &this_insn_sets);
824 AND_COMPL_HARD_REG_SET (hard_regs_saved, this_insn_sets);
827 if (code == CALL_INSN
828 && ! SIBLING_CALL_P (insn)
829 && ! find_reg_note (insn, REG_NORETURN, NULL))
831 unsigned regno;
832 HARD_REG_SET hard_regs_to_save;
833 HARD_REG_SET call_def_reg_set;
834 reg_set_iterator rsi;
835 rtx cheap;
837 cheap = find_reg_note (insn, REG_RETURNED, NULL);
838 if (cheap)
839 cheap = XEXP (cheap, 0);
841 /* Use the register life information in CHAIN to compute which
842 regs are live during the call. */
843 REG_SET_TO_HARD_REG_SET (hard_regs_to_save,
844 &chain->live_throughout);
845 /* Save hard registers always in the widest mode available. */
846 for (regno = 0; regno < FIRST_PSEUDO_REGISTER; regno++)
847 if (TEST_HARD_REG_BIT (hard_regs_to_save, regno))
848 save_mode [regno] = regno_save_mode [regno][1];
849 else
850 save_mode [regno] = VOIDmode;
852 /* Look through all live pseudos, mark their hard registers
853 and choose proper mode for saving. */
854 EXECUTE_IF_SET_IN_REG_SET
855 (&chain->live_throughout, FIRST_PSEUDO_REGISTER, regno, rsi)
857 int r = reg_renumber[regno];
858 int nregs;
859 machine_mode mode;
861 if (r < 0 || regno_reg_rtx[regno] == cheap)
862 continue;
863 nregs = hard_regno_nregs[r][PSEUDO_REGNO_MODE (regno)];
864 mode = HARD_REGNO_CALLER_SAVE_MODE
865 (r, nregs, PSEUDO_REGNO_MODE (regno));
866 if (GET_MODE_BITSIZE (mode)
867 > GET_MODE_BITSIZE (save_mode[r]))
868 save_mode[r] = mode;
869 while (nregs-- > 0)
870 SET_HARD_REG_BIT (hard_regs_to_save, r + nregs);
873 /* Record all registers set in this call insn. These don't need
874 to be saved. N.B. the call insn might set a subreg of a
875 multi-hard-reg pseudo; then the pseudo is considered live
876 during the call, but the subreg that is set isn't. */
877 CLEAR_HARD_REG_SET (this_insn_sets);
878 note_stores (PATTERN (insn), mark_set_regs, &this_insn_sets);
880 /* Compute which hard regs must be saved before this call. */
881 AND_COMPL_HARD_REG_SET (hard_regs_to_save, call_fixed_reg_set);
882 AND_COMPL_HARD_REG_SET (hard_regs_to_save, this_insn_sets);
883 AND_COMPL_HARD_REG_SET (hard_regs_to_save, hard_regs_saved);
884 get_call_reg_set_usage (insn, &call_def_reg_set,
885 call_used_reg_set);
886 AND_HARD_REG_SET (hard_regs_to_save, call_def_reg_set);
888 for (regno = 0; regno < FIRST_PSEUDO_REGISTER; regno++)
889 if (TEST_HARD_REG_BIT (hard_regs_to_save, regno))
890 regno += insert_save (chain, 1, regno, &hard_regs_to_save, save_mode);
892 /* Must recompute n_regs_saved. */
893 n_regs_saved = 0;
894 for (regno = 0; regno < FIRST_PSEUDO_REGISTER; regno++)
895 if (TEST_HARD_REG_BIT (hard_regs_saved, regno))
896 n_regs_saved++;
898 if (cheap
899 && HARD_REGISTER_P (cheap)
900 && TEST_HARD_REG_BIT (call_used_reg_set, REGNO (cheap)))
902 rtx dest, newpat;
903 rtx pat = PATTERN (insn);
904 if (GET_CODE (pat) == PARALLEL)
905 pat = XVECEXP (pat, 0, 0);
906 dest = SET_DEST (pat);
907 /* For multiple return values dest is PARALLEL.
908 Currently we handle only single return value case. */
909 if (REG_P (dest))
911 newpat = gen_rtx_SET (cheap, copy_rtx (dest));
912 chain = insert_one_insn (chain, 0, -1, newpat);
916 last = chain;
918 else if (DEBUG_INSN_P (insn) && n_regs_saved)
919 mark_referenced_regs (&PATTERN (insn),
920 replace_reg_with_saved_mem,
921 save_mode);
923 if (chain->next == 0 || chain->next->block != chain->block)
925 int regno;
926 /* At the end of the basic block, we must restore any registers that
927 remain saved. If the last insn in the block is a JUMP_INSN, put
928 the restore before the insn, otherwise, put it after the insn. */
930 if (n_regs_saved
931 && DEBUG_INSN_P (insn)
932 && last
933 && last->block == chain->block)
935 rtx_insn *ins, *prev;
936 basic_block bb = BLOCK_FOR_INSN (insn);
938 /* When adding hard reg restores after a DEBUG_INSN, move
939 all notes between last real insn and this DEBUG_INSN after
940 the DEBUG_INSN, otherwise we could get code
941 -g/-g0 differences. */
942 for (ins = PREV_INSN (insn); ins != last->insn; ins = prev)
944 prev = PREV_INSN (ins);
945 if (NOTE_P (ins))
947 SET_NEXT_INSN (prev) = NEXT_INSN (ins);
948 SET_PREV_INSN (NEXT_INSN (ins)) = prev;
949 SET_PREV_INSN (ins) = insn;
950 SET_NEXT_INSN (ins) = NEXT_INSN (insn);
951 SET_NEXT_INSN (insn) = ins;
952 if (NEXT_INSN (ins))
953 SET_PREV_INSN (NEXT_INSN (ins)) = ins;
954 if (BB_END (bb) == insn)
955 BB_END (bb) = ins;
957 else
958 gcc_assert (DEBUG_INSN_P (ins));
961 last = NULL;
963 if (n_regs_saved)
964 for (regno = 0; regno < FIRST_PSEUDO_REGISTER; regno++)
965 if (TEST_HARD_REG_BIT (hard_regs_saved, regno))
966 regno += insert_restore (chain, JUMP_P (insn),
967 regno, MOVE_MAX_WORDS, save_mode);
972 /* Here from note_stores, or directly from save_call_clobbered_regs, when
973 an insn stores a value in a register.
974 Set the proper bit or bits in this_insn_sets. All pseudos that have
975 been assigned hard regs have had their register number changed already,
976 so we can ignore pseudos. */
977 static void
978 mark_set_regs (rtx reg, const_rtx setter ATTRIBUTE_UNUSED, void *data)
980 int regno, endregno, i;
981 HARD_REG_SET *this_insn_sets = (HARD_REG_SET *) data;
983 if (GET_CODE (reg) == SUBREG)
985 rtx inner = SUBREG_REG (reg);
986 if (!REG_P (inner) || REGNO (inner) >= FIRST_PSEUDO_REGISTER)
987 return;
988 regno = subreg_regno (reg);
989 endregno = regno + subreg_nregs (reg);
991 else if (REG_P (reg)
992 && REGNO (reg) < FIRST_PSEUDO_REGISTER)
994 regno = REGNO (reg);
995 endregno = END_REGNO (reg);
997 else
998 return;
1000 for (i = regno; i < endregno; i++)
1001 SET_HARD_REG_BIT (*this_insn_sets, i);
1004 /* Here from note_stores when an insn stores a value in a register.
1005 Set the proper bit or bits in the passed regset. All pseudos that have
1006 been assigned hard regs have had their register number changed already,
1007 so we can ignore pseudos. */
1008 static void
1009 add_stored_regs (rtx reg, const_rtx setter, void *data)
1011 int regno, endregno, i;
1012 machine_mode mode = GET_MODE (reg);
1013 int offset = 0;
1015 if (GET_CODE (setter) == CLOBBER)
1016 return;
1018 if (GET_CODE (reg) == SUBREG
1019 && REG_P (SUBREG_REG (reg))
1020 && REGNO (SUBREG_REG (reg)) < FIRST_PSEUDO_REGISTER)
1022 offset = subreg_regno_offset (REGNO (SUBREG_REG (reg)),
1023 GET_MODE (SUBREG_REG (reg)),
1024 SUBREG_BYTE (reg),
1025 GET_MODE (reg));
1026 regno = REGNO (SUBREG_REG (reg)) + offset;
1027 endregno = regno + subreg_nregs (reg);
1029 else
1031 if (!REG_P (reg) || REGNO (reg) >= FIRST_PSEUDO_REGISTER)
1032 return;
1034 regno = REGNO (reg) + offset;
1035 endregno = end_hard_regno (mode, regno);
1038 for (i = regno; i < endregno; i++)
1039 SET_REGNO_REG_SET ((regset) data, i);
1042 /* Walk X and record all referenced registers in REFERENCED_REGS. */
1043 static void
1044 mark_referenced_regs (rtx *loc, refmarker_fn *mark, void *arg)
1046 enum rtx_code code = GET_CODE (*loc);
1047 const char *fmt;
1048 int i, j;
1050 if (code == SET)
1051 mark_referenced_regs (&SET_SRC (*loc), mark, arg);
1052 if (code == SET || code == CLOBBER)
1054 loc = &SET_DEST (*loc);
1055 code = GET_CODE (*loc);
1056 if ((code == REG && REGNO (*loc) < FIRST_PSEUDO_REGISTER)
1057 || code == PC || code == CC0
1058 || (code == SUBREG && REG_P (SUBREG_REG (*loc))
1059 && REGNO (SUBREG_REG (*loc)) < FIRST_PSEUDO_REGISTER
1060 /* If we're setting only part of a multi-word register,
1061 we shall mark it as referenced, because the words
1062 that are not being set should be restored. */
1063 && ((GET_MODE_SIZE (GET_MODE (*loc))
1064 >= GET_MODE_SIZE (GET_MODE (SUBREG_REG (*loc))))
1065 || (GET_MODE_SIZE (GET_MODE (SUBREG_REG (*loc)))
1066 <= UNITS_PER_WORD))))
1067 return;
1069 if (code == MEM || code == SUBREG)
1071 loc = &XEXP (*loc, 0);
1072 code = GET_CODE (*loc);
1075 if (code == REG)
1077 int regno = REGNO (*loc);
1078 int hardregno = (regno < FIRST_PSEUDO_REGISTER ? regno
1079 : reg_renumber[regno]);
1081 if (hardregno >= 0)
1082 mark (loc, GET_MODE (*loc), hardregno, arg);
1083 else if (arg)
1084 /* ??? Will we ever end up with an equiv expression in a debug
1085 insn, that would have required restoring a reg, or will
1086 reload take care of it for us? */
1087 return;
1088 /* If this is a pseudo that did not get a hard register, scan its
1089 memory location, since it might involve the use of another
1090 register, which might be saved. */
1091 else if (reg_equiv_mem (regno) != 0)
1092 mark_referenced_regs (&XEXP (reg_equiv_mem (regno), 0), mark, arg);
1093 else if (reg_equiv_address (regno) != 0)
1094 mark_referenced_regs (&reg_equiv_address (regno), mark, arg);
1095 return;
1098 fmt = GET_RTX_FORMAT (code);
1099 for (i = GET_RTX_LENGTH (code) - 1; i >= 0; i--)
1101 if (fmt[i] == 'e')
1102 mark_referenced_regs (&XEXP (*loc, i), mark, arg);
1103 else if (fmt[i] == 'E')
1104 for (j = XVECLEN (*loc, i) - 1; j >= 0; j--)
1105 mark_referenced_regs (&XVECEXP (*loc, i, j), mark, arg);
1109 /* Parameter function for mark_referenced_regs() that adds registers
1110 present in the insn and in equivalent mems and addresses to
1111 referenced_regs. */
1113 static void
1114 mark_reg_as_referenced (rtx *loc ATTRIBUTE_UNUSED,
1115 machine_mode mode,
1116 int hardregno,
1117 void *arg ATTRIBUTE_UNUSED)
1119 add_to_hard_reg_set (&referenced_regs, mode, hardregno);
1122 /* Parameter function for mark_referenced_regs() that replaces
1123 registers referenced in a debug_insn that would have been restored,
1124 should it be a non-debug_insn, with their save locations. */
1126 static void
1127 replace_reg_with_saved_mem (rtx *loc,
1128 machine_mode mode,
1129 int regno,
1130 void *arg)
1132 unsigned int i, nregs = hard_regno_nregs [regno][mode];
1133 rtx mem;
1134 machine_mode *save_mode = (machine_mode *)arg;
1136 for (i = 0; i < nregs; i++)
1137 if (TEST_HARD_REG_BIT (hard_regs_saved, regno + i))
1138 break;
1140 /* If none of the registers in the range would need restoring, we're
1141 all set. */
1142 if (i == nregs)
1143 return;
1145 while (++i < nregs)
1146 if (!TEST_HARD_REG_BIT (hard_regs_saved, regno + i))
1147 break;
1149 if (i == nregs
1150 && regno_save_mem[regno][nregs])
1152 mem = copy_rtx (regno_save_mem[regno][nregs]);
1154 if (nregs == (unsigned int) hard_regno_nregs[regno][save_mode[regno]])
1155 mem = adjust_address_nv (mem, save_mode[regno], 0);
1157 if (GET_MODE (mem) != mode)
1159 /* This is gen_lowpart_if_possible(), but without validating
1160 the newly-formed address. */
1161 int offset = 0;
1163 if (WORDS_BIG_ENDIAN)
1164 offset = (MAX (GET_MODE_SIZE (GET_MODE (mem)), UNITS_PER_WORD)
1165 - MAX (GET_MODE_SIZE (mode), UNITS_PER_WORD));
1166 if (BYTES_BIG_ENDIAN)
1167 /* Adjust the address so that the address-after-the-data is
1168 unchanged. */
1169 offset -= (MIN (UNITS_PER_WORD, GET_MODE_SIZE (mode))
1170 - MIN (UNITS_PER_WORD, GET_MODE_SIZE (GET_MODE (mem))));
1172 mem = adjust_address_nv (mem, mode, offset);
1175 else
1177 mem = gen_rtx_CONCATN (mode, rtvec_alloc (nregs));
1178 for (i = 0; i < nregs; i++)
1179 if (TEST_HARD_REG_BIT (hard_regs_saved, regno + i))
1181 gcc_assert (regno_save_mem[regno + i][1]);
1182 XVECEXP (mem, 0, i) = copy_rtx (regno_save_mem[regno + i][1]);
1184 else
1186 machine_mode smode = save_mode[regno];
1187 gcc_assert (smode != VOIDmode);
1188 if (hard_regno_nregs [regno][smode] > 1)
1189 smode = mode_for_size (GET_MODE_SIZE (mode) / nregs,
1190 GET_MODE_CLASS (mode), 0);
1191 XVECEXP (mem, 0, i) = gen_rtx_REG (smode, regno + i);
1195 gcc_assert (GET_MODE (mem) == mode);
1196 *loc = mem;
1200 /* Insert a sequence of insns to restore. Place these insns in front of
1201 CHAIN if BEFORE_P is nonzero, behind the insn otherwise. MAXRESTORE is
1202 the maximum number of registers which should be restored during this call.
1203 It should never be less than 1 since we only work with entire registers.
1205 Note that we have verified in init_caller_save that we can do this
1206 with a simple SET, so use it. Set INSN_CODE to what we save there
1207 since the address might not be valid so the insn might not be recognized.
1208 These insns will be reloaded and have register elimination done by
1209 find_reload, so we need not worry about that here.
1211 Return the extra number of registers saved. */
1213 static int
1214 insert_restore (struct insn_chain *chain, int before_p, int regno,
1215 int maxrestore, machine_mode *save_mode)
1217 int i, k;
1218 rtx pat = NULL_RTX;
1219 int code;
1220 unsigned int numregs = 0;
1221 struct insn_chain *new_chain;
1222 rtx mem;
1224 /* A common failure mode if register status is not correct in the
1225 RTL is for this routine to be called with a REGNO we didn't
1226 expect to save. That will cause us to write an insn with a (nil)
1227 SET_DEST or SET_SRC. Instead of doing so and causing a crash
1228 later, check for this common case here instead. This will remove
1229 one step in debugging such problems. */
1230 gcc_assert (regno_save_mem[regno][1]);
1232 /* Get the pattern to emit and update our status.
1234 See if we can restore `maxrestore' registers at once. Work
1235 backwards to the single register case. */
1236 for (i = maxrestore; i > 0; i--)
1238 int j;
1239 int ok = 1;
1241 if (regno_save_mem[regno][i] == 0)
1242 continue;
1244 for (j = 0; j < i; j++)
1245 if (! TEST_HARD_REG_BIT (hard_regs_saved, regno + j))
1247 ok = 0;
1248 break;
1250 /* Must do this one restore at a time. */
1251 if (! ok)
1252 continue;
1254 numregs = i;
1255 break;
1258 mem = regno_save_mem [regno][numregs];
1259 if (save_mode [regno] != VOIDmode
1260 && save_mode [regno] != GET_MODE (mem)
1261 && numregs == (unsigned int) hard_regno_nregs[regno][save_mode [regno]]
1262 /* Check that insn to restore REGNO in save_mode[regno] is
1263 correct. */
1264 && reg_save_code (regno, save_mode[regno]) >= 0)
1265 mem = adjust_address_nv (mem, save_mode[regno], 0);
1266 else
1267 mem = copy_rtx (mem);
1269 /* Verify that the alignment of spill space is equal to or greater
1270 than required. */
1271 gcc_assert (MIN (MAX_SUPPORTED_STACK_ALIGNMENT,
1272 GET_MODE_ALIGNMENT (GET_MODE (mem))) <= MEM_ALIGN (mem));
1274 pat = gen_rtx_SET (gen_rtx_REG (GET_MODE (mem), regno), mem);
1275 code = reg_restore_code (regno, GET_MODE (mem));
1276 new_chain = insert_one_insn (chain, before_p, code, pat);
1278 /* Clear status for all registers we restored. */
1279 for (k = 0; k < i; k++)
1281 CLEAR_HARD_REG_BIT (hard_regs_saved, regno + k);
1282 SET_REGNO_REG_SET (&new_chain->dead_or_set, regno + k);
1283 n_regs_saved--;
1286 /* Tell our callers how many extra registers we saved/restored. */
1287 return numregs - 1;
1290 /* Like insert_restore above, but save registers instead. */
1292 static int
1293 insert_save (struct insn_chain *chain, int before_p, int regno,
1294 HARD_REG_SET (*to_save), machine_mode *save_mode)
1296 int i;
1297 unsigned int k;
1298 rtx pat = NULL_RTX;
1299 int code;
1300 unsigned int numregs = 0;
1301 struct insn_chain *new_chain;
1302 rtx mem;
1304 /* A common failure mode if register status is not correct in the
1305 RTL is for this routine to be called with a REGNO we didn't
1306 expect to save. That will cause us to write an insn with a (nil)
1307 SET_DEST or SET_SRC. Instead of doing so and causing a crash
1308 later, check for this common case here. This will remove one
1309 step in debugging such problems. */
1310 gcc_assert (regno_save_mem[regno][1]);
1312 /* Get the pattern to emit and update our status.
1314 See if we can save several registers with a single instruction.
1315 Work backwards to the single register case. */
1316 for (i = MOVE_MAX_WORDS; i > 0; i--)
1318 int j;
1319 int ok = 1;
1320 if (regno_save_mem[regno][i] == 0)
1321 continue;
1323 for (j = 0; j < i; j++)
1324 if (! TEST_HARD_REG_BIT (*to_save, regno + j))
1326 ok = 0;
1327 break;
1329 /* Must do this one save at a time. */
1330 if (! ok)
1331 continue;
1333 numregs = i;
1334 break;
1337 mem = regno_save_mem [regno][numregs];
1338 if (save_mode [regno] != VOIDmode
1339 && save_mode [regno] != GET_MODE (mem)
1340 && numregs == (unsigned int) hard_regno_nregs[regno][save_mode [regno]]
1341 /* Check that insn to save REGNO in save_mode[regno] is
1342 correct. */
1343 && reg_save_code (regno, save_mode[regno]) >= 0)
1344 mem = adjust_address_nv (mem, save_mode[regno], 0);
1345 else
1346 mem = copy_rtx (mem);
1348 /* Verify that the alignment of spill space is equal to or greater
1349 than required. */
1350 gcc_assert (MIN (MAX_SUPPORTED_STACK_ALIGNMENT,
1351 GET_MODE_ALIGNMENT (GET_MODE (mem))) <= MEM_ALIGN (mem));
1353 pat = gen_rtx_SET (mem, gen_rtx_REG (GET_MODE (mem), regno));
1354 code = reg_save_code (regno, GET_MODE (mem));
1355 new_chain = insert_one_insn (chain, before_p, code, pat);
1357 /* Set hard_regs_saved and dead_or_set for all the registers we saved. */
1358 for (k = 0; k < numregs; k++)
1360 SET_HARD_REG_BIT (hard_regs_saved, regno + k);
1361 SET_REGNO_REG_SET (&new_chain->dead_or_set, regno + k);
1362 n_regs_saved++;
1365 /* Tell our callers how many extra registers we saved/restored. */
1366 return numregs - 1;
1369 /* A note_uses callback used by insert_one_insn. Add the hard-register
1370 equivalent of each REG to regset DATA. */
1372 static void
1373 add_used_regs (rtx *loc, void *data)
1375 subrtx_iterator::array_type array;
1376 FOR_EACH_SUBRTX (iter, array, *loc, NONCONST)
1378 const_rtx x = *iter;
1379 if (REG_P (x))
1381 unsigned int regno = REGNO (x);
1382 if (HARD_REGISTER_NUM_P (regno))
1383 bitmap_set_range ((regset) data, regno,
1384 hard_regno_nregs[regno][GET_MODE (x)]);
1385 else
1386 gcc_checking_assert (reg_renumber[regno] < 0);
1391 /* Emit a new caller-save insn and set the code. */
1392 static struct insn_chain *
1393 insert_one_insn (struct insn_chain *chain, int before_p, int code, rtx pat)
1395 rtx_insn *insn = chain->insn;
1396 struct insn_chain *new_chain;
1398 /* If INSN references CC0, put our insns in front of the insn that sets
1399 CC0. This is always safe, since the only way we could be passed an
1400 insn that references CC0 is for a restore, and doing a restore earlier
1401 isn't a problem. We do, however, assume here that CALL_INSNs don't
1402 reference CC0. Guard against non-INSN's like CODE_LABEL. */
1404 if (HAVE_cc0 && (NONJUMP_INSN_P (insn) || JUMP_P (insn))
1405 && before_p
1406 && reg_referenced_p (cc0_rtx, PATTERN (insn)))
1407 chain = chain->prev, insn = chain->insn;
1409 new_chain = new_insn_chain ();
1410 if (before_p)
1412 rtx link;
1414 new_chain->prev = chain->prev;
1415 if (new_chain->prev != 0)
1416 new_chain->prev->next = new_chain;
1417 else
1418 reload_insn_chain = new_chain;
1420 chain->prev = new_chain;
1421 new_chain->next = chain;
1422 new_chain->insn = emit_insn_before (pat, insn);
1423 /* ??? It would be nice if we could exclude the already / still saved
1424 registers from the live sets. */
1425 COPY_REG_SET (&new_chain->live_throughout, &chain->live_throughout);
1426 note_uses (&PATTERN (chain->insn), add_used_regs,
1427 &new_chain->live_throughout);
1428 /* If CHAIN->INSN is a call, then the registers which contain
1429 the arguments to the function are live in the new insn. */
1430 if (CALL_P (chain->insn))
1431 for (link = CALL_INSN_FUNCTION_USAGE (chain->insn);
1432 link != NULL_RTX;
1433 link = XEXP (link, 1))
1434 note_uses (&XEXP (link, 0), add_used_regs,
1435 &new_chain->live_throughout);
1437 CLEAR_REG_SET (&new_chain->dead_or_set);
1438 if (chain->insn == BB_HEAD (BASIC_BLOCK_FOR_FN (cfun, chain->block)))
1439 BB_HEAD (BASIC_BLOCK_FOR_FN (cfun, chain->block)) = new_chain->insn;
1441 else
1443 new_chain->next = chain->next;
1444 if (new_chain->next != 0)
1445 new_chain->next->prev = new_chain;
1446 chain->next = new_chain;
1447 new_chain->prev = chain;
1448 new_chain->insn = emit_insn_after (pat, insn);
1449 /* ??? It would be nice if we could exclude the already / still saved
1450 registers from the live sets, and observe REG_UNUSED notes. */
1451 COPY_REG_SET (&new_chain->live_throughout, &chain->live_throughout);
1452 /* Registers that are set in CHAIN->INSN live in the new insn.
1453 (Unless there is a REG_UNUSED note for them, but we don't
1454 look for them here.) */
1455 note_stores (PATTERN (chain->insn), add_stored_regs,
1456 &new_chain->live_throughout);
1457 CLEAR_REG_SET (&new_chain->dead_or_set);
1458 if (chain->insn == BB_END (BASIC_BLOCK_FOR_FN (cfun, chain->block)))
1459 BB_END (BASIC_BLOCK_FOR_FN (cfun, chain->block)) = new_chain->insn;
1461 new_chain->block = chain->block;
1462 new_chain->is_caller_save_insn = 1;
1464 INSN_CODE (new_chain->insn) = code;
1465 return new_chain;
1467 #include "gt-caller-save.h"