Merge -r 127928:132243 from trunk
[official-gcc.git] / gcc / caller-save.c
blob92d02f75ead7f82a35509f62a1cd656f7047e933
1 /* Save and restore call-clobbered registers which are live across a call.
2 Copyright (C) 1989, 1992, 1994, 1995, 1997, 1998, 1999, 2000,
3 2001, 2002, 2003, 2004, 2005, 2006, 2007 Free Software Foundation, Inc.
5 This file is part of GCC.
7 GCC is free software; you can redistribute it and/or modify it under
8 the terms of the GNU General Public License as published by the Free
9 Software Foundation; either version 3, or (at your option) any later
10 version.
12 GCC is distributed in the hope that it will be useful, but WITHOUT ANY
13 WARRANTY; without even the implied warranty of MERCHANTABILITY or
14 FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
15 for more details.
17 You should have received a copy of the GNU General Public License
18 along with GCC; see the file COPYING3. If not see
19 <http://www.gnu.org/licenses/>. */
21 #include "config.h"
22 #include "system.h"
23 #include "coretypes.h"
24 #include "tm.h"
25 #include "rtl.h"
26 #include "regs.h"
27 #include "insn-config.h"
28 #include "flags.h"
29 #include "hard-reg-set.h"
30 #include "recog.h"
31 #include "basic-block.h"
32 #include "reload.h"
33 #include "function.h"
34 #include "expr.h"
35 #include "toplev.h"
36 #include "tm_p.h"
37 #include "addresses.h"
38 #include "output.h"
39 #include "ira.h"
40 #include "df.h"
41 #include "ggc.h"
43 /* Call used hard registers which can not be saved because there is no
44 insn for this. */
45 HARD_REG_SET no_caller_save_reg_set;
47 #ifndef MAX_MOVE_MAX
48 #define MAX_MOVE_MAX MOVE_MAX
49 #endif
51 #ifndef MIN_UNITS_PER_WORD
52 #define MIN_UNITS_PER_WORD UNITS_PER_WORD
53 #endif
55 #define MOVE_MAX_WORDS (MOVE_MAX / UNITS_PER_WORD)
57 /* Modes for each hard register that we can save. The smallest mode is wide
58 enough to save the entire contents of the register. When saving the
59 register because it is live we first try to save in multi-register modes.
60 If that is not possible the save is done one register at a time. */
62 static enum machine_mode
63 regno_save_mode[FIRST_PSEUDO_REGISTER][MAX_MOVE_MAX / MIN_UNITS_PER_WORD + 1];
65 /* For each hard register, a place on the stack where it can be saved,
66 if needed. */
68 static rtx
69 regno_save_mem[FIRST_PSEUDO_REGISTER][MAX_MOVE_MAX / MIN_UNITS_PER_WORD + 1];
71 /* We will only make a register eligible for caller-save if it can be
72 saved in its widest mode with a simple SET insn as long as the memory
73 address is valid. We record the INSN_CODE is those insns here since
74 when we emit them, the addresses might not be valid, so they might not
75 be recognized. */
77 static int
78 cached_reg_save_code[FIRST_PSEUDO_REGISTER][MAX_MACHINE_MODE];
79 static int
80 cached_reg_restore_code[FIRST_PSEUDO_REGISTER][MAX_MACHINE_MODE];
82 /* Set of hard regs currently residing in save area (during insn scan). */
84 static HARD_REG_SET hard_regs_saved;
86 /* Number of registers currently in hard_regs_saved. */
88 static int n_regs_saved;
90 /* Computed by mark_referenced_regs, all regs referenced in a given
91 insn. */
92 static HARD_REG_SET referenced_regs;
95 static void mark_set_regs (rtx, const_rtx, void *);
96 static void mark_referenced_regs (rtx);
97 static int insert_save (struct insn_chain *, int, int, HARD_REG_SET *,
98 enum machine_mode *);
99 static int insert_restore (struct insn_chain *, int, int, int,
100 enum machine_mode *);
101 static struct insn_chain *insert_one_insn (struct insn_chain *, int, int,
102 rtx);
103 static void add_stored_regs (rtx, const_rtx, void *);
105 static GTY(()) rtx savepat;
106 static GTY(()) rtx restpat;
107 static GTY(()) rtx test_reg;
108 static GTY(()) rtx test_mem;
109 static GTY(()) rtx saveinsn;
110 static GTY(()) rtx restinsn;
112 /* Return the INSN_CODE used to save register REG in mode MODE. */
113 static int
114 reg_save_code (int reg, enum machine_mode mode)
116 bool ok;
117 if (cached_reg_save_code[reg][mode])
118 return cached_reg_save_code[reg][mode];
119 if (!HARD_REGNO_MODE_OK (reg, mode))
121 cached_reg_save_code[reg][mode] = -1;
122 cached_reg_restore_code[reg][mode] = -1;
123 return -1;
126 /* Update the register number and modes of the register
127 and memory operand. */
128 SET_REGNO (test_reg, reg);
129 PUT_MODE (test_reg, mode);
130 PUT_MODE (test_mem, mode);
132 /* Force re-recognition of the modified insns. */
133 INSN_CODE (saveinsn) = -1;
134 INSN_CODE (restinsn) = -1;
136 cached_reg_save_code[reg][mode] = recog_memoized (saveinsn);
137 cached_reg_restore_code[reg][mode] = recog_memoized (restinsn);
139 /* Now extract both insns and see if we can meet their
140 constraints. */
141 ok = (cached_reg_save_code[reg][mode] != -1
142 && cached_reg_restore_code[reg][mode] != -1);
143 if (ok)
145 extract_insn (saveinsn);
146 ok = constrain_operands (1);
147 extract_insn (restinsn);
148 ok &= constrain_operands (1);
151 if (! ok)
153 cached_reg_save_code[reg][mode] = -1;
154 cached_reg_restore_code[reg][mode] = -1;
156 gcc_assert (cached_reg_save_code[reg][mode]);
157 return cached_reg_save_code[reg][mode];
160 /* Return the INSN_CODE used to restore register REG in mode MODE. */
161 static int
162 reg_restore_code (int reg, enum machine_mode mode)
164 if (cached_reg_restore_code[reg][mode])
165 return cached_reg_restore_code[reg][mode];
166 /* Populate our cache. */
167 reg_save_code (reg, mode);
168 return cached_reg_restore_code[reg][mode];
171 /* Initialize for caller-save.
173 Look at all the hard registers that are used by a call and for which
174 regclass.c has not already excluded from being used across a call.
176 Ensure that we can find a mode to save the register and that there is a
177 simple insn to save and restore the register. This latter check avoids
178 problems that would occur if we tried to save the MQ register of some
179 machines directly into memory. */
181 void
182 init_caller_save (void)
184 rtx addr_reg;
185 int offset;
186 rtx address;
187 int i, j;
189 CLEAR_HARD_REG_SET (no_caller_save_reg_set);
190 /* First find all the registers that we need to deal with and all
191 the modes that they can have. If we can't find a mode to use,
192 we can't have the register live over calls. */
194 for (i = 0; i < FIRST_PSEUDO_REGISTER; i++)
196 if (call_used_regs[i] && ! call_fixed_regs[i])
198 for (j = 1; j <= MOVE_MAX_WORDS; j++)
200 regno_save_mode[i][j] = HARD_REGNO_CALLER_SAVE_MODE (i, j,
201 VOIDmode);
202 if (regno_save_mode[i][j] == VOIDmode && j == 1)
204 call_fixed_regs[i] = 1;
205 SET_HARD_REG_BIT (call_fixed_reg_set, i);
209 else
210 regno_save_mode[i][1] = VOIDmode;
213 /* The following code tries to approximate the conditions under which
214 we can easily save and restore a register without scratch registers or
215 other complexities. It will usually work, except under conditions where
216 the validity of an insn operand is dependent on the address offset.
217 No such cases are currently known.
219 We first find a typical offset from some BASE_REG_CLASS register.
220 This address is chosen by finding the first register in the class
221 and by finding the smallest power of two that is a valid offset from
222 that register in every mode we will use to save registers. */
224 for (i = 0; i < FIRST_PSEUDO_REGISTER; i++)
225 if (TEST_HARD_REG_BIT
226 (reg_class_contents
227 [(int) base_reg_class (regno_save_mode [i][1], PLUS, CONST_INT)], i))
228 break;
230 gcc_assert (i < FIRST_PSEUDO_REGISTER);
232 addr_reg = gen_rtx_REG (Pmode, i);
234 for (offset = 1 << (HOST_BITS_PER_INT / 2); offset; offset >>= 1)
236 address = gen_rtx_PLUS (Pmode, addr_reg, GEN_INT (offset));
238 for (i = 0; i < FIRST_PSEUDO_REGISTER; i++)
239 if (regno_save_mode[i][1] != VOIDmode
240 && ! strict_memory_address_p (regno_save_mode[i][1], address))
241 break;
243 if (i == FIRST_PSEUDO_REGISTER)
244 break;
247 /* If we didn't find a valid address, we must use register indirect. */
248 if (offset == 0)
249 address = addr_reg;
251 /* Next we try to form an insn to save and restore the register. We
252 see if such an insn is recognized and meets its constraints.
254 To avoid lots of unnecessary RTL allocation, we construct all the RTL
255 once, then modify the memory and register operands in-place. */
257 test_reg = gen_rtx_REG (VOIDmode, 0);
258 test_mem = gen_rtx_MEM (VOIDmode, address);
259 savepat = gen_rtx_SET (VOIDmode, test_mem, test_reg);
260 restpat = gen_rtx_SET (VOIDmode, test_reg, test_mem);
262 saveinsn = gen_rtx_INSN (VOIDmode, 0, 0, 0, 0, 0, savepat, -1, 0);
263 restinsn = gen_rtx_INSN (VOIDmode, 0, 0, 0, 0, 0, restpat, -1, 0);
265 for (i = 0; i < FIRST_PSEUDO_REGISTER; i++)
266 for (j = 1; j <= MOVE_MAX_WORDS; j++)
267 if (reg_save_code (i,regno_save_mode[i][j]) == -1)
269 regno_save_mode[i][j] = VOIDmode;
270 if (j == 1)
272 call_fixed_regs[i] = 1;
273 SET_HARD_REG_BIT (call_fixed_reg_set, i);
274 if (call_used_regs[i])
275 SET_HARD_REG_BIT (no_caller_save_reg_set, i);
282 /* Initialize save areas by showing that we haven't allocated any yet. */
284 void
285 init_save_areas (void)
287 int i, j;
289 for (i = 0; i < FIRST_PSEUDO_REGISTER; i++)
290 for (j = 1; j <= MOVE_MAX_WORDS; j++)
291 regno_save_mem[i][j] = 0;
294 /* The structure represents a hard register which should be saved
295 through the call. */
296 struct saved_hard_reg
298 /* Order number starting with 0. */
299 int num;
300 /* The hard regno. */
301 int hard_regno;
302 /* Execution frequency of all calls through which given hard
303 register should be saved. */
304 int call_freq;
305 /* Stack slot reserved to save the hard register through calls. */
306 rtx slot;
307 /* True if it is first hard register in the chain of hard registers
308 sharing the same stack slot. */
309 int first_p;
310 /* Order number of the next hard register with the same slot in the
311 chain. -1 represents end of the chain. */
312 int next;
315 /* Map: hard register number to the corresponding structure. */
316 static struct saved_hard_reg *hard_reg_map [FIRST_PSEUDO_REGISTER];
318 /* The number of all structures representing hard register should be
319 saved. */
320 static int saved_regs_num;
322 /* Pointers to all the structures. Index is the order number of the
323 structure. */
324 static struct saved_hard_reg *all_saved_regs [FIRST_PSEUDO_REGISTER];
326 /* First called function for work with saved hard registers. */
327 static void
328 initiate_saved_hard_regs (void)
330 int i;
332 saved_regs_num = 0;
333 for (i = 0; i < FIRST_PSEUDO_REGISTER; i++)
334 hard_reg_map [i] = NULL;
337 /* Allocate and return new saved hard register with given REGNO and
338 CALL_FREQ. */
339 static struct saved_hard_reg *
340 new_saved_hard_reg (int regno, int call_freq)
342 struct saved_hard_reg *saved_reg;
344 saved_reg = xmalloc (sizeof (struct saved_hard_reg));
345 hard_reg_map [regno] = all_saved_regs [saved_regs_num] = saved_reg;
346 saved_reg->num = saved_regs_num++;
347 saved_reg->hard_regno = regno;
348 saved_reg->call_freq = call_freq;
349 saved_reg->first_p = FALSE;
350 saved_reg->next = -1;
351 return saved_reg;
354 /* Free memory allocated for the saved hard registers. */
355 static void
356 finish_saved_hard_regs (void)
358 int i;
360 for (i = 0; i < saved_regs_num; i++)
361 free (all_saved_regs [i]);
364 /* The function is used to sort the saved hard registers according
365 their frequency. */
366 static int
367 saved_hard_reg_compare_func (const void *v1p, const void *v2p)
369 struct saved_hard_reg *p1 = *(struct saved_hard_reg **) v1p;
370 struct saved_hard_reg *p2 = *(struct saved_hard_reg **) v2p;
372 if (flag_omit_frame_pointer)
374 if (p1->call_freq - p2->call_freq != 0)
375 return p1->call_freq - p2->call_freq;
377 else if (p2->call_freq - p1->call_freq != 0)
378 return p2->call_freq - p1->call_freq;
380 return p1->num - p2->num;
383 /* Allocate save areas for any hard registers that might need saving.
384 We take a conservative approach here and look for call-clobbered hard
385 registers that are assigned to pseudos that cross calls. This may
386 overestimate slightly (especially if some of these registers are later
387 used as spill registers), but it should not be significant.
389 For IRA we use priority coloring to decrease stack slots needed for
390 saving hard registers through calls.
392 Future work:
394 In the fallback case we should iterate backwards across all possible
395 modes for the save, choosing the largest available one instead of
396 falling back to the smallest mode immediately. (eg TF -> DF -> SF).
398 We do not try to use "move multiple" instructions that exist
399 on some machines (such as the 68k moveml). It could be a win to try
400 and use them when possible. The hard part is doing it in a way that is
401 machine independent since they might be saving non-consecutive
402 registers. (imagine caller-saving d0,d1,a0,a1 on the 68k) */
404 void
405 setup_save_areas (void)
407 int i, j, k;
408 unsigned int r;
409 HARD_REG_SET hard_regs_used;
411 /* Allocate space in the save area for the largest multi-register
412 pseudos first, then work backwards to single register
413 pseudos. */
415 /* Find and record all call-used hard-registers in this function. */
416 CLEAR_HARD_REG_SET (hard_regs_used);
417 for (i = FIRST_PSEUDO_REGISTER; i < max_regno; i++)
418 if (reg_renumber[i] >= 0 && REG_N_CALLS_CROSSED (i) > 0)
420 unsigned int regno = reg_renumber[i];
421 unsigned int endregno
422 = end_hard_regno (GET_MODE (regno_reg_rtx[i]), regno);
423 if (flag_ira && flag_ira_ipra)
425 HARD_REG_SET clobbered_regs;
427 collect_pseudo_call_clobbered_regs (i, &clobbered_regs);
428 for (r = regno; r < endregno; r++)
429 if (TEST_HARD_REG_BIT (clobbered_regs, r))
430 SET_HARD_REG_BIT (hard_regs_used, r);
432 else
433 for (r = regno; r < endregno; r++)
434 if (call_used_regs[r])
435 SET_HARD_REG_BIT (hard_regs_used, r);
438 if (flag_ira && flag_ira_share_save_slots)
440 rtx insn, slot;
441 struct insn_chain *chain, *next;
442 char *saved_reg_conflicts;
443 unsigned int regno;
444 int next_k, freq;
445 struct saved_hard_reg *saved_reg, *saved_reg2, *saved_reg3;
446 int call_saved_regs_num;
447 struct saved_hard_reg *call_saved_regs [FIRST_PSEUDO_REGISTER];
448 HARD_REG_SET hard_regs_to_save, used_regs, this_insn_sets;
449 reg_set_iterator rsi;
451 initiate_saved_hard_regs ();
452 /* Create hard reg saved regs. */
453 for (chain = reload_insn_chain; chain != 0; chain = next)
455 insn = chain->insn;
456 next = chain->next;
457 if (GET_CODE (insn) != CALL_INSN
458 || find_reg_note (insn, REG_NORETURN, NULL))
459 continue;
460 freq = REG_FREQ_FROM_BB (BLOCK_FOR_INSN (insn));
461 REG_SET_TO_HARD_REG_SET (hard_regs_to_save,
462 &chain->live_throughout);
463 get_call_invalidated_used_regs (insn, &used_regs, false);
465 /* Record all registers set in this call insn. These don't
466 need to be saved. N.B. the call insn might set a subreg
467 of a multi-hard-reg pseudo; then the pseudo is considered
468 live during the call, but the subreg that is set
469 isn't. */
470 CLEAR_HARD_REG_SET (this_insn_sets);
471 note_stores (PATTERN (insn), mark_set_regs, &this_insn_sets);
472 /* Sibcalls are considered to set the return value,
473 compare flow.c:propagate_one_insn. */
474 if (SIBLING_CALL_P (insn) && current_function_return_rtx)
475 mark_set_regs (current_function_return_rtx, NULL_RTX,
476 &this_insn_sets);
478 AND_COMPL_HARD_REG_SET (used_regs, call_fixed_reg_set);
479 AND_COMPL_HARD_REG_SET (used_regs, this_insn_sets);
480 AND_HARD_REG_SET (hard_regs_to_save, used_regs);
481 for (regno = 0; regno < FIRST_PSEUDO_REGISTER; regno++)
482 if (TEST_HARD_REG_BIT (hard_regs_to_save, regno))
484 if (hard_reg_map [regno] != NULL)
485 hard_reg_map [regno]->call_freq += freq;
486 else
487 saved_reg = new_saved_hard_reg (regno, freq);
489 /* Look through all live pseudos, mark their hard registers. */
490 EXECUTE_IF_SET_IN_REG_SET
491 (&chain->live_throughout, FIRST_PSEUDO_REGISTER, regno, rsi)
493 int r = reg_renumber [regno];
494 int bound;
496 if (r < 0)
497 continue;
499 bound = r + hard_regno_nregs [r] [PSEUDO_REGNO_MODE (regno)];
500 for (; r < bound; r++)
501 if (TEST_HARD_REG_BIT (used_regs, r))
503 if (hard_reg_map [r] != NULL)
504 hard_reg_map [r]->call_freq += freq;
505 else
506 saved_reg = new_saved_hard_reg (r, freq);
510 /* Find saved hard register conflicts. */
511 saved_reg_conflicts = xmalloc (saved_regs_num * saved_regs_num);
512 memset (saved_reg_conflicts, 0, saved_regs_num * saved_regs_num);
513 for (chain = reload_insn_chain; chain != 0; chain = next)
515 call_saved_regs_num = 0;
516 insn = chain->insn;
517 next = chain->next;
518 if (GET_CODE (insn) != CALL_INSN
519 || find_reg_note (insn, REG_NORETURN, NULL))
520 continue;
521 REG_SET_TO_HARD_REG_SET (hard_regs_to_save,
522 &chain->live_throughout);
523 get_call_invalidated_used_regs (insn, &used_regs, false);
525 /* Record all registers set in this call insn. These don't
526 need to be saved. N.B. the call insn might set a subreg
527 of a multi-hard-reg pseudo; then the pseudo is considered
528 live during the call, but the subreg that is set
529 isn't. */
530 CLEAR_HARD_REG_SET (this_insn_sets);
531 note_stores (PATTERN (insn), mark_set_regs, &this_insn_sets);
532 /* Sibcalls are considered to set the return value,
533 compare flow.c:propagate_one_insn. */
534 if (SIBLING_CALL_P (insn) && current_function_return_rtx)
535 mark_set_regs (current_function_return_rtx, NULL_RTX,
536 &this_insn_sets);
538 AND_COMPL_HARD_REG_SET (used_regs, call_fixed_reg_set);
539 AND_COMPL_HARD_REG_SET (used_regs, this_insn_sets);
540 AND_HARD_REG_SET (hard_regs_to_save, used_regs);
541 for (regno = 0; regno < FIRST_PSEUDO_REGISTER; regno++)
542 if (TEST_HARD_REG_BIT (hard_regs_to_save, regno))
544 gcc_assert (hard_reg_map [regno] != NULL);
545 call_saved_regs [call_saved_regs_num++] = hard_reg_map [regno];
547 /* Look through all live pseudos, mark their hard registers. */
548 EXECUTE_IF_SET_IN_REG_SET
549 (&chain->live_throughout, FIRST_PSEUDO_REGISTER, regno, rsi)
551 int r = reg_renumber[regno];
552 int bound;
554 if (r < 0)
555 continue;
556 bound = r + hard_regno_nregs [r] [PSEUDO_REGNO_MODE (regno)];
557 for (; r < bound; r++)
558 if (TEST_HARD_REG_BIT (used_regs, r))
559 call_saved_regs [call_saved_regs_num++] = hard_reg_map [r];
561 for (i = 0; i < call_saved_regs_num; i++)
563 saved_reg = call_saved_regs [i];
564 for (j = 0; j < call_saved_regs_num; j++)
565 if (i != j)
567 saved_reg2 = call_saved_regs [j];
568 saved_reg_conflicts [saved_reg->num * saved_regs_num
569 + saved_reg2->num]
570 = saved_reg_conflicts [saved_reg2->num * saved_regs_num
571 + saved_reg->num]
572 = TRUE;
576 /* Sort saved hard regs. */
577 qsort (all_saved_regs, saved_regs_num, sizeof (struct saved_hard_reg *),
578 saved_hard_reg_compare_func);
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 for (j = 0; j < i; j++)
585 saved_reg2 = all_saved_regs [j];
586 if (! saved_reg2->first_p)
587 continue;
588 slot = saved_reg2->slot;
589 for (k = j; k >= 0; k = next_k)
591 saved_reg3 = all_saved_regs [k];
592 next_k = saved_reg3->next;
593 if (saved_reg_conflicts [saved_reg->num * saved_regs_num
594 + saved_reg3->num])
595 break;
597 if (k < 0
598 && (GET_MODE_SIZE (regno_save_mode
599 [saved_reg->hard_regno] [1])
600 <= GET_MODE_SIZE (regno_save_mode
601 [saved_reg2->hard_regno] [1])))
603 saved_reg->slot = slot;
604 regno_save_mem [saved_reg->hard_regno] [1] = slot;
605 saved_reg->next = saved_reg2->next;
606 saved_reg2->next = i;
607 if (dump_file != NULL)
608 fprintf (dump_file, "%d uses slot of %d\n",
609 saved_reg->hard_regno, saved_reg2->hard_regno);
610 break;
613 if (j == i)
615 saved_reg->first_p = TRUE;
616 if (regno_save_mem [saved_reg->hard_regno] [1] != NULL_RTX)
618 saved_reg->slot = regno_save_mem [saved_reg->hard_regno] [1];
619 if (dump_file != NULL)
620 fprintf (dump_file,
621 "%d uses slot from prev iteration\n",
622 saved_reg->hard_regno);
624 else
626 saved_reg->slot
627 = assign_stack_local
628 (regno_save_mode [saved_reg->hard_regno] [1],
629 GET_MODE_SIZE (regno_save_mode
630 [saved_reg->hard_regno] [1]), 0);
631 regno_save_mem [saved_reg->hard_regno] [1] = saved_reg->slot;
632 if (dump_file != NULL)
633 fprintf (dump_file, "%d uses a new slot\n",
634 saved_reg->hard_regno);
638 free (saved_reg_conflicts);
639 finish_saved_hard_regs ();
641 else
643 /* Now run through all the call-used hard-registers and allocate
644 space for them in the caller-save area. Try to allocate space
645 in a manner which allows multi-register saves/restores to be done. */
647 for (i = 0; i < FIRST_PSEUDO_REGISTER; i++)
648 for (j = MOVE_MAX_WORDS; j > 0; j--)
650 int do_save = 1;
652 /* If no mode exists for this size, try another. Also break out
653 if we have already saved this hard register. */
654 if (regno_save_mode[i][j] == VOIDmode || regno_save_mem[i][1] != 0)
655 continue;
657 /* See if any register in this group has been saved. */
658 for (k = 0; k < j; k++)
659 if (regno_save_mem[i + k][1])
661 do_save = 0;
662 break;
664 if (! do_save)
665 continue;
667 for (k = 0; k < j; k++)
668 if (! TEST_HARD_REG_BIT (hard_regs_used, i + k))
670 do_save = 0;
671 break;
673 if (! do_save)
674 continue;
676 /* We have found an acceptable mode to store in. */
677 regno_save_mem[i][j]
678 = assign_stack_local (regno_save_mode[i][j],
679 GET_MODE_SIZE (regno_save_mode[i][j]), 0);
681 /* Setup single word save area just in case... */
682 for (k = 0; k < j; k++)
683 /* This should not depend on WORDS_BIG_ENDIAN.
684 The order of words in regs is the same as in memory. */
685 regno_save_mem[i + k][1]
686 = adjust_address_nv (regno_save_mem[i][j],
687 regno_save_mode[i + k][1],
688 k * UNITS_PER_WORD);
692 /* Now loop again and set the alias set of any save areas we made to
693 the alias set used to represent frame objects. */
694 for (i = 0; i < FIRST_PSEUDO_REGISTER; i++)
695 for (j = MOVE_MAX_WORDS; j > 0; j--)
696 if (regno_save_mem[i][j] != 0)
697 set_mem_alias_set (regno_save_mem[i][j], get_frame_alias_set ());
701 /* The following structure contains basic block data flow information
702 used to calculate hard registers to save at BB ends. Data flow
703 equation is bidirectional because we don't want to put save/restore
704 code on CFG edges. */
705 struct bb_info
707 /* The basic block reverse post-order number. */
708 int rts_number;
709 /* True if save_in should empty for this block. */
710 int empty_save_in_p;
711 /* Hard registers correspondingly used (or set) and should be saved but
712 not used (or used) afterward in the basic block. */
713 HARD_REG_SET kill, gen;
714 /* Registers needed to be saved at the start and end of the basic
715 block. */
716 HARD_REG_SET save_in, save_out;
717 /* Hard registers living at the start of the basic block. */
718 HARD_REG_SET live_at_start;
719 /* We don't want to generate save/restore insns on edges because it
720 changes CFG during reload. To prevent this we use the following
721 set. This set defines what hard registers should be saved at the
722 end of basic block. */
723 HARD_REG_SET save_here;
724 /* Saving modes at the start and end of the basic block. */
725 unsigned char save_in_mode [FIRST_PSEUDO_REGISTER];
726 /* It corresponds to set GEN right after the call of
727 calculate_local_save_info. */
728 unsigned char save_out_mode [FIRST_PSEUDO_REGISTER];
731 /* Macros for accessing data flow information of basic blocks. */
732 #define BB_INFO(BB) ((struct bb_info *) (BB)->aux)
733 #define BB_INFO_BY_INDEX(N) BB_INFO (BASIC_BLOCK(N))
735 /* The function calculates sets KILL, GEN, LIVE_AT_START and
736 SAVE_OUT_MODES corresponding to GEN for basic blocks. */
737 static void
738 calculate_local_save_info (void)
740 int i, empty_save_in_p;
741 struct insn_chain *chain, *next;
742 struct bb_info *bb_info;
743 /* Computed in mark_set_regs, holds all registers set by the current
744 instruction. */
745 HARD_REG_SET this_insn_sets, gen, kill;
746 unsigned regno;
747 reg_set_iterator rsi;
748 enum machine_mode save_mode [FIRST_PSEUDO_REGISTER];
750 CLEAR_HARD_REG_SET (gen);
751 CLEAR_HARD_REG_SET (kill);
752 empty_save_in_p = FALSE;
753 for (i = 0; i < FIRST_PSEUDO_REGISTER; i++)
754 save_mode [i] = VOIDmode;
756 for (chain = reload_insn_chain; chain != 0; chain = next)
758 rtx insn = chain->insn;
759 enum rtx_code code = GET_CODE (insn);
761 next = chain->next;
763 gcc_assert (!chain->is_caller_save_insn);
765 bb_info = BB_INFO_BY_INDEX (chain->block);
766 if (INSN_P (insn))
768 if (JUMP_P (insn)
769 && (GET_CODE (PATTERN (insn)) == ADDR_DIFF_VEC
770 || GET_CODE (PATTERN (insn)) == ADDR_VEC))
771 empty_save_in_p = TRUE;
773 CLEAR_HARD_REG_SET (referenced_regs);
774 mark_referenced_regs (PATTERN (insn));
775 IOR_HARD_REG_SET (kill, referenced_regs);
776 AND_COMPL_HARD_REG_SET (gen, referenced_regs);
778 if (code == CALL_INSN && find_reg_note (insn, REG_NORETURN, NULL))
779 SET_HARD_REG_SET (kill);
780 else if (code == CALL_INSN)
782 HARD_REG_SET hard_regs_to_save, used_regs;
784 /* Use the register life information in CHAIN to compute
785 which regs are live during the call. */
786 REG_SET_TO_HARD_REG_SET (hard_regs_to_save,
787 &chain->live_throughout);
788 /* Look through all live pseudos, mark their hard registers
789 and choose proper mode for saving. */
790 EXECUTE_IF_SET_IN_REG_SET
791 (&chain->live_throughout, FIRST_PSEUDO_REGISTER, regno, rsi)
793 int r = reg_renumber[regno];
794 int nregs;
795 enum machine_mode mode;
797 if (flag_ira && r < 0)
798 continue;
799 gcc_assert (r >= 0);
800 nregs = hard_regno_nregs[r][PSEUDO_REGNO_MODE (regno)];
801 mode = HARD_REGNO_CALLER_SAVE_MODE
802 (r, nregs, PSEUDO_REGNO_MODE (regno));
803 if (nregs == 1)
805 SET_HARD_REG_BIT (hard_regs_to_save, r);
806 save_mode[r] = mode;
808 else
810 while (nregs-- > 0)
812 SET_HARD_REG_BIT (hard_regs_to_save, r + nregs);
813 save_mode [r + nregs]
814 = regno_save_mode [r + nregs] [1];
819 /* Record all registers set in this call insn. These
820 don't need to be saved. N.B. the call insn might set
821 a subreg of a multi-hard-reg pseudo; then the pseudo
822 is considered live during the call, but the subreg
823 that is set isn't. */
824 CLEAR_HARD_REG_SET (this_insn_sets);
825 note_stores (PATTERN (insn), mark_set_regs, &this_insn_sets);
826 /* Sibcalls are considered to set the return value,
827 compare flow.c:propagate_one_insn. */
828 if (SIBLING_CALL_P (insn) && current_function_return_rtx)
829 mark_set_regs (current_function_return_rtx, NULL_RTX,
830 &this_insn_sets);
832 /* Compute which hard regs must be saved before this call. */
833 AND_COMPL_HARD_REG_SET (hard_regs_to_save, call_fixed_reg_set);
834 AND_COMPL_HARD_REG_SET (hard_regs_to_save, this_insn_sets);
835 get_call_invalidated_used_regs (insn, &used_regs, false);
836 AND_HARD_REG_SET (hard_regs_to_save, used_regs);
837 IOR_HARD_REG_SET (gen, hard_regs_to_save);
841 if (chain->next == 0 || chain->next->block != chain->block)
843 basic_block bb = BASIC_BLOCK (chain->block);
845 bb_info->empty_save_in_p = empty_save_in_p;
846 empty_save_in_p = FALSE;
847 CLEAR_HARD_REG_SET (bb_info->save_in);
848 COPY_HARD_REG_SET (bb_info->gen, gen);
849 for (i = 0; i < FIRST_PSEUDO_REGISTER; i++)
851 bb_info->save_in_mode [i] = VOIDmode;
852 if (TEST_HARD_REG_BIT (gen, i))
853 bb_info->save_out_mode [i] = save_mode [i];
854 else
855 bb_info->save_out_mode [i] = VOIDmode;
857 COPY_HARD_REG_SET (bb_info->kill, kill);
858 SET_HARD_REG_SET (bb_info->save_out);
859 REG_SET_TO_HARD_REG_SET
860 (bb_info->live_at_start,
861 flag_ira ? DF_LR_IN (bb) : DF_LIVE_IN (bb));
862 EXECUTE_IF_SET_IN_REG_SET
863 ((flag_ira ? DF_LR_IN (bb) : DF_LIVE_IN (bb)),
864 FIRST_PSEUDO_REGISTER, regno, rsi)
866 int r = reg_renumber[regno];
867 int nregs;
869 if (r < 0)
870 continue;
871 nregs = hard_regno_nregs[r][PSEUDO_REGNO_MODE (regno)];
872 while (nregs-- > 0)
873 SET_HARD_REG_BIT (bb_info->live_at_start, r + nregs);
875 CLEAR_HARD_REG_SET (bb_info->save_here);
876 CLEAR_HARD_REG_SET (gen);
877 CLEAR_HARD_REG_SET (kill);
878 for (i = 0; i < FIRST_PSEUDO_REGISTER; i++)
879 save_mode [i] = VOIDmode;
884 /* The function sets up reverse post-order number of each basic
885 block. */
886 static void
887 set_up_bb_rts_numbers (void)
889 int i;
890 int *rts_order;
892 rts_order = XNEWVEC (int, n_basic_blocks - NUM_FIXED_BLOCKS);
893 post_order_compute (rts_order, false, false);
894 for (i = 0; i < n_basic_blocks - NUM_FIXED_BLOCKS; i++)
895 BB_INFO_BY_INDEX (rts_order [i])->rts_number = i;
896 free (rts_order);
899 /* Compare function for sorting blocks in reverse postorder. */
900 static int
901 rpost_cmp (const void *bb1, const void *bb2)
903 basic_block b1 = *(basic_block *) bb1, b2 = *(basic_block *) bb2;
905 return BB_INFO (b2)->rts_number - BB_INFO (b1)->rts_number;
908 /* The function calculates global save information according
909 to the following equations:
911 bb.save_in = empty for entry block or one with empty_save_in_p
912 | ^ (pred.save_out - pred.save_here) ^ bb.live_at_start
913 bb.save_out = (bb.save_in - bb.kill) U bb.gen.
915 See function calculate_save_here to know how SAVE_HERE is
916 calculated */
917 static void
918 calculate_save_in_out (void)
920 basic_block bb, succ;
921 edge e;
922 int i, j, nel;
923 VEC(basic_block,heap) *bbs, *new_bbs, *temp;
924 basic_block *bb_array;
925 HARD_REG_SET temp_set;
926 sbitmap wset;
928 bbs = VEC_alloc (basic_block, heap, last_basic_block);
929 new_bbs = VEC_alloc (basic_block, heap, last_basic_block);
930 FOR_EACH_BB (bb)
932 VEC_quick_push (basic_block, bbs, bb);
934 wset = sbitmap_alloc (last_basic_block);
935 while (VEC_length (basic_block, bbs))
937 bb_array = VEC_address (basic_block, bbs);
938 nel = VEC_length (basic_block, bbs);
939 qsort (bb_array, nel, sizeof (basic_block), rpost_cmp);
940 sbitmap_zero (wset);
941 for (i = 0; i < nel; i++)
943 int first_p;
944 edge_iterator ei;
945 struct bb_info *bb_info;
947 bb = bb_array [i];
948 bb_info = BB_INFO (bb);
949 first_p = TRUE;
950 if (! bb_info->empty_save_in_p)
951 FOR_EACH_EDGE (e, ei, bb->preds)
953 basic_block pred = e->src;
955 if (e->flags & EDGE_ABNORMAL)
957 CLEAR_HARD_REG_SET (bb_info->save_in);
958 break;
960 if (pred->index != ENTRY_BLOCK)
962 COPY_HARD_REG_SET (temp_set, BB_INFO (pred)->save_out);
963 AND_COMPL_HARD_REG_SET (temp_set,
964 BB_INFO (pred)->save_here);
965 AND_HARD_REG_SET (temp_set, bb_info->live_at_start);
966 if (first_p)
967 COPY_HARD_REG_SET (bb_info->save_in, temp_set);
968 else
969 AND_HARD_REG_SET (bb_info->save_in, temp_set);
970 for (j = 0; j < FIRST_PSEUDO_REGISTER; j++)
971 if (TEST_HARD_REG_BIT (temp_set, j))
973 gcc_assert
974 (bb_info->save_in_mode [j] == VOIDmode
975 || BB_INFO (pred)->save_out_mode [j] == VOIDmode
976 || (bb_info->save_in_mode [j]
977 == BB_INFO (pred)->save_out_mode [j]));
978 if (BB_INFO (pred)->save_out_mode [j] != VOIDmode)
979 bb_info->save_in_mode [j]
980 = BB_INFO (pred)->save_out_mode [j];
982 first_p = FALSE;
985 COPY_HARD_REG_SET (temp_set, bb_info->save_in);
986 AND_COMPL_HARD_REG_SET (temp_set, bb_info->kill);
987 for (j = 0; j < FIRST_PSEUDO_REGISTER; j++)
988 if (TEST_HARD_REG_BIT (temp_set, j)
989 && ! TEST_HARD_REG_BIT (bb_info->gen, j))
991 gcc_assert (bb_info->save_out_mode [j] == VOIDmode
992 || (bb_info->save_out_mode [j]
993 == bb_info->save_in_mode [j]));
994 bb_info->save_out_mode [j] = bb_info->save_in_mode [j];
996 IOR_HARD_REG_SET (temp_set, bb_info->gen);
998 if (! hard_reg_set_equal_p (temp_set, bb_info->save_out))
1000 COPY_HARD_REG_SET (bb_info->save_out, temp_set);
1001 FOR_EACH_EDGE (e, ei, bb->succs)
1003 succ = e->dest;
1004 if (succ->index != EXIT_BLOCK
1005 && ! TEST_BIT (wset, succ->index))
1007 SET_BIT (wset, succ->index);
1008 VEC_quick_push (basic_block, new_bbs, succ);
1013 temp = bbs;
1014 bbs = new_bbs;
1015 new_bbs = temp;
1016 VEC_truncate (basic_block, new_bbs, 0);
1018 sbitmap_free (wset);
1019 VEC_free (basic_block, heap, new_bbs);
1020 VEC_free (basic_block, heap, bbs);
1023 /* The function calculates SAVE_HERE according to the equation
1024 bb.save_here = U ((bb.save_out - succ.save_in) ^ succ.live_at_start). */
1025 static int
1026 calculate_save_here (void)
1028 basic_block bb;
1029 edge e;
1030 edge_iterator ei;
1031 HARD_REG_SET save_here, temp_set;
1032 int changed_p = FALSE;
1034 FOR_EACH_BB (bb)
1036 CLEAR_HARD_REG_SET (save_here);
1037 FOR_EACH_EDGE (e, ei, bb->succs)
1039 basic_block dest = e->dest;
1041 COPY_HARD_REG_SET (temp_set, BB_INFO (bb)->save_out);
1042 AND_COMPL_HARD_REG_SET (temp_set, BB_INFO (dest)->save_in);
1043 AND_HARD_REG_SET (temp_set, BB_INFO (dest)->live_at_start);
1044 IOR_HARD_REG_SET (save_here, temp_set);
1047 if (! hard_reg_set_equal_p (save_here, BB_INFO (bb)->save_here))
1049 COPY_HARD_REG_SET (BB_INFO (bb)->save_here, save_here);
1050 changed_p = TRUE;
1053 return changed_p;
1056 /* The function calculates global save information used to put
1057 save/restore code without generating new blocks. This is a
1058 bidirectional data flow problem (calculation of SAVE_IN and
1059 SAVE_OUT is a forward data flow problem and SAVE_HERE is a backward
1060 one). It is complicated by calculation of modes for
1061 saving/restoring call used hard registers. */
1062 static void
1063 make_global_save_analysis (void)
1065 int iter, changed_p;
1067 calculate_local_save_info ();
1068 set_up_bb_rts_numbers ();
1069 for (iter = 1;; iter++)
1071 calculate_save_in_out ();
1072 changed_p = calculate_save_here ();
1073 if (! changed_p)
1074 break;
1076 if (dump_file != NULL)
1077 fprintf (dump_file, " Number of global save analysis iterations %d\n",
1078 iter);
1081 /* Print hard registers in SET to file F. The registers are printed
1082 with its mode given in MODES. */
1083 static void
1084 print_hard_reg_set_and_mode (FILE *f, HARD_REG_SET set, unsigned char *modes)
1086 int i;
1088 for (i = 0; i < FIRST_PSEUDO_REGISTER; i++)
1089 if (TEST_HARD_REG_BIT (set, i))
1090 fprintf (f, " %d:%s %s", i, GET_MODE_NAME (modes [i]), reg_names [i]);
1093 /* Print hard registers in SET to file F. */
1094 static void
1095 print_hard_reg_set (FILE *f, HARD_REG_SET set)
1097 int i;
1099 for (i = 0; i < FIRST_PSEUDO_REGISTER; i++)
1100 if (TEST_HARD_REG_BIT (set, i))
1101 fprintf (f, " %d %s", i, reg_names [i]);
1104 /* Print save information for each block to file F. */
1105 static void
1106 print_save_data (FILE *f)
1108 basic_block bb;
1109 struct bb_info *bb_info;
1111 FOR_EACH_BB (bb)
1113 bb_info = BB_INFO (bb);
1114 fprintf (f, "bb %d:\n save_in:", bb->index);
1115 print_hard_reg_set_and_mode (f, bb_info->save_in,
1116 bb_info->save_in_mode);
1117 fprintf (f, "\n save_out:");
1118 print_hard_reg_set_and_mode (f, bb_info->save_out,
1119 bb_info->save_out_mode);
1120 fprintf (f, "\n live_at_start:");
1121 print_hard_reg_set (f, bb_info->live_at_start);
1122 fprintf (f, "\n save_here:");
1123 print_hard_reg_set (f, bb_info->save_here);
1124 fprintf (f, "\n kill:");
1125 print_hard_reg_set (f, bb_info->kill);
1126 fprintf (f, "\n gen:");
1127 print_hard_reg_set (f, bb_info->gen);
1128 fprintf (f, "\n\n");
1132 /* Print save information for each block to stderr. */
1133 void
1134 debug_save_data (void)
1136 print_save_data (stderr);
1139 /* Setup hard registers in SET to save. Setup also their save modes
1140 in SAVE_MODE from FROM_SAVE_MODE. */
1141 static void
1142 set_hard_reg_saved (HARD_REG_SET set, unsigned char *from_saved_mode,
1143 enum machine_mode *save_mode)
1145 int regno;
1147 n_regs_saved = 0;
1148 COPY_HARD_REG_SET (hard_regs_saved, set);
1149 for (regno = 0; regno < FIRST_PSEUDO_REGISTER; regno++)
1150 if (TEST_HARD_REG_BIT (set, regno))
1152 n_regs_saved++;
1153 save_mode [regno] = from_saved_mode [regno];
1155 else
1156 save_mode [regno] = VOIDmode;
1159 /* Find the places where hard regs are live across calls and save them. */
1161 void
1162 save_call_clobbered_regs (void)
1164 /* Computed in mark_set_regs, holds all registers set by the current
1165 instruction. */
1166 HARD_REG_SET this_insn_sets;
1167 struct insn_chain *chain, *next;
1168 enum machine_mode save_mode [FIRST_PSEUDO_REGISTER];
1170 if (flag_ira && flag_ira_move_spills)
1172 alloc_aux_for_blocks (sizeof (struct bb_info));
1173 make_global_save_analysis ();
1176 CLEAR_HARD_REG_SET (hard_regs_saved);
1177 n_regs_saved = 0;
1179 for (chain = reload_insn_chain; chain != 0; chain = next)
1181 rtx insn = chain->insn;
1182 enum rtx_code code = GET_CODE (insn);
1184 next = chain->next;
1186 gcc_assert (!chain->is_caller_save_insn);
1188 if (INSN_P (insn))
1190 /* If some registers have been saved, see if INSN references
1191 any of them. We must restore them before the insn if so. */
1193 if (n_regs_saved)
1195 int regno;
1197 if (code == JUMP_INSN)
1198 /* Restore all registers if this is a JUMP_INSN. */
1199 COPY_HARD_REG_SET (referenced_regs, hard_regs_saved);
1200 else
1202 CLEAR_HARD_REG_SET (referenced_regs);
1203 mark_referenced_regs (PATTERN (insn));
1204 AND_HARD_REG_SET (referenced_regs, hard_regs_saved);
1207 for (regno = 0; regno < FIRST_PSEUDO_REGISTER; regno++)
1208 if (TEST_HARD_REG_BIT (referenced_regs, regno))
1210 int before = regno;
1212 regno += insert_restore (chain, 1, regno, MOVE_MAX_WORDS,
1213 save_mode);
1214 if (flag_ira && flag_ira_move_spills)
1216 gcc_assert (before == regno);
1217 save_mode [before] = VOIDmode;
1222 if (code == CALL_INSN
1223 && ! SIBLING_CALL_P (insn)
1224 && ! find_reg_note (insn, REG_NORETURN, NULL))
1226 unsigned regno;
1227 HARD_REG_SET hard_regs_to_save, used_regs;
1228 reg_set_iterator rsi;
1230 /* Use the register life information in CHAIN to compute which
1231 regs are live during the call. */
1232 REG_SET_TO_HARD_REG_SET (hard_regs_to_save,
1233 &chain->live_throughout);
1234 /* Save hard registers always in the widest mode available. */
1235 for (regno = 0; regno < FIRST_PSEUDO_REGISTER; regno++)
1236 if (TEST_HARD_REG_BIT (hard_regs_to_save, regno))
1238 CLEAR_HARD_REG_SET (this_insn_sets);
1239 note_stores (PATTERN (insn), mark_set_regs,
1240 &this_insn_sets);
1241 save_mode [regno] = regno_save_mode [regno] [1];
1243 else
1244 save_mode [regno] = VOIDmode;
1246 /* Look through all live pseudos, mark their hard registers
1247 and choose proper mode for saving. */
1248 EXECUTE_IF_SET_IN_REG_SET
1249 (&chain->live_throughout, FIRST_PSEUDO_REGISTER, regno, rsi)
1251 int r = reg_renumber[regno];
1252 int nregs;
1253 enum machine_mode mode;
1255 if (flag_ira && r < 0)
1256 continue;
1257 gcc_assert (r >= 0);
1258 nregs = hard_regno_nregs[r][PSEUDO_REGNO_MODE (regno)];
1259 mode = HARD_REGNO_CALLER_SAVE_MODE
1260 (r, nregs, PSEUDO_REGNO_MODE (regno));
1261 if (GET_MODE_BITSIZE (mode)
1262 > GET_MODE_BITSIZE (save_mode[r]))
1263 save_mode[r] = mode;
1264 while (nregs-- > 0)
1265 SET_HARD_REG_BIT (hard_regs_to_save, r + nregs);
1268 /* Record all registers set in this call insn. These don't need
1269 to be saved. N.B. the call insn might set a subreg of a
1270 multi-hard-reg pseudo; then the pseudo is considered live
1271 during the call, but the subreg that is set isn't. */
1272 CLEAR_HARD_REG_SET (this_insn_sets);
1273 note_stores (PATTERN (insn), mark_set_regs, &this_insn_sets);
1275 /* Compute which hard regs must be saved before this call. */
1276 AND_COMPL_HARD_REG_SET (hard_regs_to_save, call_fixed_reg_set);
1277 AND_COMPL_HARD_REG_SET (hard_regs_to_save, this_insn_sets);
1278 AND_COMPL_HARD_REG_SET (hard_regs_to_save, hard_regs_saved);
1279 get_call_invalidated_used_regs (insn, &used_regs, false);
1280 AND_HARD_REG_SET (hard_regs_to_save, used_regs);
1282 for (regno = 0; regno < FIRST_PSEUDO_REGISTER; regno++)
1283 if (TEST_HARD_REG_BIT (hard_regs_to_save, regno))
1284 regno += insert_save (chain, 1, regno, &hard_regs_to_save,
1285 save_mode);
1287 /* Must recompute n_regs_saved. */
1288 n_regs_saved = 0;
1289 for (regno = 0; regno < FIRST_PSEUDO_REGISTER; regno++)
1290 if (TEST_HARD_REG_BIT (hard_regs_saved, regno))
1291 n_regs_saved++;
1295 if (chain->next == 0 || chain->next->block != chain->block)
1297 int regno;
1298 struct bb_info *next_bb_info;
1300 next_bb_info = (chain->next != NULL
1301 ? BB_INFO_BY_INDEX (chain->next->block) : NULL);
1303 /* At the end of the basic block, we must restore any registers that
1304 remain saved. If the last insn in the block is a JUMP_INSN, put
1305 the restore before the insn, otherwise, put it after the insn. */
1307 if (flag_ira && flag_ira_move_spills)
1308 set_hard_reg_saved (BB_INFO_BY_INDEX (chain->block)->save_here,
1309 BB_INFO_BY_INDEX (chain->block)->save_out_mode,
1310 save_mode);
1312 if (n_regs_saved)
1313 for (regno = 0; regno < FIRST_PSEUDO_REGISTER; regno++)
1314 if (TEST_HARD_REG_BIT (hard_regs_saved, regno))
1316 int before = regno;
1318 regno += insert_restore (chain, JUMP_P (insn),
1319 regno, MOVE_MAX_WORDS, save_mode);
1320 if (flag_ira && flag_ira_move_spills)
1322 gcc_assert (before == regno);
1323 save_mode [before] = VOIDmode;
1327 if (flag_ira && flag_ira_move_spills && next_bb_info != NULL)
1328 set_hard_reg_saved (next_bb_info->save_in,
1329 next_bb_info->save_in_mode, save_mode);
1334 if (flag_ira && flag_ira_move_spills)
1335 free_aux_for_blocks ();
1338 /* Here from note_stores, or directly from save_call_clobbered_regs, when
1339 an insn stores a value in a register.
1340 Set the proper bit or bits in this_insn_sets. All pseudos that have
1341 been assigned hard regs have had their register number changed already,
1342 so we can ignore pseudos. */
1343 static void
1344 mark_set_regs (rtx reg, const_rtx setter ATTRIBUTE_UNUSED, void *data)
1346 int regno, endregno, i;
1347 HARD_REG_SET *this_insn_sets = data;
1349 if (GET_CODE (reg) == SUBREG)
1351 rtx inner = SUBREG_REG (reg);
1352 if (!REG_P (inner) || REGNO (inner) >= FIRST_PSEUDO_REGISTER)
1353 return;
1354 regno = subreg_regno (reg);
1355 endregno = regno + subreg_nregs (reg);
1357 else if (REG_P (reg)
1358 && REGNO (reg) < FIRST_PSEUDO_REGISTER)
1360 regno = REGNO (reg);
1361 endregno = END_HARD_REGNO (reg);
1363 else
1364 return;
1366 for (i = regno; i < endregno; i++)
1367 SET_HARD_REG_BIT (*this_insn_sets, i);
1370 /* Here from note_stores when an insn stores a value in a register.
1371 Set the proper bit or bits in the passed regset. All pseudos that have
1372 been assigned hard regs have had their register number changed already,
1373 so we can ignore pseudos. */
1374 static void
1375 add_stored_regs (rtx reg, const_rtx setter, void *data)
1377 int regno, endregno, i;
1378 enum machine_mode mode = GET_MODE (reg);
1379 int offset = 0;
1381 if (GET_CODE (setter) == CLOBBER)
1382 return;
1384 if (GET_CODE (reg) == SUBREG
1385 && REG_P (SUBREG_REG (reg))
1386 && REGNO (SUBREG_REG (reg)) < FIRST_PSEUDO_REGISTER)
1388 offset = subreg_regno_offset (REGNO (SUBREG_REG (reg)),
1389 GET_MODE (SUBREG_REG (reg)),
1390 SUBREG_BYTE (reg),
1391 GET_MODE (reg));
1392 regno = REGNO (SUBREG_REG (reg)) + offset;
1393 endregno = regno + subreg_nregs (reg);
1395 else
1397 if (!REG_P (reg) || REGNO (reg) >= FIRST_PSEUDO_REGISTER)
1398 return;
1400 regno = REGNO (reg) + offset;
1401 endregno = end_hard_regno (mode, regno);
1404 for (i = regno; i < endregno; i++)
1405 SET_REGNO_REG_SET ((regset) data, i);
1408 /* Walk X and record all referenced registers in REFERENCED_REGS. */
1409 static void
1410 mark_referenced_regs (rtx x)
1412 enum rtx_code code = GET_CODE (x);
1413 const char *fmt;
1414 int i, j;
1416 if (code == SET)
1417 mark_referenced_regs (SET_SRC (x));
1418 if (code == SET || code == CLOBBER)
1420 x = SET_DEST (x);
1421 code = GET_CODE (x);
1422 if ((code == REG && REGNO (x) < FIRST_PSEUDO_REGISTER)
1423 || code == PC || code == CC0
1424 || (code == SUBREG && REG_P (SUBREG_REG (x))
1425 && REGNO (SUBREG_REG (x)) < FIRST_PSEUDO_REGISTER
1426 /* If we're setting only part of a multi-word register,
1427 we shall mark it as referenced, because the words
1428 that are not being set should be restored. */
1429 && ((GET_MODE_SIZE (GET_MODE (x))
1430 >= GET_MODE_SIZE (GET_MODE (SUBREG_REG (x))))
1431 || (GET_MODE_SIZE (GET_MODE (SUBREG_REG (x)))
1432 <= UNITS_PER_WORD))))
1433 return;
1435 if (code == MEM || code == SUBREG)
1437 x = XEXP (x, 0);
1438 code = GET_CODE (x);
1441 if (code == REG)
1443 int regno = REGNO (x);
1444 int hardregno = (regno < FIRST_PSEUDO_REGISTER ? regno
1445 : reg_renumber[regno]);
1447 if (hardregno >= 0)
1448 add_to_hard_reg_set (&referenced_regs, GET_MODE (x), hardregno);
1449 /* If this is a pseudo that did not get a hard register, scan its
1450 memory location, since it might involve the use of another
1451 register, which might be saved. */
1452 else if (reg_equiv_mem[regno] != 0)
1453 mark_referenced_regs (XEXP (reg_equiv_mem[regno], 0));
1454 else if (reg_equiv_address[regno] != 0)
1455 mark_referenced_regs (reg_equiv_address[regno]);
1456 return;
1459 fmt = GET_RTX_FORMAT (code);
1460 for (i = GET_RTX_LENGTH (code) - 1; i >= 0; i--)
1462 if (fmt[i] == 'e')
1463 mark_referenced_regs (XEXP (x, i));
1464 else if (fmt[i] == 'E')
1465 for (j = XVECLEN (x, i) - 1; j >= 0; j--)
1466 mark_referenced_regs (XVECEXP (x, i, j));
1470 /* Insert a sequence of insns to restore. Place these insns in front of
1471 CHAIN if BEFORE_P is nonzero, behind the insn otherwise. MAXRESTORE is
1472 the maximum number of registers which should be restored during this call.
1473 It should never be less than 1 since we only work with entire registers.
1475 Note that we have verified in init_caller_save that we can do this
1476 with a simple SET, so use it. Set INSN_CODE to what we save there
1477 since the address might not be valid so the insn might not be recognized.
1478 These insns will be reloaded and have register elimination done by
1479 find_reload, so we need not worry about that here.
1481 Return the extra number of registers saved. */
1483 static int
1484 insert_restore (struct insn_chain *chain, int before_p, int regno,
1485 int maxrestore, enum machine_mode *save_mode)
1487 int i, k;
1488 rtx pat = NULL_RTX;
1489 int code;
1490 unsigned int numregs = 0;
1491 struct insn_chain *new;
1492 rtx mem;
1494 /* A common failure mode if register status is not correct in the
1495 RTL is for this routine to be called with a REGNO we didn't
1496 expect to save. That will cause us to write an insn with a (nil)
1497 SET_DEST or SET_SRC. Instead of doing so and causing a crash
1498 later, check for this common case here instead. This will remove
1499 one step in debugging such problems. */
1500 gcc_assert (regno_save_mem[regno][1]);
1502 /* Get the pattern to emit and update our status.
1504 See if we can restore `maxrestore' registers at once. Work
1505 backwards to the single register case. */
1506 for (i = maxrestore; i > 0; i--)
1508 int j;
1509 int ok = 1;
1511 if (regno_save_mem[regno][i] == 0)
1512 continue;
1514 for (j = 0; j < i; j++)
1515 if (! TEST_HARD_REG_BIT (hard_regs_saved, regno + j))
1517 ok = 0;
1518 break;
1520 /* Must do this one restore at a time. */
1521 if (! ok)
1522 continue;
1524 numregs = i;
1525 break;
1528 mem = regno_save_mem [regno][numregs];
1529 if (save_mode [regno] != VOIDmode
1530 && save_mode [regno] != GET_MODE (mem)
1531 && numregs == (unsigned int) hard_regno_nregs[regno][save_mode [regno]]
1532 && reg_save_code (regno, save_mode[regno]) >= 0)
1533 mem = adjust_address_nv (mem, save_mode[regno], 0);
1534 else
1535 mem = copy_rtx (mem);
1536 pat = gen_rtx_SET (VOIDmode,
1537 gen_rtx_REG (GET_MODE (mem),
1538 regno), mem);
1539 code = reg_restore_code (regno, GET_MODE (mem));
1540 new = insert_one_insn (chain, before_p, code, pat);
1542 /* Clear status for all registers we restored. */
1543 for (k = 0; k < i; k++)
1545 CLEAR_HARD_REG_BIT (hard_regs_saved, regno + k);
1546 SET_REGNO_REG_SET (&new->dead_or_set, regno + k);
1547 n_regs_saved--;
1550 /* Tell our callers how many extra registers we saved/restored. */
1551 return numregs - 1;
1554 /* Like insert_restore above, but save registers instead. */
1556 static int
1557 insert_save (struct insn_chain *chain, int before_p, int regno,
1558 HARD_REG_SET (*to_save), enum machine_mode *save_mode)
1560 int i;
1561 unsigned int k;
1562 rtx pat = NULL_RTX;
1563 int code;
1564 unsigned int numregs = 0;
1565 struct insn_chain *new;
1566 rtx mem;
1568 /* A common failure mode if register status is not correct in the
1569 RTL is for this routine to be called with a REGNO we didn't
1570 expect to save. That will cause us to write an insn with a (nil)
1571 SET_DEST or SET_SRC. Instead of doing so and causing a crash
1572 later, check for this common case here. This will remove one
1573 step in debugging such problems. */
1574 gcc_assert (regno_save_mem[regno][1]);
1576 /* Get the pattern to emit and update our status.
1578 See if we can save several registers with a single instruction.
1579 Work backwards to the single register case. */
1580 for (i = MOVE_MAX_WORDS; i > 0; i--)
1582 int j;
1583 int ok = 1;
1584 if (regno_save_mem[regno][i] == 0)
1585 continue;
1587 for (j = 0; j < i; j++)
1588 if (! TEST_HARD_REG_BIT (*to_save, regno + j))
1590 ok = 0;
1591 break;
1593 /* Must do this one save at a time. */
1594 if (! ok)
1595 continue;
1597 numregs = i;
1598 break;
1601 mem = regno_save_mem [regno][numregs];
1602 if (save_mode [regno] != VOIDmode
1603 && save_mode [regno] != GET_MODE (mem)
1604 && numregs == (unsigned int) hard_regno_nregs[regno][save_mode [regno]]
1605 && reg_save_code (regno, save_mode[regno]) >= 0)
1606 mem = adjust_address_nv (mem, save_mode[regno], 0);
1607 else
1608 mem = copy_rtx (mem);
1609 pat = gen_rtx_SET (VOIDmode, mem,
1610 gen_rtx_REG (GET_MODE (mem),
1611 regno));
1612 code = reg_save_code (regno, GET_MODE (mem));
1613 new = insert_one_insn (chain, before_p, code, pat);
1615 /* Set hard_regs_saved and dead_or_set for all the registers we saved. */
1616 for (k = 0; k < numregs; k++)
1618 SET_HARD_REG_BIT (hard_regs_saved, regno + k);
1619 SET_REGNO_REG_SET (&new->dead_or_set, regno + k);
1620 n_regs_saved++;
1623 /* Tell our callers how many extra registers we saved/restored. */
1624 return numregs - 1;
1627 /* Emit a new caller-save insn and set the code. */
1628 static struct insn_chain *
1629 insert_one_insn (struct insn_chain *chain, int before_p, int code, rtx pat)
1631 rtx insn = chain->insn;
1632 struct insn_chain *new;
1634 #ifdef HAVE_cc0
1635 /* If INSN references CC0, put our insns in front of the insn that sets
1636 CC0. This is always safe, since the only way we could be passed an
1637 insn that references CC0 is for a restore, and doing a restore earlier
1638 isn't a problem. We do, however, assume here that CALL_INSNs don't
1639 reference CC0. Guard against non-INSN's like CODE_LABEL. */
1641 if ((NONJUMP_INSN_P (insn) || JUMP_P (insn))
1642 && before_p
1643 && reg_referenced_p (cc0_rtx, PATTERN (insn)))
1644 chain = chain->prev, insn = chain->insn;
1645 #endif
1647 new = new_insn_chain ();
1648 if (before_p)
1650 rtx link;
1652 new->prev = chain->prev;
1653 if (new->prev != 0)
1654 new->prev->next = new;
1655 else
1656 reload_insn_chain = new;
1658 chain->prev = new;
1659 new->next = chain;
1660 new->insn = emit_insn_before (pat, insn);
1661 /* ??? It would be nice if we could exclude the already / still saved
1662 registers from the live sets. */
1663 COPY_REG_SET (&new->live_throughout, &chain->live_throughout);
1664 /* Registers that die in CHAIN->INSN still live in the new insn. */
1665 for (link = REG_NOTES (chain->insn); link; link = XEXP (link, 1))
1667 if (REG_NOTE_KIND (link) == REG_DEAD)
1669 rtx reg = XEXP (link, 0);
1670 int regno, i;
1672 gcc_assert (REG_P (reg));
1673 regno = REGNO (reg);
1674 if (regno >= FIRST_PSEUDO_REGISTER)
1675 regno = reg_renumber[regno];
1676 if (regno < 0)
1677 continue;
1678 for (i = hard_regno_nregs[regno][GET_MODE (reg)] - 1;
1679 i >= 0; i--)
1680 SET_REGNO_REG_SET (&new->live_throughout, regno + i);
1684 /* If CHAIN->INSN is a call, then the registers which contain
1685 the arguments to the function are live in the new insn. */
1686 if (CALL_P (chain->insn))
1688 for (link = CALL_INSN_FUNCTION_USAGE (chain->insn);
1689 link != NULL_RTX;
1690 link = XEXP (link, 1))
1692 rtx arg = XEXP (link, 0);
1694 if (GET_CODE (arg) == USE)
1696 rtx reg = XEXP (arg, 0);
1698 if (REG_P (reg))
1700 int i, regno = REGNO (reg);
1702 /* Registers in CALL_INSN_FUNCTION_USAGE are always
1703 hard registers. */
1704 gcc_assert (regno < FIRST_PSEUDO_REGISTER);
1706 for (i = hard_regno_nregs[regno][GET_MODE (reg)] - 1;
1707 i >= 0; i--)
1708 SET_REGNO_REG_SET (&new->live_throughout, regno + i);
1715 CLEAR_REG_SET (&new->dead_or_set);
1716 if (chain->insn == BB_HEAD (BASIC_BLOCK (chain->block)))
1717 BB_HEAD (BASIC_BLOCK (chain->block)) = new->insn;
1719 else
1721 new->next = chain->next;
1722 if (new->next != 0)
1723 new->next->prev = new;
1724 chain->next = new;
1725 new->prev = chain;
1726 if (GET_CODE (insn) != CODE_LABEL)
1727 new->insn = emit_insn_after (pat, insn);
1728 else
1730 /* Put the insn after bb note in a empty basic block. */
1731 gcc_assert (NEXT_INSN (insn) && NOTE_P (NEXT_INSN (insn)));
1732 new->insn = emit_insn_after (pat, NEXT_INSN (insn));
1734 /* ??? It would be nice if we could exclude the already / still saved
1735 registers from the live sets, and observe REG_UNUSED notes. */
1736 COPY_REG_SET (&new->live_throughout, &chain->live_throughout);
1737 /* Registers that are set in CHAIN->INSN live in the new insn.
1738 (Unless there is a REG_UNUSED note for them, but we don't
1739 look for them here.) */
1740 if (INSN_P (chain->insn))
1741 note_stores (PATTERN (chain->insn), add_stored_regs,
1742 &new->live_throughout);
1743 CLEAR_REG_SET (&new->dead_or_set);
1744 if (chain->insn == BB_END (BASIC_BLOCK (chain->block)))
1745 BB_END (BASIC_BLOCK (chain->block)) = new->insn;
1747 new->block = chain->block;
1748 new->is_caller_save_insn = 1;
1750 INSN_CODE (new->insn) = code;
1751 return new;
1753 #include "gt-caller-save.h"