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