2007-12-17 Vladimir Makarov <vmakarov@redhat.com>
[official-gcc.git] / gcc / caller-save.c
blobe2ec2fd874118f926de012b9f9885263cc42ff12
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 gcc_assert (r >= 0);
798 nregs = hard_regno_nregs[r][PSEUDO_REGNO_MODE (regno)];
799 mode = HARD_REGNO_CALLER_SAVE_MODE
800 (r, nregs, PSEUDO_REGNO_MODE (regno));
801 if (nregs == 1)
803 SET_HARD_REG_BIT (hard_regs_to_save, r);
804 save_mode[r] = mode;
806 else
808 while (nregs-- > 0)
810 SET_HARD_REG_BIT (hard_regs_to_save, r + nregs);
811 save_mode [r + nregs]
812 = regno_save_mode [r + nregs] [1];
817 /* Record all registers set in this call insn. These
818 don't need to be saved. N.B. the call insn might set
819 a subreg of a multi-hard-reg pseudo; then the pseudo
820 is considered live during the call, but the subreg
821 that is set isn't. */
822 CLEAR_HARD_REG_SET (this_insn_sets);
823 note_stores (PATTERN (insn), mark_set_regs, &this_insn_sets);
824 /* Sibcalls are considered to set the return value,
825 compare flow.c:propagate_one_insn. */
826 if (SIBLING_CALL_P (insn) && current_function_return_rtx)
827 mark_set_regs (current_function_return_rtx, NULL_RTX,
828 &this_insn_sets);
830 /* Compute which hard regs must be saved before this call. */
831 AND_COMPL_HARD_REG_SET (hard_regs_to_save, call_fixed_reg_set);
832 AND_COMPL_HARD_REG_SET (hard_regs_to_save, this_insn_sets);
833 get_call_invalidated_used_regs (insn, &used_regs, false);
834 AND_HARD_REG_SET (hard_regs_to_save, used_regs);
835 IOR_HARD_REG_SET (gen, hard_regs_to_save);
839 if (chain->next == 0 || chain->next->block != chain->block)
841 basic_block bb = BASIC_BLOCK (chain->block);
843 bb_info->empty_save_in_p = empty_save_in_p;
844 empty_save_in_p = FALSE;
845 CLEAR_HARD_REG_SET (bb_info->save_in);
846 COPY_HARD_REG_SET (bb_info->gen, gen);
847 for (i = 0; i < FIRST_PSEUDO_REGISTER; i++)
849 bb_info->save_in_mode [i] = VOIDmode;
850 if (TEST_HARD_REG_BIT (gen, i))
851 bb_info->save_out_mode [i] = save_mode [i];
852 else
853 bb_info->save_out_mode [i] = VOIDmode;
855 COPY_HARD_REG_SET (bb_info->kill, kill);
856 SET_HARD_REG_SET (bb_info->save_out);
857 REG_SET_TO_HARD_REG_SET
858 (bb_info->live_at_start,
859 flag_ira ? DF_LR_TOP (bb) : DF_RA_LIVE_TOP (bb));
860 EXECUTE_IF_SET_IN_REG_SET
861 ((flag_ira ? DF_LR_TOP (bb) : DF_RA_LIVE_TOP (bb)),
862 FIRST_PSEUDO_REGISTER, regno, rsi)
864 int r = reg_renumber[regno];
865 int nregs;
867 if (r < 0)
868 continue;
869 nregs = hard_regno_nregs[r][PSEUDO_REGNO_MODE (regno)];
870 while (nregs-- > 0)
871 SET_HARD_REG_BIT (bb_info->live_at_start, r + nregs);
873 CLEAR_HARD_REG_SET (bb_info->save_here);
874 CLEAR_HARD_REG_SET (gen);
875 CLEAR_HARD_REG_SET (kill);
876 for (i = 0; i < FIRST_PSEUDO_REGISTER; i++)
877 save_mode [i] = VOIDmode;
882 /* The function sets up reverse post-order number of each basic
883 block. */
884 static void
885 set_up_bb_rts_numbers (void)
887 int i;
888 int *rts_order;
890 rts_order = XNEWVEC (int, n_basic_blocks - NUM_FIXED_BLOCKS);
891 post_order_compute (rts_order, false, false);
892 for (i = 0; i < n_basic_blocks - NUM_FIXED_BLOCKS; i++)
893 BB_INFO_BY_INDEX (rts_order [i])->rts_number = i;
894 free (rts_order);
897 /* Compare function for sorting blocks in reverse postorder. */
898 static int
899 rpost_cmp (const void *bb1, const void *bb2)
901 basic_block b1 = *(basic_block *) bb1, b2 = *(basic_block *) bb2;
903 return BB_INFO (b2)->rts_number - BB_INFO (b1)->rts_number;
906 /* The function calculates global save information according
907 to the following equations:
909 bb.save_in = empty for entry block or one with empty_save_in_p
910 | ^ (pred.save_out - pred.save_here) ^ bb.live_at_start
911 bb.save_out = (bb.save_in - bb.kill) U bb.gen.
913 See function calculate_save_here to know how SAVE_HERE is
914 calculated */
915 static void
916 calculate_save_in_out (void)
918 basic_block bb, succ;
919 edge e;
920 int i, j, nel;
921 VEC(basic_block,heap) *bbs, *new_bbs, *temp;
922 basic_block *bb_array;
923 HARD_REG_SET temp_set;
924 sbitmap wset;
926 bbs = VEC_alloc (basic_block, heap, last_basic_block);
927 new_bbs = VEC_alloc (basic_block, heap, last_basic_block);
928 FOR_EACH_BB (bb)
930 VEC_quick_push (basic_block, bbs, bb);
932 wset = sbitmap_alloc (last_basic_block);
933 while (VEC_length (basic_block, bbs))
935 bb_array = VEC_address (basic_block, bbs);
936 nel = VEC_length (basic_block, bbs);
937 qsort (bb_array, nel, sizeof (basic_block), rpost_cmp);
938 sbitmap_zero (wset);
939 for (i = 0; i < nel; i++)
941 int first_p;
942 edge_iterator ei;
943 struct bb_info *bb_info;
945 bb = bb_array [i];
946 bb_info = BB_INFO (bb);
947 first_p = TRUE;
948 if (! bb_info->empty_save_in_p)
949 FOR_EACH_EDGE (e, ei, bb->preds)
951 basic_block pred = e->src;
953 if (e->flags & EDGE_ABNORMAL)
955 CLEAR_HARD_REG_SET (bb_info->save_in);
956 break;
958 if (pred->index != ENTRY_BLOCK)
960 COPY_HARD_REG_SET (temp_set, BB_INFO (pred)->save_out);
961 AND_COMPL_HARD_REG_SET (temp_set,
962 BB_INFO (pred)->save_here);
963 AND_HARD_REG_SET (temp_set, bb_info->live_at_start);
964 if (first_p)
965 COPY_HARD_REG_SET (bb_info->save_in, temp_set);
966 else
967 AND_HARD_REG_SET (bb_info->save_in, temp_set);
968 for (j = 0; j < FIRST_PSEUDO_REGISTER; j++)
969 if (TEST_HARD_REG_BIT (temp_set, j))
971 gcc_assert
972 (bb_info->save_in_mode [j] == VOIDmode
973 || BB_INFO (pred)->save_out_mode [j] == VOIDmode
974 || (bb_info->save_in_mode [j]
975 == BB_INFO (pred)->save_out_mode [j]));
976 if (BB_INFO (pred)->save_out_mode [j] != VOIDmode)
977 bb_info->save_in_mode [j]
978 = BB_INFO (pred)->save_out_mode [j];
980 first_p = FALSE;
983 COPY_HARD_REG_SET (temp_set, bb_info->save_in);
984 AND_COMPL_HARD_REG_SET (temp_set, bb_info->kill);
985 for (j = 0; j < FIRST_PSEUDO_REGISTER; j++)
986 if (TEST_HARD_REG_BIT (temp_set, j)
987 && ! TEST_HARD_REG_BIT (bb_info->gen, j))
989 gcc_assert (bb_info->save_out_mode [j] == VOIDmode
990 || (bb_info->save_out_mode [j]
991 == bb_info->save_in_mode [j]));
992 bb_info->save_out_mode [j] = bb_info->save_in_mode [j];
994 IOR_HARD_REG_SET (temp_set, bb_info->gen);
996 if (! hard_reg_set_equal_p (temp_set, bb_info->save_out))
998 COPY_HARD_REG_SET (bb_info->save_out, temp_set);
999 FOR_EACH_EDGE (e, ei, bb->succs)
1001 succ = e->dest;
1002 if (succ->index != EXIT_BLOCK
1003 && ! TEST_BIT (wset, succ->index))
1005 SET_BIT (wset, succ->index);
1006 VEC_quick_push (basic_block, new_bbs, succ);
1011 temp = bbs;
1012 bbs = new_bbs;
1013 new_bbs = temp;
1014 VEC_truncate (basic_block, new_bbs, 0);
1016 sbitmap_free (wset);
1017 VEC_free (basic_block, heap, new_bbs);
1018 VEC_free (basic_block, heap, bbs);
1021 /* The function calculates SAVE_HERE according to the equation
1022 bb.save_here = U ((bb.save_out - succ.save_in) ^ succ.live_at_start). */
1023 static int
1024 calculate_save_here (void)
1026 basic_block bb;
1027 edge e;
1028 edge_iterator ei;
1029 HARD_REG_SET save_here, temp_set;
1030 int changed_p = FALSE;
1032 FOR_EACH_BB (bb)
1034 CLEAR_HARD_REG_SET (save_here);
1035 FOR_EACH_EDGE (e, ei, bb->succs)
1037 basic_block dest = e->dest;
1039 COPY_HARD_REG_SET (temp_set, BB_INFO (bb)->save_out);
1040 AND_COMPL_HARD_REG_SET (temp_set, BB_INFO (dest)->save_in);
1041 AND_HARD_REG_SET (temp_set, BB_INFO (dest)->live_at_start);
1042 IOR_HARD_REG_SET (save_here, temp_set);
1045 if (! hard_reg_set_equal_p (save_here, BB_INFO (bb)->save_here))
1047 COPY_HARD_REG_SET (BB_INFO (bb)->save_here, save_here);
1048 changed_p = TRUE;
1051 return changed_p;
1054 /* The function calculates global save information used to put
1055 save/restore code without generating new blocks. This is a
1056 bidirectional data flow problem (calculation of SAVE_IN and
1057 SAVE_OUT is a forward data flow problem and SAVE_HERE is a backward
1058 one). It is complicated by calculation of modes for
1059 saving/restoring call used hard registers. */
1060 static void
1061 make_global_save_analysis (void)
1063 int iter, changed_p;
1065 calculate_local_save_info ();
1066 set_up_bb_rts_numbers ();
1067 for (iter = 1;; iter++)
1069 calculate_save_in_out ();
1070 changed_p = calculate_save_here ();
1071 if (! changed_p)
1072 break;
1074 if (dump_file != NULL)
1075 fprintf (dump_file, " Number of global save analysis iterations %d\n",
1076 iter);
1079 /* Print hard registers in SET to file F. The registers are printed
1080 with its mode given in MODES. */
1081 static void
1082 print_hard_reg_set_and_mode (FILE *f, HARD_REG_SET set, unsigned char *modes)
1084 int i;
1086 for (i = 0; i < FIRST_PSEUDO_REGISTER; i++)
1087 if (TEST_HARD_REG_BIT (set, i))
1088 fprintf (f, " %d:%s %s", i, GET_MODE_NAME (modes [i]), reg_names [i]);
1091 /* Print hard registers in SET to file F. */
1092 static void
1093 print_hard_reg_set (FILE *f, HARD_REG_SET set)
1095 int i;
1097 for (i = 0; i < FIRST_PSEUDO_REGISTER; i++)
1098 if (TEST_HARD_REG_BIT (set, i))
1099 fprintf (f, " %d %s", i, reg_names [i]);
1102 /* Print save information for each block to file F. */
1103 static void
1104 print_save_data (FILE *f)
1106 basic_block bb;
1107 struct bb_info *bb_info;
1109 FOR_EACH_BB (bb)
1111 bb_info = BB_INFO (bb);
1112 fprintf (f, "bb %d:\n save_in:", bb->index);
1113 print_hard_reg_set_and_mode (f, bb_info->save_in,
1114 bb_info->save_in_mode);
1115 fprintf (f, "\n save_out:");
1116 print_hard_reg_set_and_mode (f, bb_info->save_out,
1117 bb_info->save_out_mode);
1118 fprintf (f, "\n live_at_start:");
1119 print_hard_reg_set (f, bb_info->live_at_start);
1120 fprintf (f, "\n save_here:");
1121 print_hard_reg_set (f, bb_info->save_here);
1122 fprintf (f, "\n kill:");
1123 print_hard_reg_set (f, bb_info->kill);
1124 fprintf (f, "\n gen:");
1125 print_hard_reg_set (f, bb_info->gen);
1126 fprintf (f, "\n\n");
1130 /* Print save information for each block to stderr. */
1131 void
1132 debug_save_data (void)
1134 print_save_data (stderr);
1137 /* Setup hard registers in SET to save. Setup also their save modes
1138 in SAVE_MODE from FROM_SAVE_MODE. */
1139 static void
1140 set_hard_reg_saved (HARD_REG_SET set, unsigned char *from_saved_mode,
1141 enum machine_mode *save_mode)
1143 int regno;
1145 n_regs_saved = 0;
1146 COPY_HARD_REG_SET (hard_regs_saved, set);
1147 for (regno = 0; regno < FIRST_PSEUDO_REGISTER; regno++)
1148 if (TEST_HARD_REG_BIT (set, regno))
1150 n_regs_saved++;
1151 save_mode [regno] = from_saved_mode [regno];
1153 else
1154 save_mode [regno] = VOIDmode;
1157 /* Find the places where hard regs are live across calls and save them. */
1159 void
1160 save_call_clobbered_regs (void)
1162 /* Computed in mark_set_regs, holds all registers set by the current
1163 instruction. */
1164 HARD_REG_SET this_insn_sets;
1165 struct insn_chain *chain, *next;
1166 enum machine_mode save_mode [FIRST_PSEUDO_REGISTER];
1168 if (flag_ira && flag_ira_move_spills)
1170 alloc_aux_for_blocks (sizeof (struct bb_info));
1171 make_global_save_analysis ();
1174 CLEAR_HARD_REG_SET (hard_regs_saved);
1175 n_regs_saved = 0;
1177 for (chain = reload_insn_chain; chain != 0; chain = next)
1179 rtx insn = chain->insn;
1180 enum rtx_code code = GET_CODE (insn);
1182 next = chain->next;
1184 gcc_assert (!chain->is_caller_save_insn);
1186 if (INSN_P (insn))
1188 /* If some registers have been saved, see if INSN references
1189 any of them. We must restore them before the insn if so. */
1191 if (n_regs_saved)
1193 int regno;
1195 if (code == JUMP_INSN)
1196 /* Restore all registers if this is a JUMP_INSN. */
1197 COPY_HARD_REG_SET (referenced_regs, hard_regs_saved);
1198 else
1200 CLEAR_HARD_REG_SET (referenced_regs);
1201 mark_referenced_regs (PATTERN (insn));
1202 AND_HARD_REG_SET (referenced_regs, hard_regs_saved);
1205 for (regno = 0; regno < FIRST_PSEUDO_REGISTER; regno++)
1206 if (TEST_HARD_REG_BIT (referenced_regs, regno))
1208 int before = regno;
1210 regno += insert_restore (chain, 1, regno, MOVE_MAX_WORDS,
1211 save_mode);
1212 if (flag_ira && flag_ira_move_spills)
1214 gcc_assert (before == regno);
1215 save_mode [before] = VOIDmode;
1220 if (code == CALL_INSN
1221 && ! SIBLING_CALL_P (insn)
1222 && ! find_reg_note (insn, REG_NORETURN, NULL))
1224 unsigned regno;
1225 HARD_REG_SET hard_regs_to_save, used_regs;
1226 reg_set_iterator rsi;
1228 /* Use the register life information in CHAIN to compute which
1229 regs are live during the call. */
1230 REG_SET_TO_HARD_REG_SET (hard_regs_to_save,
1231 &chain->live_throughout);
1232 /* Save hard registers always in the widest mode available. */
1233 for (regno = 0; regno < FIRST_PSEUDO_REGISTER; regno++)
1234 if (TEST_HARD_REG_BIT (hard_regs_to_save, regno))
1236 CLEAR_HARD_REG_SET (this_insn_sets);
1237 note_stores (PATTERN (insn), mark_set_regs,
1238 &this_insn_sets);
1239 save_mode [regno] = regno_save_mode [regno] [1];
1241 else
1242 save_mode [regno] = VOIDmode;
1244 /* Look through all live pseudos, mark their hard registers
1245 and choose proper mode for saving. */
1246 EXECUTE_IF_SET_IN_REG_SET
1247 (&chain->live_throughout, FIRST_PSEUDO_REGISTER, regno, rsi)
1249 int r = reg_renumber[regno];
1250 int nregs;
1251 enum machine_mode mode;
1253 gcc_assert (r >= 0);
1254 nregs = hard_regno_nregs[r][PSEUDO_REGNO_MODE (regno)];
1255 mode = HARD_REGNO_CALLER_SAVE_MODE
1256 (r, nregs, PSEUDO_REGNO_MODE (regno));
1257 if (GET_MODE_BITSIZE (mode)
1258 > GET_MODE_BITSIZE (save_mode[r]))
1259 save_mode[r] = mode;
1260 while (nregs-- > 0)
1261 SET_HARD_REG_BIT (hard_regs_to_save, r + nregs);
1264 /* Record all registers set in this call insn. These don't need
1265 to be saved. N.B. the call insn might set a subreg of a
1266 multi-hard-reg pseudo; then the pseudo is considered live
1267 during the call, but the subreg that is set isn't. */
1268 CLEAR_HARD_REG_SET (this_insn_sets);
1269 note_stores (PATTERN (insn), mark_set_regs, &this_insn_sets);
1271 /* Compute which hard regs must be saved before this call. */
1272 AND_COMPL_HARD_REG_SET (hard_regs_to_save, call_fixed_reg_set);
1273 AND_COMPL_HARD_REG_SET (hard_regs_to_save, this_insn_sets);
1274 AND_COMPL_HARD_REG_SET (hard_regs_to_save, hard_regs_saved);
1275 get_call_invalidated_used_regs (insn, &used_regs, false);
1276 AND_HARD_REG_SET (hard_regs_to_save, used_regs);
1278 for (regno = 0; regno < FIRST_PSEUDO_REGISTER; regno++)
1279 if (TEST_HARD_REG_BIT (hard_regs_to_save, regno))
1280 regno += insert_save (chain, 1, regno, &hard_regs_to_save,
1281 save_mode);
1283 /* Must recompute n_regs_saved. */
1284 n_regs_saved = 0;
1285 for (regno = 0; regno < FIRST_PSEUDO_REGISTER; regno++)
1286 if (TEST_HARD_REG_BIT (hard_regs_saved, regno))
1287 n_regs_saved++;
1291 if (chain->next == 0 || chain->next->block != chain->block)
1293 int regno;
1294 struct bb_info *next_bb_info;
1296 next_bb_info = (chain->next != NULL
1297 ? BB_INFO_BY_INDEX (chain->next->block) : NULL);
1299 /* At the end of the basic block, we must restore any registers that
1300 remain saved. If the last insn in the block is a JUMP_INSN, put
1301 the restore before the insn, otherwise, put it after the insn. */
1303 if (flag_ira && flag_ira_move_spills)
1304 set_hard_reg_saved (BB_INFO_BY_INDEX (chain->block)->save_here,
1305 BB_INFO_BY_INDEX (chain->block)->save_out_mode,
1306 save_mode);
1308 if (n_regs_saved)
1309 for (regno = 0; regno < FIRST_PSEUDO_REGISTER; regno++)
1310 if (TEST_HARD_REG_BIT (hard_regs_saved, regno))
1312 int before = regno;
1314 regno += insert_restore (chain, JUMP_P (insn),
1315 regno, MOVE_MAX_WORDS, save_mode);
1316 if (flag_ira && flag_ira_move_spills)
1318 gcc_assert (before == regno);
1319 save_mode [before] = VOIDmode;
1323 if (flag_ira && flag_ira_move_spills && next_bb_info != NULL)
1324 set_hard_reg_saved (next_bb_info->save_in,
1325 next_bb_info->save_in_mode, save_mode);
1330 if (flag_ira && flag_ira_move_spills)
1331 free_aux_for_blocks ();
1334 /* Here from note_stores, or directly from save_call_clobbered_regs, when
1335 an insn stores a value in a register.
1336 Set the proper bit or bits in this_insn_sets. All pseudos that have
1337 been assigned hard regs have had their register number changed already,
1338 so we can ignore pseudos. */
1339 static void
1340 mark_set_regs (rtx reg, const_rtx setter ATTRIBUTE_UNUSED, void *data)
1342 int regno, endregno, i;
1343 HARD_REG_SET *this_insn_sets = data;
1345 if (GET_CODE (reg) == SUBREG)
1347 rtx inner = SUBREG_REG (reg);
1348 if (!REG_P (inner) || REGNO (inner) >= FIRST_PSEUDO_REGISTER)
1349 return;
1350 regno = subreg_regno (reg);
1351 endregno = regno + subreg_nregs (reg);
1353 else if (REG_P (reg)
1354 && REGNO (reg) < FIRST_PSEUDO_REGISTER)
1356 regno = REGNO (reg);
1357 endregno = END_HARD_REGNO (reg);
1359 else
1360 return;
1362 for (i = regno; i < endregno; i++)
1363 SET_HARD_REG_BIT (*this_insn_sets, i);
1366 /* Here from note_stores when an insn stores a value in a register.
1367 Set the proper bit or bits in the passed regset. All pseudos that have
1368 been assigned hard regs have had their register number changed already,
1369 so we can ignore pseudos. */
1370 static void
1371 add_stored_regs (rtx reg, const_rtx setter, void *data)
1373 int regno, endregno, i;
1374 enum machine_mode mode = GET_MODE (reg);
1375 int offset = 0;
1377 if (GET_CODE (setter) == CLOBBER)
1378 return;
1380 if (GET_CODE (reg) == SUBREG
1381 && REG_P (SUBREG_REG (reg))
1382 && REGNO (SUBREG_REG (reg)) < FIRST_PSEUDO_REGISTER)
1384 offset = subreg_regno_offset (REGNO (SUBREG_REG (reg)),
1385 GET_MODE (SUBREG_REG (reg)),
1386 SUBREG_BYTE (reg),
1387 GET_MODE (reg));
1388 regno = REGNO (SUBREG_REG (reg)) + offset;
1389 endregno = regno + subreg_nregs (reg);
1391 else
1393 if (!REG_P (reg) || REGNO (reg) >= FIRST_PSEUDO_REGISTER)
1394 return;
1396 regno = REGNO (reg) + offset;
1397 endregno = end_hard_regno (mode, regno);
1400 for (i = regno; i < endregno; i++)
1401 SET_REGNO_REG_SET ((regset) data, i);
1404 /* Walk X and record all referenced registers in REFERENCED_REGS. */
1405 static void
1406 mark_referenced_regs (rtx x)
1408 enum rtx_code code = GET_CODE (x);
1409 const char *fmt;
1410 int i, j;
1412 if (code == SET)
1413 mark_referenced_regs (SET_SRC (x));
1414 if (code == SET || code == CLOBBER)
1416 x = SET_DEST (x);
1417 code = GET_CODE (x);
1418 if ((code == REG && REGNO (x) < FIRST_PSEUDO_REGISTER)
1419 || code == PC || code == CC0
1420 || (code == SUBREG && REG_P (SUBREG_REG (x))
1421 && REGNO (SUBREG_REG (x)) < FIRST_PSEUDO_REGISTER
1422 /* If we're setting only part of a multi-word register,
1423 we shall mark it as referenced, because the words
1424 that are not being set should be restored. */
1425 && ((GET_MODE_SIZE (GET_MODE (x))
1426 >= GET_MODE_SIZE (GET_MODE (SUBREG_REG (x))))
1427 || (GET_MODE_SIZE (GET_MODE (SUBREG_REG (x)))
1428 <= UNITS_PER_WORD))))
1429 return;
1431 if (code == MEM || code == SUBREG)
1433 x = XEXP (x, 0);
1434 code = GET_CODE (x);
1437 if (code == REG)
1439 int regno = REGNO (x);
1440 int hardregno = (regno < FIRST_PSEUDO_REGISTER ? regno
1441 : reg_renumber[regno]);
1443 if (hardregno >= 0)
1444 add_to_hard_reg_set (&referenced_regs, GET_MODE (x), hardregno);
1445 /* If this is a pseudo that did not get a hard register, scan its
1446 memory location, since it might involve the use of another
1447 register, which might be saved. */
1448 else if (reg_equiv_mem[regno] != 0)
1449 mark_referenced_regs (XEXP (reg_equiv_mem[regno], 0));
1450 else if (reg_equiv_address[regno] != 0)
1451 mark_referenced_regs (reg_equiv_address[regno]);
1452 return;
1455 fmt = GET_RTX_FORMAT (code);
1456 for (i = GET_RTX_LENGTH (code) - 1; i >= 0; i--)
1458 if (fmt[i] == 'e')
1459 mark_referenced_regs (XEXP (x, i));
1460 else if (fmt[i] == 'E')
1461 for (j = XVECLEN (x, i) - 1; j >= 0; j--)
1462 mark_referenced_regs (XVECEXP (x, i, j));
1466 /* Insert a sequence of insns to restore. Place these insns in front of
1467 CHAIN if BEFORE_P is nonzero, behind the insn otherwise. MAXRESTORE is
1468 the maximum number of registers which should be restored during this call.
1469 It should never be less than 1 since we only work with entire registers.
1471 Note that we have verified in init_caller_save that we can do this
1472 with a simple SET, so use it. Set INSN_CODE to what we save there
1473 since the address might not be valid so the insn might not be recognized.
1474 These insns will be reloaded and have register elimination done by
1475 find_reload, so we need not worry about that here.
1477 Return the extra number of registers saved. */
1479 static int
1480 insert_restore (struct insn_chain *chain, int before_p, int regno,
1481 int maxrestore, enum machine_mode *save_mode)
1483 int i, k;
1484 rtx pat = NULL_RTX;
1485 int code;
1486 unsigned int numregs = 0;
1487 struct insn_chain *new;
1488 rtx mem;
1490 /* A common failure mode if register status is not correct in the
1491 RTL is for this routine to be called with a REGNO we didn't
1492 expect to save. That will cause us to write an insn with a (nil)
1493 SET_DEST or SET_SRC. Instead of doing so and causing a crash
1494 later, check for this common case here instead. This will remove
1495 one step in debugging such problems. */
1496 gcc_assert (regno_save_mem[regno][1]);
1498 /* Get the pattern to emit and update our status.
1500 See if we can restore `maxrestore' registers at once. Work
1501 backwards to the single register case. */
1502 for (i = maxrestore; i > 0; i--)
1504 int j;
1505 int ok = 1;
1507 if (regno_save_mem[regno][i] == 0)
1508 continue;
1510 for (j = 0; j < i; j++)
1511 if (! TEST_HARD_REG_BIT (hard_regs_saved, regno + j))
1513 ok = 0;
1514 break;
1516 /* Must do this one restore at a time. */
1517 if (! ok)
1518 continue;
1520 numregs = i;
1521 break;
1524 mem = regno_save_mem [regno][numregs];
1525 if (save_mode [regno] != VOIDmode
1526 && save_mode [regno] != GET_MODE (mem)
1527 && numregs == (unsigned int) hard_regno_nregs[regno][save_mode [regno]]
1528 && reg_save_code (regno, save_mode[regno]) >= 0)
1529 mem = adjust_address_nv (mem, save_mode[regno], 0);
1530 else
1531 mem = copy_rtx (mem);
1532 pat = gen_rtx_SET (VOIDmode,
1533 gen_rtx_REG (GET_MODE (mem),
1534 regno), mem);
1535 code = reg_restore_code (regno, GET_MODE (mem));
1536 new = insert_one_insn (chain, before_p, code, pat);
1538 /* Clear status for all registers we restored. */
1539 for (k = 0; k < i; k++)
1541 CLEAR_HARD_REG_BIT (hard_regs_saved, regno + k);
1542 SET_REGNO_REG_SET (&new->dead_or_set, regno + k);
1543 n_regs_saved--;
1546 /* Tell our callers how many extra registers we saved/restored. */
1547 return numregs - 1;
1550 /* Like insert_restore above, but save registers instead. */
1552 static int
1553 insert_save (struct insn_chain *chain, int before_p, int regno,
1554 HARD_REG_SET (*to_save), enum machine_mode *save_mode)
1556 int i;
1557 unsigned int k;
1558 rtx pat = NULL_RTX;
1559 int code;
1560 unsigned int numregs = 0;
1561 struct insn_chain *new;
1562 rtx mem;
1564 /* A common failure mode if register status is not correct in the
1565 RTL is for this routine to be called with a REGNO we didn't
1566 expect to save. That will cause us to write an insn with a (nil)
1567 SET_DEST or SET_SRC. Instead of doing so and causing a crash
1568 later, check for this common case here. This will remove one
1569 step in debugging such problems. */
1570 gcc_assert (regno_save_mem[regno][1]);
1572 /* Get the pattern to emit and update our status.
1574 See if we can save several registers with a single instruction.
1575 Work backwards to the single register case. */
1576 for (i = MOVE_MAX_WORDS; i > 0; i--)
1578 int j;
1579 int ok = 1;
1580 if (regno_save_mem[regno][i] == 0)
1581 continue;
1583 for (j = 0; j < i; j++)
1584 if (! TEST_HARD_REG_BIT (*to_save, regno + j))
1586 ok = 0;
1587 break;
1589 /* Must do this one save at a time. */
1590 if (! ok)
1591 continue;
1593 numregs = i;
1594 break;
1597 mem = regno_save_mem [regno][numregs];
1598 if (save_mode [regno] != VOIDmode
1599 && save_mode [regno] != GET_MODE (mem)
1600 && numregs == (unsigned int) hard_regno_nregs[regno][save_mode [regno]]
1601 && reg_save_code (regno, save_mode[regno]) >= 0)
1602 mem = adjust_address_nv (mem, save_mode[regno], 0);
1603 else
1604 mem = copy_rtx (mem);
1605 pat = gen_rtx_SET (VOIDmode, mem,
1606 gen_rtx_REG (GET_MODE (mem),
1607 regno));
1608 code = reg_save_code (regno, GET_MODE (mem));
1609 new = insert_one_insn (chain, before_p, code, pat);
1611 /* Set hard_regs_saved and dead_or_set for all the registers we saved. */
1612 for (k = 0; k < numregs; k++)
1614 SET_HARD_REG_BIT (hard_regs_saved, regno + k);
1615 SET_REGNO_REG_SET (&new->dead_or_set, regno + k);
1616 n_regs_saved++;
1619 /* Tell our callers how many extra registers we saved/restored. */
1620 return numregs - 1;
1623 /* Emit a new caller-save insn and set the code. */
1624 static struct insn_chain *
1625 insert_one_insn (struct insn_chain *chain, int before_p, int code, rtx pat)
1627 rtx insn = chain->insn;
1628 struct insn_chain *new;
1630 #ifdef HAVE_cc0
1631 /* If INSN references CC0, put our insns in front of the insn that sets
1632 CC0. This is always safe, since the only way we could be passed an
1633 insn that references CC0 is for a restore, and doing a restore earlier
1634 isn't a problem. We do, however, assume here that CALL_INSNs don't
1635 reference CC0. Guard against non-INSN's like CODE_LABEL. */
1637 if ((NONJUMP_INSN_P (insn) || JUMP_P (insn))
1638 && before_p
1639 && reg_referenced_p (cc0_rtx, PATTERN (insn)))
1640 chain = chain->prev, insn = chain->insn;
1641 #endif
1643 new = new_insn_chain ();
1644 if (before_p)
1646 rtx link;
1648 new->prev = chain->prev;
1649 if (new->prev != 0)
1650 new->prev->next = new;
1651 else
1652 reload_insn_chain = new;
1654 chain->prev = new;
1655 new->next = chain;
1656 new->insn = emit_insn_before (pat, insn);
1657 /* ??? It would be nice if we could exclude the already / still saved
1658 registers from the live sets. */
1659 COPY_REG_SET (&new->live_throughout, &chain->live_throughout);
1660 /* Registers that die in CHAIN->INSN still live in the new insn. */
1661 for (link = REG_NOTES (chain->insn); link; link = XEXP (link, 1))
1663 if (REG_NOTE_KIND (link) == REG_DEAD)
1665 rtx reg = XEXP (link, 0);
1666 int regno, i;
1668 gcc_assert (REG_P (reg));
1669 regno = REGNO (reg);
1670 if (regno >= FIRST_PSEUDO_REGISTER)
1671 regno = reg_renumber[regno];
1672 if (regno < 0)
1673 continue;
1674 for (i = hard_regno_nregs[regno][GET_MODE (reg)] - 1;
1675 i >= 0; i--)
1676 SET_REGNO_REG_SET (&new->live_throughout, regno + i);
1679 CLEAR_REG_SET (&new->dead_or_set);
1680 if (chain->insn == BB_HEAD (BASIC_BLOCK (chain->block)))
1681 BB_HEAD (BASIC_BLOCK (chain->block)) = new->insn;
1683 else
1685 new->next = chain->next;
1686 if (new->next != 0)
1687 new->next->prev = new;
1688 chain->next = new;
1689 new->prev = chain;
1690 if (GET_CODE (insn) != CODE_LABEL)
1691 new->insn = emit_insn_after (pat, insn);
1692 else
1694 /* Put the insn after bb note in a empty basic block. */
1695 gcc_assert (NEXT_INSN (insn) && NOTE_P (NEXT_INSN (insn)));
1696 new->insn = emit_insn_after (pat, NEXT_INSN (insn));
1698 /* ??? It would be nice if we could exclude the already / still saved
1699 registers from the live sets, and observe REG_UNUSED notes. */
1700 COPY_REG_SET (&new->live_throughout, &chain->live_throughout);
1701 /* Registers that are set in CHAIN->INSN live in the new insn.
1702 (Unless there is a REG_UNUSED note for them, but we don't
1703 look for them here.) */
1704 if (INSN_P (chain->insn))
1705 note_stores (PATTERN (chain->insn), add_stored_regs,
1706 &new->live_throughout);
1707 CLEAR_REG_SET (&new->dead_or_set);
1708 if (chain->insn == BB_END (BASIC_BLOCK (chain->block)))
1709 BB_END (BASIC_BLOCK (chain->block)) = new->insn;
1711 new->block = chain->block;
1712 new->is_caller_save_insn = 1;
1714 INSN_CODE (new->insn) = code;
1715 return new;
1717 #include "gt-caller-save.h"