Copy shadow argument conditionally (PR hsa/70337)
[official-gcc.git] / gcc / caller-save.c
blobcaf6486a5a80369f7973df053dc229373a16d578
1 /* Save and restore call-clobbered registers which are live across a call.
2 Copyright (C) 1989-2016 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 "backend.h"
24 #include "rtl.h"
25 #include "tree.h"
26 #include "predict.h"
27 #include "df.h"
28 #include "tm_p.h"
29 #include "insn-config.h"
30 #include "regs.h"
31 #include "emit-rtl.h"
32 #include "recog.h"
33 #include "reload.h"
34 #include "alias.h"
35 #include "addresses.h"
36 #include "dumpfile.h"
37 #include "rtl-iter.h"
39 #define MOVE_MAX_WORDS (MOVE_MAX / UNITS_PER_WORD)
41 #define regno_save_mode \
42 (this_target_reload->x_regno_save_mode)
43 #define cached_reg_save_code \
44 (this_target_reload->x_cached_reg_save_code)
45 #define cached_reg_restore_code \
46 (this_target_reload->x_cached_reg_restore_code)
48 /* For each hard register, a place on the stack where it can be saved,
49 if needed. */
51 static rtx
52 regno_save_mem[FIRST_PSEUDO_REGISTER][MAX_MOVE_MAX / MIN_UNITS_PER_WORD + 1];
54 /* The number of elements in the subsequent array. */
55 static int save_slots_num;
57 /* Allocated slots so far. */
58 static rtx save_slots[FIRST_PSEUDO_REGISTER];
60 /* Set of hard regs currently residing in save area (during insn scan). */
62 static HARD_REG_SET hard_regs_saved;
64 /* Number of registers currently in hard_regs_saved. */
66 static int n_regs_saved;
68 /* Computed by mark_referenced_regs, all regs referenced in a given
69 insn. */
70 static HARD_REG_SET referenced_regs;
73 typedef void refmarker_fn (rtx *loc, machine_mode mode, int hardregno,
74 void *mark_arg);
76 static int reg_save_code (int, machine_mode);
77 static int reg_restore_code (int, machine_mode);
79 struct saved_hard_reg;
80 static void initiate_saved_hard_regs (void);
81 static void new_saved_hard_reg (int, int);
82 static void finish_saved_hard_regs (void);
83 static int saved_hard_reg_compare_func (const void *, const void *);
85 static void mark_set_regs (rtx, const_rtx, void *);
86 static void mark_referenced_regs (rtx *, refmarker_fn *mark, void *mark_arg);
87 static refmarker_fn mark_reg_as_referenced;
88 static refmarker_fn replace_reg_with_saved_mem;
89 static int insert_save (struct insn_chain *, int, int, HARD_REG_SET *,
90 machine_mode *);
91 static int insert_restore (struct insn_chain *, int, int, int,
92 machine_mode *);
93 static struct insn_chain *insert_one_insn (struct insn_chain *, int, int,
94 rtx);
95 static void add_stored_regs (rtx, const_rtx, void *);
99 static GTY(()) rtx savepat;
100 static GTY(()) rtx restpat;
101 static GTY(()) rtx test_reg;
102 static GTY(()) rtx test_mem;
103 static GTY(()) rtx_insn *saveinsn;
104 static GTY(()) rtx_insn *restinsn;
106 /* Return the INSN_CODE used to save register REG in mode MODE. */
107 static int
108 reg_save_code (int reg, machine_mode mode)
110 bool ok;
111 if (cached_reg_save_code[reg][mode])
112 return cached_reg_save_code[reg][mode];
113 if (!HARD_REGNO_MODE_OK (reg, mode))
115 /* Depending on how HARD_REGNO_MODE_OK is defined, range propagation
116 might deduce here that reg >= FIRST_PSEUDO_REGISTER. So the assert
117 below silences a warning. */
118 gcc_assert (reg < FIRST_PSEUDO_REGISTER);
119 cached_reg_save_code[reg][mode] = -1;
120 cached_reg_restore_code[reg][mode] = -1;
121 return -1;
124 /* Update the register number and modes of the register
125 and memory operand. */
126 set_mode_and_regno (test_reg, mode, reg);
127 PUT_MODE (test_mem, mode);
129 /* Force re-recognition of the modified insns. */
130 INSN_CODE (saveinsn) = -1;
131 INSN_CODE (restinsn) = -1;
133 cached_reg_save_code[reg][mode] = recog_memoized (saveinsn);
134 cached_reg_restore_code[reg][mode] = recog_memoized (restinsn);
136 /* Now extract both insns and see if we can meet their
137 constraints. We don't know here whether the save and restore will
138 be in size- or speed-tuned code, so just use the set of enabled
139 alternatives. */
140 ok = (cached_reg_save_code[reg][mode] != -1
141 && cached_reg_restore_code[reg][mode] != -1);
142 if (ok)
144 extract_insn (saveinsn);
145 ok = constrain_operands (1, get_enabled_alternatives (saveinsn));
146 extract_insn (restinsn);
147 ok &= constrain_operands (1, get_enabled_alternatives (restinsn));
150 if (! ok)
152 cached_reg_save_code[reg][mode] = -1;
153 cached_reg_restore_code[reg][mode] = -1;
155 gcc_assert (cached_reg_save_code[reg][mode]);
156 return cached_reg_save_code[reg][mode];
159 /* Return the INSN_CODE used to restore register REG in mode MODE. */
160 static int
161 reg_restore_code (int reg, machine_mode mode)
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];
170 /* Initialize for caller-save.
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.
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. */
180 void
181 init_caller_save (void)
183 rtx addr_reg;
184 int offset;
185 rtx address;
186 int i, j;
188 if (caller_save_initialized_p)
189 return;
191 caller_save_initialized_p = true;
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. */
198 for (i = 0; i < FIRST_PSEUDO_REGISTER; i++)
200 if (call_used_regs[i]
201 && !TEST_HARD_REG_BIT (call_fixed_reg_set, i))
203 for (j = 1; j <= MOVE_MAX_WORDS; j++)
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)
209 SET_HARD_REG_BIT (call_fixed_reg_set, i);
213 else
214 regno_save_mode[i][1] = VOIDmode;
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.
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. */
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], ADDR_SPACE_GENERIC,
232 PLUS, CONST_INT)], i))
233 break;
235 gcc_assert (i < FIRST_PSEUDO_REGISTER);
237 addr_reg = gen_rtx_REG (Pmode, i);
239 for (offset = 1 << (HOST_BITS_PER_INT / 2); offset; offset >>= 1)
241 address = gen_rtx_PLUS (Pmode, addr_reg, gen_int_mode (offset, Pmode));
243 for (i = 0; i < FIRST_PSEUDO_REGISTER; i++)
244 if (regno_save_mode[i][1] != VOIDmode
245 && ! strict_memory_address_p (regno_save_mode[i][1], address))
246 break;
248 if (i == FIRST_PSEUDO_REGISTER)
249 break;
252 /* If we didn't find a valid address, we must use register indirect. */
253 if (offset == 0)
254 address = addr_reg;
256 /* Next we try to form an insn to save and restore the register. We
257 see if such an insn is recognized and meets its constraints.
259 To avoid lots of unnecessary RTL allocation, we construct all the RTL
260 once, then modify the memory and register operands in-place. */
262 test_reg = gen_rtx_REG (word_mode, LAST_VIRTUAL_REGISTER + 1);
263 test_mem = gen_rtx_MEM (word_mode, address);
264 savepat = gen_rtx_SET (test_mem, test_reg);
265 restpat = gen_rtx_SET (test_reg, test_mem);
267 saveinsn = gen_rtx_INSN (VOIDmode, 0, 0, 0, savepat, 0, -1, 0);
268 restinsn = gen_rtx_INSN (VOIDmode, 0, 0, 0, restpat, 0, -1, 0);
270 for (i = 0; i < FIRST_PSEUDO_REGISTER; i++)
271 for (j = 1; j <= MOVE_MAX_WORDS; j++)
272 if (reg_save_code (i,regno_save_mode[i][j]) == -1)
274 regno_save_mode[i][j] = VOIDmode;
275 if (j == 1)
277 SET_HARD_REG_BIT (call_fixed_reg_set, i);
278 if (call_used_regs[i])
279 SET_HARD_REG_BIT (no_caller_save_reg_set, i);
286 /* Initialize save areas by showing that we haven't allocated any yet. */
288 void
289 init_save_areas (void)
291 int i, j;
293 for (i = 0; i < FIRST_PSEUDO_REGISTER; i++)
294 for (j = 1; j <= MOVE_MAX_WORDS; j++)
295 regno_save_mem[i][j] = 0;
296 save_slots_num = 0;
300 /* The structure represents a hard register which should be saved
301 through the call. It is used when the integrated register
302 allocator (IRA) is used and sharing save slots is on. */
303 struct saved_hard_reg
305 /* Order number starting with 0. */
306 int num;
307 /* The hard regno. */
308 int hard_regno;
309 /* Execution frequency of all calls through which given hard
310 register should be saved. */
311 int call_freq;
312 /* Stack slot reserved to save the hard register through calls. */
313 rtx slot;
314 /* True if it is first hard register in the chain of hard registers
315 sharing the same stack slot. */
316 int first_p;
317 /* Order number of the next hard register structure with the same
318 slot in the chain. -1 represents end of the chain. */
319 int next;
322 /* Map: hard register number to the corresponding structure. */
323 static struct saved_hard_reg *hard_reg_map[FIRST_PSEUDO_REGISTER];
325 /* The number of all structures representing hard registers should be
326 saved, in order words, the number of used elements in the following
327 array. */
328 static int saved_regs_num;
330 /* Pointers to all the structures. Index is the order number of the
331 corresponding structure. */
332 static struct saved_hard_reg *all_saved_regs[FIRST_PSEUDO_REGISTER];
334 /* First called function for work with saved hard registers. */
335 static void
336 initiate_saved_hard_regs (void)
338 int i;
340 saved_regs_num = 0;
341 for (i = 0; i < FIRST_PSEUDO_REGISTER; i++)
342 hard_reg_map[i] = NULL;
345 /* Allocate and return new saved hard register with given REGNO and
346 CALL_FREQ. */
347 static void
348 new_saved_hard_reg (int regno, int call_freq)
350 struct saved_hard_reg *saved_reg;
352 saved_reg
353 = (struct saved_hard_reg *) xmalloc (sizeof (struct saved_hard_reg));
354 hard_reg_map[regno] = all_saved_regs[saved_regs_num] = saved_reg;
355 saved_reg->num = saved_regs_num++;
356 saved_reg->hard_regno = regno;
357 saved_reg->call_freq = call_freq;
358 saved_reg->first_p = FALSE;
359 saved_reg->next = -1;
362 /* Free memory allocated for the saved hard registers. */
363 static void
364 finish_saved_hard_regs (void)
366 int i;
368 for (i = 0; i < saved_regs_num; i++)
369 free (all_saved_regs[i]);
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)
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;
380 if (flag_omit_frame_pointer)
382 if (p1->call_freq - p2->call_freq != 0)
383 return p1->call_freq - p2->call_freq;
385 else if (p2->call_freq - p1->call_freq != 0)
386 return p2->call_freq - p1->call_freq;
388 return p1->num - p2->num;
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.
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.
401 Future work:
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).
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) */
413 void
414 setup_save_areas (void)
416 int i, j, k, freq;
417 HARD_REG_SET hard_regs_used;
418 struct saved_hard_reg *saved_reg;
419 rtx_insn *insn;
420 struct insn_chain *chain, *next;
421 unsigned int regno;
422 HARD_REG_SET hard_regs_to_save, used_regs, this_insn_sets;
423 reg_set_iterator rsi;
425 CLEAR_HARD_REG_SET (hard_regs_used);
427 /* Find every CALL_INSN and record which hard regs are live across the
428 call into HARD_REG_MAP and HARD_REGS_USED. */
429 initiate_saved_hard_regs ();
430 /* Create hard reg saved regs. */
431 for (chain = reload_insn_chain; chain != 0; chain = next)
433 rtx cheap;
435 insn = chain->insn;
436 next = chain->next;
437 if (!CALL_P (insn)
438 || find_reg_note (insn, REG_NORETURN, NULL))
439 continue;
440 freq = REG_FREQ_FROM_BB (BLOCK_FOR_INSN (insn));
441 REG_SET_TO_HARD_REG_SET (hard_regs_to_save,
442 &chain->live_throughout);
443 get_call_reg_set_usage (insn, &used_regs, call_used_reg_set);
445 /* Record all registers set in this call insn. These don't
446 need to be saved. N.B. the call insn might set a subreg
447 of a multi-hard-reg pseudo; then the pseudo is considered
448 live during the call, but the subreg that is set
449 isn't. */
450 CLEAR_HARD_REG_SET (this_insn_sets);
451 note_stores (PATTERN (insn), mark_set_regs, &this_insn_sets);
452 /* Sibcalls are considered to set the return value. */
453 if (SIBLING_CALL_P (insn) && crtl->return_rtx)
454 mark_set_regs (crtl->return_rtx, NULL_RTX, &this_insn_sets);
456 AND_COMPL_HARD_REG_SET (used_regs, call_fixed_reg_set);
457 AND_COMPL_HARD_REG_SET (used_regs, this_insn_sets);
458 AND_HARD_REG_SET (hard_regs_to_save, used_regs);
459 for (regno = 0; regno < FIRST_PSEUDO_REGISTER; regno++)
460 if (TEST_HARD_REG_BIT (hard_regs_to_save, regno))
462 if (hard_reg_map[regno] != NULL)
463 hard_reg_map[regno]->call_freq += freq;
464 else
465 new_saved_hard_reg (regno, freq);
466 SET_HARD_REG_BIT (hard_regs_used, regno);
468 cheap = find_reg_note (insn, REG_RETURNED, NULL);
469 if (cheap)
470 cheap = XEXP (cheap, 0);
471 /* Look through all live pseudos, mark their hard registers. */
472 EXECUTE_IF_SET_IN_REG_SET
473 (&chain->live_throughout, FIRST_PSEUDO_REGISTER, regno, rsi)
475 int r = reg_renumber[regno];
476 int bound;
478 if (r < 0 || regno_reg_rtx[regno] == cheap)
479 continue;
481 bound = r + hard_regno_nregs[r][PSEUDO_REGNO_MODE (regno)];
482 for (; r < bound; r++)
483 if (TEST_HARD_REG_BIT (used_regs, r))
485 if (hard_reg_map[r] != NULL)
486 hard_reg_map[r]->call_freq += freq;
487 else
488 new_saved_hard_reg (r, freq);
489 SET_HARD_REG_BIT (hard_regs_to_save, r);
490 SET_HARD_REG_BIT (hard_regs_used, r);
495 /* If requested, figure out which hard regs can share save slots. */
496 if (optimize && flag_ira_share_save_slots)
498 rtx slot;
499 char *saved_reg_conflicts;
500 int next_k;
501 struct saved_hard_reg *saved_reg2, *saved_reg3;
502 int call_saved_regs_num;
503 struct saved_hard_reg *call_saved_regs[FIRST_PSEUDO_REGISTER];
504 int best_slot_num;
505 int prev_save_slots_num;
506 rtx prev_save_slots[FIRST_PSEUDO_REGISTER];
508 /* Find saved hard register conflicts. */
509 saved_reg_conflicts = (char *) xmalloc (saved_regs_num * saved_regs_num);
510 memset (saved_reg_conflicts, 0, saved_regs_num * saved_regs_num);
511 for (chain = reload_insn_chain; chain != 0; chain = next)
513 rtx cheap;
514 call_saved_regs_num = 0;
515 insn = chain->insn;
516 next = chain->next;
517 if (!CALL_P (insn)
518 || find_reg_note (insn, REG_NORETURN, NULL))
519 continue;
521 cheap = find_reg_note (insn, REG_RETURNED, NULL);
522 if (cheap)
523 cheap = XEXP (cheap, 0);
525 REG_SET_TO_HARD_REG_SET (hard_regs_to_save,
526 &chain->live_throughout);
527 get_call_reg_set_usage (insn, &used_regs, call_used_reg_set);
529 /* Record all registers set in this call insn. These don't
530 need to be saved. N.B. the call insn might set a subreg
531 of a multi-hard-reg pseudo; then the pseudo is considered
532 live during the call, but the subreg that is set
533 isn't. */
534 CLEAR_HARD_REG_SET (this_insn_sets);
535 note_stores (PATTERN (insn), mark_set_regs, &this_insn_sets);
536 /* Sibcalls are considered to set the return value,
537 compare df-scan.c:df_get_call_refs. */
538 if (SIBLING_CALL_P (insn) && crtl->return_rtx)
539 mark_set_regs (crtl->return_rtx, NULL_RTX, &this_insn_sets);
541 AND_COMPL_HARD_REG_SET (used_regs, call_fixed_reg_set);
542 AND_COMPL_HARD_REG_SET (used_regs, this_insn_sets);
543 AND_HARD_REG_SET (hard_regs_to_save, used_regs);
544 for (regno = 0; regno < FIRST_PSEUDO_REGISTER; regno++)
545 if (TEST_HARD_REG_BIT (hard_regs_to_save, regno))
547 gcc_assert (hard_reg_map[regno] != NULL);
548 call_saved_regs[call_saved_regs_num++] = hard_reg_map[regno];
550 /* Look through all live pseudos, mark their hard registers. */
551 EXECUTE_IF_SET_IN_REG_SET
552 (&chain->live_throughout, FIRST_PSEUDO_REGISTER, regno, rsi)
554 int r = reg_renumber[regno];
555 int bound;
557 if (r < 0 || regno_reg_rtx[regno] == cheap)
558 continue;
560 bound = r + hard_regno_nregs[r][PSEUDO_REGNO_MODE (regno)];
561 for (; r < bound; r++)
562 if (TEST_HARD_REG_BIT (used_regs, r))
563 call_saved_regs[call_saved_regs_num++] = hard_reg_map[r];
565 for (i = 0; i < call_saved_regs_num; i++)
567 saved_reg = call_saved_regs[i];
568 for (j = 0; j < call_saved_regs_num; j++)
569 if (i != j)
571 saved_reg2 = call_saved_regs[j];
572 saved_reg_conflicts[saved_reg->num * saved_regs_num
573 + saved_reg2->num]
574 = saved_reg_conflicts[saved_reg2->num * saved_regs_num
575 + saved_reg->num]
576 = TRUE;
580 /* Sort saved hard regs. */
581 qsort (all_saved_regs, saved_regs_num, sizeof (struct saved_hard_reg *),
582 saved_hard_reg_compare_func);
583 /* Initiate slots available from the previous reload
584 iteration. */
585 prev_save_slots_num = save_slots_num;
586 memcpy (prev_save_slots, save_slots, save_slots_num * sizeof (rtx));
587 save_slots_num = 0;
588 /* Allocate stack slots for the saved hard registers. */
589 for (i = 0; i < saved_regs_num; i++)
591 saved_reg = all_saved_regs[i];
592 regno = saved_reg->hard_regno;
593 for (j = 0; j < i; j++)
595 saved_reg2 = all_saved_regs[j];
596 if (! saved_reg2->first_p)
597 continue;
598 slot = saved_reg2->slot;
599 for (k = j; k >= 0; k = next_k)
601 saved_reg3 = all_saved_regs[k];
602 next_k = saved_reg3->next;
603 if (saved_reg_conflicts[saved_reg->num * saved_regs_num
604 + saved_reg3->num])
605 break;
607 if (k < 0
608 && (GET_MODE_SIZE (regno_save_mode[regno][1])
609 <= GET_MODE_SIZE (regno_save_mode
610 [saved_reg2->hard_regno][1])))
612 saved_reg->slot
613 = adjust_address_nv
614 (slot, regno_save_mode[saved_reg->hard_regno][1], 0);
615 regno_save_mem[regno][1] = saved_reg->slot;
616 saved_reg->next = saved_reg2->next;
617 saved_reg2->next = i;
618 if (dump_file != NULL)
619 fprintf (dump_file, "%d uses slot of %d\n",
620 regno, saved_reg2->hard_regno);
621 break;
624 if (j == i)
626 saved_reg->first_p = TRUE;
627 for (best_slot_num = -1, j = 0; j < prev_save_slots_num; j++)
629 slot = prev_save_slots[j];
630 if (slot == NULL_RTX)
631 continue;
632 if (GET_MODE_SIZE (regno_save_mode[regno][1])
633 <= GET_MODE_SIZE (GET_MODE (slot))
634 && best_slot_num < 0)
635 best_slot_num = j;
636 if (GET_MODE (slot) == regno_save_mode[regno][1])
637 break;
639 if (best_slot_num >= 0)
641 saved_reg->slot = prev_save_slots[best_slot_num];
642 saved_reg->slot
643 = adjust_address_nv
644 (saved_reg->slot,
645 regno_save_mode[saved_reg->hard_regno][1], 0);
646 if (dump_file != NULL)
647 fprintf (dump_file,
648 "%d uses a slot from prev iteration\n", regno);
649 prev_save_slots[best_slot_num] = NULL_RTX;
650 if (best_slot_num + 1 == prev_save_slots_num)
651 prev_save_slots_num--;
653 else
655 saved_reg->slot
656 = assign_stack_local_1
657 (regno_save_mode[regno][1],
658 GET_MODE_SIZE (regno_save_mode[regno][1]), 0,
659 ASLK_REDUCE_ALIGN);
660 if (dump_file != NULL)
661 fprintf (dump_file, "%d uses a new slot\n", regno);
663 regno_save_mem[regno][1] = saved_reg->slot;
664 save_slots[save_slots_num++] = saved_reg->slot;
667 free (saved_reg_conflicts);
668 finish_saved_hard_regs ();
670 else
672 /* We are not sharing slots.
674 Run through all the call-used hard-registers and allocate
675 space for each in the caller-save area. Try to allocate space
676 in a manner which allows multi-register saves/restores to be done. */
678 for (i = 0; i < FIRST_PSEUDO_REGISTER; i++)
679 for (j = MOVE_MAX_WORDS; j > 0; j--)
681 int do_save = 1;
683 /* If no mode exists for this size, try another. Also break out
684 if we have already saved this hard register. */
685 if (regno_save_mode[i][j] == VOIDmode || regno_save_mem[i][1] != 0)
686 continue;
688 /* See if any register in this group has been saved. */
689 for (k = 0; k < j; k++)
690 if (regno_save_mem[i + k][1])
692 do_save = 0;
693 break;
695 if (! do_save)
696 continue;
698 for (k = 0; k < j; k++)
699 if (! TEST_HARD_REG_BIT (hard_regs_used, i + k))
701 do_save = 0;
702 break;
704 if (! do_save)
705 continue;
707 /* We have found an acceptable mode to store in. Since
708 hard register is always saved in the widest mode
709 available, the mode may be wider than necessary, it is
710 OK to reduce the alignment of spill space. We will
711 verify that it is equal to or greater than required
712 when we restore and save the hard register in
713 insert_restore and insert_save. */
714 regno_save_mem[i][j]
715 = assign_stack_local_1 (regno_save_mode[i][j],
716 GET_MODE_SIZE (regno_save_mode[i][j]),
717 0, ASLK_REDUCE_ALIGN);
719 /* Setup single word save area just in case... */
720 for (k = 0; k < j; k++)
721 /* This should not depend on WORDS_BIG_ENDIAN.
722 The order of words in regs is the same as in memory. */
723 regno_save_mem[i + k][1]
724 = adjust_address_nv (regno_save_mem[i][j],
725 regno_save_mode[i + k][1],
726 k * UNITS_PER_WORD);
730 /* Now loop again and set the alias set of any save areas we made to
731 the alias set used to represent frame objects. */
732 for (i = 0; i < FIRST_PSEUDO_REGISTER; i++)
733 for (j = MOVE_MAX_WORDS; j > 0; j--)
734 if (regno_save_mem[i][j] != 0)
735 set_mem_alias_set (regno_save_mem[i][j], get_frame_alias_set ());
740 /* Find the places where hard regs are live across calls and save them. */
742 void
743 save_call_clobbered_regs (void)
745 struct insn_chain *chain, *next, *last = NULL;
746 machine_mode save_mode [FIRST_PSEUDO_REGISTER];
748 /* Computed in mark_set_regs, holds all registers set by the current
749 instruction. */
750 HARD_REG_SET this_insn_sets;
752 CLEAR_HARD_REG_SET (hard_regs_saved);
753 n_regs_saved = 0;
755 for (chain = reload_insn_chain; chain != 0; chain = next)
757 rtx_insn *insn = chain->insn;
758 enum rtx_code code = GET_CODE (insn);
760 next = chain->next;
762 gcc_assert (!chain->is_caller_save_insn);
764 if (NONDEBUG_INSN_P (insn))
766 /* If some registers have been saved, see if INSN references
767 any of them. We must restore them before the insn if so. */
769 if (n_regs_saved)
771 int regno;
772 HARD_REG_SET this_insn_sets;
774 if (code == JUMP_INSN)
775 /* Restore all registers if this is a JUMP_INSN. */
776 COPY_HARD_REG_SET (referenced_regs, hard_regs_saved);
777 else
779 CLEAR_HARD_REG_SET (referenced_regs);
780 mark_referenced_regs (&PATTERN (insn),
781 mark_reg_as_referenced, NULL);
782 AND_HARD_REG_SET (referenced_regs, hard_regs_saved);
785 for (regno = 0; regno < FIRST_PSEUDO_REGISTER; regno++)
786 if (TEST_HARD_REG_BIT (referenced_regs, regno))
787 regno += insert_restore (chain, 1, regno, MOVE_MAX_WORDS,
788 save_mode);
789 /* If a saved register is set after the call, this means we no
790 longer should restore it. This can happen when parts of a
791 multi-word pseudo do not conflict with other pseudos, so
792 IRA may allocate the same hard register for both. One may
793 be live across the call, while the other is set
794 afterwards. */
795 CLEAR_HARD_REG_SET (this_insn_sets);
796 note_stores (PATTERN (insn), mark_set_regs, &this_insn_sets);
797 AND_COMPL_HARD_REG_SET (hard_regs_saved, this_insn_sets);
800 if (code == CALL_INSN
801 && ! SIBLING_CALL_P (insn)
802 && ! find_reg_note (insn, REG_NORETURN, NULL))
804 unsigned regno;
805 HARD_REG_SET hard_regs_to_save;
806 HARD_REG_SET call_def_reg_set;
807 reg_set_iterator rsi;
808 rtx cheap;
810 cheap = find_reg_note (insn, REG_RETURNED, NULL);
811 if (cheap)
812 cheap = XEXP (cheap, 0);
814 /* Use the register life information in CHAIN to compute which
815 regs are live during the call. */
816 REG_SET_TO_HARD_REG_SET (hard_regs_to_save,
817 &chain->live_throughout);
818 /* Save hard registers always in the widest mode available. */
819 for (regno = 0; regno < FIRST_PSEUDO_REGISTER; regno++)
820 if (TEST_HARD_REG_BIT (hard_regs_to_save, regno))
821 save_mode [regno] = regno_save_mode [regno][1];
822 else
823 save_mode [regno] = VOIDmode;
825 /* Look through all live pseudos, mark their hard registers
826 and choose proper mode for saving. */
827 EXECUTE_IF_SET_IN_REG_SET
828 (&chain->live_throughout, FIRST_PSEUDO_REGISTER, regno, rsi)
830 int r = reg_renumber[regno];
831 int nregs;
832 machine_mode mode;
834 if (r < 0 || regno_reg_rtx[regno] == cheap)
835 continue;
836 nregs = hard_regno_nregs[r][PSEUDO_REGNO_MODE (regno)];
837 mode = HARD_REGNO_CALLER_SAVE_MODE
838 (r, nregs, PSEUDO_REGNO_MODE (regno));
839 if (GET_MODE_BITSIZE (mode)
840 > GET_MODE_BITSIZE (save_mode[r]))
841 save_mode[r] = mode;
842 while (nregs-- > 0)
843 SET_HARD_REG_BIT (hard_regs_to_save, r + nregs);
846 /* Record all registers set in this call insn. These don't need
847 to be saved. N.B. the call insn might set a subreg of a
848 multi-hard-reg pseudo; then the pseudo is considered live
849 during the call, but the subreg that is set isn't. */
850 CLEAR_HARD_REG_SET (this_insn_sets);
851 note_stores (PATTERN (insn), mark_set_regs, &this_insn_sets);
853 /* Compute which hard regs must be saved before this call. */
854 AND_COMPL_HARD_REG_SET (hard_regs_to_save, call_fixed_reg_set);
855 AND_COMPL_HARD_REG_SET (hard_regs_to_save, this_insn_sets);
856 AND_COMPL_HARD_REG_SET (hard_regs_to_save, hard_regs_saved);
857 get_call_reg_set_usage (insn, &call_def_reg_set,
858 call_used_reg_set);
859 AND_HARD_REG_SET (hard_regs_to_save, call_def_reg_set);
861 for (regno = 0; regno < FIRST_PSEUDO_REGISTER; regno++)
862 if (TEST_HARD_REG_BIT (hard_regs_to_save, regno))
863 regno += insert_save (chain, 1, regno, &hard_regs_to_save, save_mode);
865 /* Must recompute n_regs_saved. */
866 n_regs_saved = 0;
867 for (regno = 0; regno < FIRST_PSEUDO_REGISTER; regno++)
868 if (TEST_HARD_REG_BIT (hard_regs_saved, regno))
869 n_regs_saved++;
871 if (cheap
872 && HARD_REGISTER_P (cheap)
873 && TEST_HARD_REG_BIT (call_used_reg_set, REGNO (cheap)))
875 rtx dest, newpat;
876 rtx pat = PATTERN (insn);
877 if (GET_CODE (pat) == PARALLEL)
878 pat = XVECEXP (pat, 0, 0);
879 dest = SET_DEST (pat);
880 /* For multiple return values dest is PARALLEL.
881 Currently we handle only single return value case. */
882 if (REG_P (dest))
884 newpat = gen_rtx_SET (cheap, copy_rtx (dest));
885 chain = insert_one_insn (chain, 0, -1, newpat);
889 last = chain;
891 else if (DEBUG_INSN_P (insn) && n_regs_saved)
892 mark_referenced_regs (&PATTERN (insn),
893 replace_reg_with_saved_mem,
894 save_mode);
896 if (chain->next == 0 || chain->next->block != chain->block)
898 int regno;
899 /* At the end of the basic block, we must restore any registers that
900 remain saved. If the last insn in the block is a JUMP_INSN, put
901 the restore before the insn, otherwise, put it after the insn. */
903 if (n_regs_saved
904 && DEBUG_INSN_P (insn)
905 && last
906 && last->block == chain->block)
908 rtx_insn *ins, *prev;
909 basic_block bb = BLOCK_FOR_INSN (insn);
911 /* When adding hard reg restores after a DEBUG_INSN, move
912 all notes between last real insn and this DEBUG_INSN after
913 the DEBUG_INSN, otherwise we could get code
914 -g/-g0 differences. */
915 for (ins = PREV_INSN (insn); ins != last->insn; ins = prev)
917 prev = PREV_INSN (ins);
918 if (NOTE_P (ins))
920 SET_NEXT_INSN (prev) = NEXT_INSN (ins);
921 SET_PREV_INSN (NEXT_INSN (ins)) = prev;
922 SET_PREV_INSN (ins) = insn;
923 SET_NEXT_INSN (ins) = NEXT_INSN (insn);
924 SET_NEXT_INSN (insn) = ins;
925 if (NEXT_INSN (ins))
926 SET_PREV_INSN (NEXT_INSN (ins)) = ins;
927 if (BB_END (bb) == insn)
928 BB_END (bb) = ins;
930 else
931 gcc_assert (DEBUG_INSN_P (ins));
934 last = NULL;
936 if (n_regs_saved)
937 for (regno = 0; regno < FIRST_PSEUDO_REGISTER; regno++)
938 if (TEST_HARD_REG_BIT (hard_regs_saved, regno))
939 regno += insert_restore (chain, JUMP_P (insn),
940 regno, MOVE_MAX_WORDS, save_mode);
945 /* Here from note_stores, or directly from save_call_clobbered_regs, when
946 an insn stores a value in a register.
947 Set the proper bit or bits in this_insn_sets. All pseudos that have
948 been assigned hard regs have had their register number changed already,
949 so we can ignore pseudos. */
950 static void
951 mark_set_regs (rtx reg, const_rtx setter ATTRIBUTE_UNUSED, void *data)
953 int regno, endregno, i;
954 HARD_REG_SET *this_insn_sets = (HARD_REG_SET *) data;
956 if (GET_CODE (reg) == SUBREG)
958 rtx inner = SUBREG_REG (reg);
959 if (!REG_P (inner) || REGNO (inner) >= FIRST_PSEUDO_REGISTER)
960 return;
961 regno = subreg_regno (reg);
962 endregno = regno + subreg_nregs (reg);
964 else if (REG_P (reg)
965 && REGNO (reg) < FIRST_PSEUDO_REGISTER)
967 regno = REGNO (reg);
968 endregno = END_REGNO (reg);
970 else
971 return;
973 for (i = regno; i < endregno; i++)
974 SET_HARD_REG_BIT (*this_insn_sets, i);
977 /* Here from note_stores when an insn stores a value in a register.
978 Set the proper bit or bits in the passed regset. All pseudos that have
979 been assigned hard regs have had their register number changed already,
980 so we can ignore pseudos. */
981 static void
982 add_stored_regs (rtx reg, const_rtx setter, void *data)
984 int regno, endregno, i;
985 machine_mode mode = GET_MODE (reg);
986 int offset = 0;
988 if (GET_CODE (setter) == CLOBBER)
989 return;
991 if (GET_CODE (reg) == SUBREG
992 && REG_P (SUBREG_REG (reg))
993 && REGNO (SUBREG_REG (reg)) < FIRST_PSEUDO_REGISTER)
995 offset = subreg_regno_offset (REGNO (SUBREG_REG (reg)),
996 GET_MODE (SUBREG_REG (reg)),
997 SUBREG_BYTE (reg),
998 GET_MODE (reg));
999 regno = REGNO (SUBREG_REG (reg)) + offset;
1000 endregno = regno + subreg_nregs (reg);
1002 else
1004 if (!REG_P (reg) || REGNO (reg) >= FIRST_PSEUDO_REGISTER)
1005 return;
1007 regno = REGNO (reg) + offset;
1008 endregno = end_hard_regno (mode, regno);
1011 for (i = regno; i < endregno; i++)
1012 SET_REGNO_REG_SET ((regset) data, i);
1015 /* Walk X and record all referenced registers in REFERENCED_REGS. */
1016 static void
1017 mark_referenced_regs (rtx *loc, refmarker_fn *mark, void *arg)
1019 enum rtx_code code = GET_CODE (*loc);
1020 const char *fmt;
1021 int i, j;
1023 if (code == SET)
1024 mark_referenced_regs (&SET_SRC (*loc), mark, arg);
1025 if (code == SET || code == CLOBBER)
1027 loc = &SET_DEST (*loc);
1028 code = GET_CODE (*loc);
1029 if ((code == REG && REGNO (*loc) < FIRST_PSEUDO_REGISTER)
1030 || code == PC || code == CC0
1031 || (code == SUBREG && REG_P (SUBREG_REG (*loc))
1032 && REGNO (SUBREG_REG (*loc)) < FIRST_PSEUDO_REGISTER
1033 /* If we're setting only part of a multi-word register,
1034 we shall mark it as referenced, because the words
1035 that are not being set should be restored. */
1036 && ((GET_MODE_SIZE (GET_MODE (*loc))
1037 >= GET_MODE_SIZE (GET_MODE (SUBREG_REG (*loc))))
1038 || (GET_MODE_SIZE (GET_MODE (SUBREG_REG (*loc)))
1039 <= UNITS_PER_WORD))))
1040 return;
1042 if (code == MEM || code == SUBREG)
1044 loc = &XEXP (*loc, 0);
1045 code = GET_CODE (*loc);
1048 if (code == REG)
1050 int regno = REGNO (*loc);
1051 int hardregno = (regno < FIRST_PSEUDO_REGISTER ? regno
1052 : reg_renumber[regno]);
1054 if (hardregno >= 0)
1055 mark (loc, GET_MODE (*loc), hardregno, arg);
1056 else if (arg)
1057 /* ??? Will we ever end up with an equiv expression in a debug
1058 insn, that would have required restoring a reg, or will
1059 reload take care of it for us? */
1060 return;
1061 /* If this is a pseudo that did not get a hard register, scan its
1062 memory location, since it might involve the use of another
1063 register, which might be saved. */
1064 else if (reg_equiv_mem (regno) != 0)
1065 mark_referenced_regs (&XEXP (reg_equiv_mem (regno), 0), mark, arg);
1066 else if (reg_equiv_address (regno) != 0)
1067 mark_referenced_regs (&reg_equiv_address (regno), mark, arg);
1068 return;
1071 fmt = GET_RTX_FORMAT (code);
1072 for (i = GET_RTX_LENGTH (code) - 1; i >= 0; i--)
1074 if (fmt[i] == 'e')
1075 mark_referenced_regs (&XEXP (*loc, i), mark, arg);
1076 else if (fmt[i] == 'E')
1077 for (j = XVECLEN (*loc, i) - 1; j >= 0; j--)
1078 mark_referenced_regs (&XVECEXP (*loc, i, j), mark, arg);
1082 /* Parameter function for mark_referenced_regs() that adds registers
1083 present in the insn and in equivalent mems and addresses to
1084 referenced_regs. */
1086 static void
1087 mark_reg_as_referenced (rtx *loc ATTRIBUTE_UNUSED,
1088 machine_mode mode,
1089 int hardregno,
1090 void *arg ATTRIBUTE_UNUSED)
1092 add_to_hard_reg_set (&referenced_regs, mode, hardregno);
1095 /* Parameter function for mark_referenced_regs() that replaces
1096 registers referenced in a debug_insn that would have been restored,
1097 should it be a non-debug_insn, with their save locations. */
1099 static void
1100 replace_reg_with_saved_mem (rtx *loc,
1101 machine_mode mode,
1102 int regno,
1103 void *arg)
1105 unsigned int i, nregs = hard_regno_nregs [regno][mode];
1106 rtx mem;
1107 machine_mode *save_mode = (machine_mode *)arg;
1109 for (i = 0; i < nregs; i++)
1110 if (TEST_HARD_REG_BIT (hard_regs_saved, regno + i))
1111 break;
1113 /* If none of the registers in the range would need restoring, we're
1114 all set. */
1115 if (i == nregs)
1116 return;
1118 while (++i < nregs)
1119 if (!TEST_HARD_REG_BIT (hard_regs_saved, regno + i))
1120 break;
1122 if (i == nregs
1123 && regno_save_mem[regno][nregs])
1125 mem = copy_rtx (regno_save_mem[regno][nregs]);
1127 if (nregs == (unsigned int) hard_regno_nregs[regno][save_mode[regno]])
1128 mem = adjust_address_nv (mem, save_mode[regno], 0);
1130 if (GET_MODE (mem) != mode)
1132 /* This is gen_lowpart_if_possible(), but without validating
1133 the newly-formed address. */
1134 int offset = 0;
1136 if (WORDS_BIG_ENDIAN)
1137 offset = (MAX (GET_MODE_SIZE (GET_MODE (mem)), UNITS_PER_WORD)
1138 - MAX (GET_MODE_SIZE (mode), UNITS_PER_WORD));
1139 if (BYTES_BIG_ENDIAN)
1140 /* Adjust the address so that the address-after-the-data is
1141 unchanged. */
1142 offset -= (MIN (UNITS_PER_WORD, GET_MODE_SIZE (mode))
1143 - MIN (UNITS_PER_WORD, GET_MODE_SIZE (GET_MODE (mem))));
1145 mem = adjust_address_nv (mem, mode, offset);
1148 else
1150 mem = gen_rtx_CONCATN (mode, rtvec_alloc (nregs));
1151 for (i = 0; i < nregs; i++)
1152 if (TEST_HARD_REG_BIT (hard_regs_saved, regno + i))
1154 gcc_assert (regno_save_mem[regno + i][1]);
1155 XVECEXP (mem, 0, i) = copy_rtx (regno_save_mem[regno + i][1]);
1157 else
1159 machine_mode smode = save_mode[regno];
1160 gcc_assert (smode != VOIDmode);
1161 if (hard_regno_nregs [regno][smode] > 1)
1162 smode = mode_for_size (GET_MODE_SIZE (mode) / nregs,
1163 GET_MODE_CLASS (mode), 0);
1164 XVECEXP (mem, 0, i) = gen_rtx_REG (smode, regno + i);
1168 gcc_assert (GET_MODE (mem) == mode);
1169 *loc = mem;
1173 /* Insert a sequence of insns to restore. Place these insns in front of
1174 CHAIN if BEFORE_P is nonzero, behind the insn otherwise. MAXRESTORE is
1175 the maximum number of registers which should be restored during this call.
1176 It should never be less than 1 since we only work with entire registers.
1178 Note that we have verified in init_caller_save that we can do this
1179 with a simple SET, so use it. Set INSN_CODE to what we save there
1180 since the address might not be valid so the insn might not be recognized.
1181 These insns will be reloaded and have register elimination done by
1182 find_reload, so we need not worry about that here.
1184 Return the extra number of registers saved. */
1186 static int
1187 insert_restore (struct insn_chain *chain, int before_p, int regno,
1188 int maxrestore, machine_mode *save_mode)
1190 int i, k;
1191 rtx pat = NULL_RTX;
1192 int code;
1193 unsigned int numregs = 0;
1194 struct insn_chain *new_chain;
1195 rtx mem;
1197 /* A common failure mode if register status is not correct in the
1198 RTL is for this routine to be called with a REGNO we didn't
1199 expect to save. That will cause us to write an insn with a (nil)
1200 SET_DEST or SET_SRC. Instead of doing so and causing a crash
1201 later, check for this common case here instead. This will remove
1202 one step in debugging such problems. */
1203 gcc_assert (regno_save_mem[regno][1]);
1205 /* Get the pattern to emit and update our status.
1207 See if we can restore `maxrestore' registers at once. Work
1208 backwards to the single register case. */
1209 for (i = maxrestore; i > 0; i--)
1211 int j;
1212 int ok = 1;
1214 if (regno_save_mem[regno][i] == 0)
1215 continue;
1217 for (j = 0; j < i; j++)
1218 if (! TEST_HARD_REG_BIT (hard_regs_saved, regno + j))
1220 ok = 0;
1221 break;
1223 /* Must do this one restore at a time. */
1224 if (! ok)
1225 continue;
1227 numregs = i;
1228 break;
1231 mem = regno_save_mem [regno][numregs];
1232 if (save_mode [regno] != VOIDmode
1233 && save_mode [regno] != GET_MODE (mem)
1234 && numregs == (unsigned int) hard_regno_nregs[regno][save_mode [regno]]
1235 /* Check that insn to restore REGNO in save_mode[regno] is
1236 correct. */
1237 && reg_save_code (regno, save_mode[regno]) >= 0)
1238 mem = adjust_address_nv (mem, save_mode[regno], 0);
1239 else
1240 mem = copy_rtx (mem);
1242 /* Verify that the alignment of spill space is equal to or greater
1243 than required. */
1244 gcc_assert (MIN (MAX_SUPPORTED_STACK_ALIGNMENT,
1245 GET_MODE_ALIGNMENT (GET_MODE (mem))) <= MEM_ALIGN (mem));
1247 pat = gen_rtx_SET (gen_rtx_REG (GET_MODE (mem), regno), mem);
1248 code = reg_restore_code (regno, GET_MODE (mem));
1249 new_chain = insert_one_insn (chain, before_p, code, pat);
1251 /* Clear status for all registers we restored. */
1252 for (k = 0; k < i; k++)
1254 CLEAR_HARD_REG_BIT (hard_regs_saved, regno + k);
1255 SET_REGNO_REG_SET (&new_chain->dead_or_set, regno + k);
1256 n_regs_saved--;
1259 /* Tell our callers how many extra registers we saved/restored. */
1260 return numregs - 1;
1263 /* Like insert_restore above, but save registers instead. */
1265 static int
1266 insert_save (struct insn_chain *chain, int before_p, int regno,
1267 HARD_REG_SET (*to_save), machine_mode *save_mode)
1269 int i;
1270 unsigned int k;
1271 rtx pat = NULL_RTX;
1272 int code;
1273 unsigned int numregs = 0;
1274 struct insn_chain *new_chain;
1275 rtx mem;
1277 /* A common failure mode if register status is not correct in the
1278 RTL is for this routine to be called with a REGNO we didn't
1279 expect to save. That will cause us to write an insn with a (nil)
1280 SET_DEST or SET_SRC. Instead of doing so and causing a crash
1281 later, check for this common case here. This will remove one
1282 step in debugging such problems. */
1283 gcc_assert (regno_save_mem[regno][1]);
1285 /* Get the pattern to emit and update our status.
1287 See if we can save several registers with a single instruction.
1288 Work backwards to the single register case. */
1289 for (i = MOVE_MAX_WORDS; i > 0; i--)
1291 int j;
1292 int ok = 1;
1293 if (regno_save_mem[regno][i] == 0)
1294 continue;
1296 for (j = 0; j < i; j++)
1297 if (! TEST_HARD_REG_BIT (*to_save, regno + j))
1299 ok = 0;
1300 break;
1302 /* Must do this one save at a time. */
1303 if (! ok)
1304 continue;
1306 numregs = i;
1307 break;
1310 mem = regno_save_mem [regno][numregs];
1311 if (save_mode [regno] != VOIDmode
1312 && save_mode [regno] != GET_MODE (mem)
1313 && numregs == (unsigned int) hard_regno_nregs[regno][save_mode [regno]]
1314 /* Check that insn to save REGNO in save_mode[regno] is
1315 correct. */
1316 && reg_save_code (regno, save_mode[regno]) >= 0)
1317 mem = adjust_address_nv (mem, save_mode[regno], 0);
1318 else
1319 mem = copy_rtx (mem);
1321 /* Verify that the alignment of spill space is equal to or greater
1322 than required. */
1323 gcc_assert (MIN (MAX_SUPPORTED_STACK_ALIGNMENT,
1324 GET_MODE_ALIGNMENT (GET_MODE (mem))) <= MEM_ALIGN (mem));
1326 pat = gen_rtx_SET (mem, gen_rtx_REG (GET_MODE (mem), regno));
1327 code = reg_save_code (regno, GET_MODE (mem));
1328 new_chain = insert_one_insn (chain, before_p, code, pat);
1330 /* Set hard_regs_saved and dead_or_set for all the registers we saved. */
1331 for (k = 0; k < numregs; k++)
1333 SET_HARD_REG_BIT (hard_regs_saved, regno + k);
1334 SET_REGNO_REG_SET (&new_chain->dead_or_set, regno + k);
1335 n_regs_saved++;
1338 /* Tell our callers how many extra registers we saved/restored. */
1339 return numregs - 1;
1342 /* A note_uses callback used by insert_one_insn. Add the hard-register
1343 equivalent of each REG to regset DATA. */
1345 static void
1346 add_used_regs (rtx *loc, void *data)
1348 subrtx_iterator::array_type array;
1349 FOR_EACH_SUBRTX (iter, array, *loc, NONCONST)
1351 const_rtx x = *iter;
1352 if (REG_P (x))
1354 unsigned int regno = REGNO (x);
1355 if (HARD_REGISTER_NUM_P (regno))
1356 bitmap_set_range ((regset) data, regno,
1357 hard_regno_nregs[regno][GET_MODE (x)]);
1358 else
1359 gcc_checking_assert (reg_renumber[regno] < 0);
1364 /* Emit a new caller-save insn and set the code. */
1365 static struct insn_chain *
1366 insert_one_insn (struct insn_chain *chain, int before_p, int code, rtx pat)
1368 rtx_insn *insn = chain->insn;
1369 struct insn_chain *new_chain;
1371 /* If INSN references CC0, put our insns in front of the insn that sets
1372 CC0. This is always safe, since the only way we could be passed an
1373 insn that references CC0 is for a restore, and doing a restore earlier
1374 isn't a problem. We do, however, assume here that CALL_INSNs don't
1375 reference CC0. Guard against non-INSN's like CODE_LABEL. */
1377 if (HAVE_cc0 && (NONJUMP_INSN_P (insn) || JUMP_P (insn))
1378 && before_p
1379 && reg_referenced_p (cc0_rtx, PATTERN (insn)))
1380 chain = chain->prev, insn = chain->insn;
1382 new_chain = new_insn_chain ();
1383 if (before_p)
1385 rtx link;
1387 new_chain->prev = chain->prev;
1388 if (new_chain->prev != 0)
1389 new_chain->prev->next = new_chain;
1390 else
1391 reload_insn_chain = new_chain;
1393 chain->prev = new_chain;
1394 new_chain->next = chain;
1395 new_chain->insn = emit_insn_before (pat, insn);
1396 /* ??? It would be nice if we could exclude the already / still saved
1397 registers from the live sets. */
1398 COPY_REG_SET (&new_chain->live_throughout, &chain->live_throughout);
1399 note_uses (&PATTERN (chain->insn), add_used_regs,
1400 &new_chain->live_throughout);
1401 /* If CHAIN->INSN is a call, then the registers which contain
1402 the arguments to the function are live in the new insn. */
1403 if (CALL_P (chain->insn))
1404 for (link = CALL_INSN_FUNCTION_USAGE (chain->insn);
1405 link != NULL_RTX;
1406 link = XEXP (link, 1))
1407 note_uses (&XEXP (link, 0), add_used_regs,
1408 &new_chain->live_throughout);
1410 CLEAR_REG_SET (&new_chain->dead_or_set);
1411 if (chain->insn == BB_HEAD (BASIC_BLOCK_FOR_FN (cfun, chain->block)))
1412 BB_HEAD (BASIC_BLOCK_FOR_FN (cfun, chain->block)) = new_chain->insn;
1414 else
1416 new_chain->next = chain->next;
1417 if (new_chain->next != 0)
1418 new_chain->next->prev = new_chain;
1419 chain->next = new_chain;
1420 new_chain->prev = chain;
1421 new_chain->insn = emit_insn_after (pat, insn);
1422 /* ??? It would be nice if we could exclude the already / still saved
1423 registers from the live sets, and observe REG_UNUSED notes. */
1424 COPY_REG_SET (&new_chain->live_throughout, &chain->live_throughout);
1425 /* Registers that are set in CHAIN->INSN live in the new insn.
1426 (Unless there is a REG_UNUSED note for them, but we don't
1427 look for them here.) */
1428 note_stores (PATTERN (chain->insn), add_stored_regs,
1429 &new_chain->live_throughout);
1430 CLEAR_REG_SET (&new_chain->dead_or_set);
1431 if (chain->insn == BB_END (BASIC_BLOCK_FOR_FN (cfun, chain->block)))
1432 BB_END (BASIC_BLOCK_FOR_FN (cfun, chain->block)) = new_chain->insn;
1434 new_chain->block = chain->block;
1435 new_chain->is_caller_save_insn = 1;
1437 INSN_CODE (new_chain->insn) = code;
1438 return new_chain;
1440 #include "gt-caller-save.h"