Merge from trunk @ 138209
[official-gcc.git] / gcc / caller-save.c
blobe5ff04f08f182cfdcc43d2214d7d12acbaea5b84
1 /* Save and restore call-clobbered registers which are live across a call.
2 Copyright (C) 1989, 1992, 1994, 1995, 1997, 1998, 1999, 2000,
3 2001, 2002, 2003, 2004, 2005, 2006, 2007, 2008 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 "cfgloop.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 /* The number of elements in the subsequent array. */
72 static int save_slots_num;
74 /* Allocated slots so far. */
75 static rtx save_slots[FIRST_PSEUDO_REGISTER];
77 /* We will only make a register eligible for caller-save if it can be
78 saved in its widest mode with a simple SET insn as long as the memory
79 address is valid. We record the INSN_CODE is those insns here since
80 when we emit them, the addresses might not be valid, so they might not
81 be recognized. */
83 static int
84 cached_reg_save_code[FIRST_PSEUDO_REGISTER][MAX_MACHINE_MODE];
85 static int
86 cached_reg_restore_code[FIRST_PSEUDO_REGISTER][MAX_MACHINE_MODE];
88 /* Set of hard regs currently residing in save area (during insn scan). */
90 static HARD_REG_SET hard_regs_saved;
92 /* Number of registers currently in hard_regs_saved. */
94 static int n_regs_saved;
96 /* Computed by mark_referenced_regs, all regs referenced in a given
97 insn. */
98 static HARD_REG_SET referenced_regs;
99 /* Computed by mark_referenced_regs, all regs modified in a given insn. */
100 static HARD_REG_SET modified_regs;
103 static int reg_save_code (int, enum machine_mode);
104 static int reg_restore_code (int, enum machine_mode);
106 struct saved_hard_reg;
107 static void initiate_saved_hard_regs (void);
108 static struct saved_hard_reg *new_saved_hard_reg (int, int);
109 static void finish_saved_hard_regs (void);
110 static int saved_hard_reg_compare_func (const void *, const void *);
111 static void calculate_local_save_info (void);
112 static bool restore_trans_fun (int);
113 static void restore_con_fun_0 (basic_block);
114 static void restore_con_fun_n (edge);
115 static void calculate_restore_in_out (void);
116 static int calculate_restore_here (void);
117 static bool save_trans_fun (int);
118 static void save_con_fun_0 (basic_block);
119 static void save_con_fun_n (edge);
120 static void calculate_save_in_out (void);
121 static int calculate_save_here (void);
122 static bool free_trans_fun (int);
123 static void free_con_fun_0 (basic_block);
124 static void free_con_fun_n (edge);
125 static void calculate_free_in_out (void);
127 static void make_global_save_analysis (void);
128 static void print_annotated_hard_reg_set (FILE *, HARD_REG_SET,
129 unsigned char *, int *);
130 static void print_hard_reg_set (FILE *, HARD_REG_SET);
131 static void print_save_data (FILE *);
132 static void set_hard_reg_saved (HARD_REG_SET, unsigned char *,
133 enum machine_mode *, int *, int *);
135 static void mark_set_regs (rtx, const_rtx, void *);
136 static void add_stored_regs (rtx, const_rtx, void *);
137 static void mark_referenced_regs (rtx, int);
138 static int insert_save (struct insn_chain *, int, int, HARD_REG_SET *,
139 enum machine_mode *, int *);
140 static int insert_restore (struct insn_chain *, int, int, int,
141 enum machine_mode *, int *);
142 static struct insn_chain *insert_one_insn (struct insn_chain *, int, int,
143 rtx);
144 static void add_stored_regs (rtx, const_rtx, void *);
148 static GTY(()) rtx savepat;
149 static GTY(()) rtx restpat;
150 static GTY(()) rtx test_reg;
151 static GTY(()) rtx test_mem;
152 static GTY(()) rtx saveinsn;
153 static GTY(()) rtx restinsn;
155 /* Return the INSN_CODE used to save register REG in mode MODE. */
156 static int
157 reg_save_code (int reg, enum machine_mode mode)
159 bool ok;
160 if (cached_reg_save_code[reg][mode])
161 return cached_reg_save_code[reg][mode];
162 if (!HARD_REGNO_MODE_OK (reg, mode))
164 cached_reg_save_code[reg][mode] = -1;
165 cached_reg_restore_code[reg][mode] = -1;
166 return -1;
169 /* Update the register number and modes of the register
170 and memory operand. */
171 SET_REGNO (test_reg, reg);
172 PUT_MODE (test_reg, mode);
173 PUT_MODE (test_mem, mode);
175 /* Force re-recognition of the modified insns. */
176 INSN_CODE (saveinsn) = -1;
177 INSN_CODE (restinsn) = -1;
179 cached_reg_save_code[reg][mode] = recog_memoized (saveinsn);
180 cached_reg_restore_code[reg][mode] = recog_memoized (restinsn);
182 /* Now extract both insns and see if we can meet their
183 constraints. */
184 ok = (cached_reg_save_code[reg][mode] != -1
185 && cached_reg_restore_code[reg][mode] != -1);
186 if (ok)
188 extract_insn (saveinsn);
189 ok = constrain_operands (1);
190 extract_insn (restinsn);
191 ok &= constrain_operands (1);
194 if (! ok)
196 cached_reg_save_code[reg][mode] = -1;
197 cached_reg_restore_code[reg][mode] = -1;
199 gcc_assert (cached_reg_save_code[reg][mode]);
200 return cached_reg_save_code[reg][mode];
203 /* Return the INSN_CODE used to restore register REG in mode MODE. */
204 static int
205 reg_restore_code (int reg, enum machine_mode mode)
207 if (cached_reg_restore_code[reg][mode])
208 return cached_reg_restore_code[reg][mode];
209 /* Populate our cache. */
210 reg_save_code (reg, mode);
211 return cached_reg_restore_code[reg][mode];
214 /* Initialize for caller-save.
216 Look at all the hard registers that are used by a call and for which
217 regclass.c has not already excluded from being used across a call.
219 Ensure that we can find a mode to save the register and that there is a
220 simple insn to save and restore the register. This latter check avoids
221 problems that would occur if we tried to save the MQ register of some
222 machines directly into memory. */
224 void
225 init_caller_save (void)
227 rtx addr_reg;
228 int offset;
229 rtx address;
230 int i, j;
232 CLEAR_HARD_REG_SET (no_caller_save_reg_set);
233 /* First find all the registers that we need to deal with and all
234 the modes that they can have. If we can't find a mode to use,
235 we can't have the register live over calls. */
237 for (i = 0; i < FIRST_PSEUDO_REGISTER; i++)
239 if (call_used_regs[i] && ! call_fixed_regs[i])
241 for (j = 1; j <= MOVE_MAX_WORDS; j++)
243 regno_save_mode[i][j] = HARD_REGNO_CALLER_SAVE_MODE (i, j,
244 VOIDmode);
245 if (regno_save_mode[i][j] == VOIDmode && j == 1)
247 call_fixed_regs[i] = 1;
248 SET_HARD_REG_BIT (call_fixed_reg_set, i);
252 else
253 regno_save_mode[i][1] = VOIDmode;
256 /* The following code tries to approximate the conditions under which
257 we can easily save and restore a register without scratch registers or
258 other complexities. It will usually work, except under conditions where
259 the validity of an insn operand is dependent on the address offset.
260 No such cases are currently known.
262 We first find a typical offset from some BASE_REG_CLASS register.
263 This address is chosen by finding the first register in the class
264 and by finding the smallest power of two that is a valid offset from
265 that register in every mode we will use to save registers. */
267 for (i = 0; i < FIRST_PSEUDO_REGISTER; i++)
268 if (TEST_HARD_REG_BIT
269 (reg_class_contents
270 [(int) base_reg_class (regno_save_mode[i][1], PLUS, CONST_INT)], i))
271 break;
273 gcc_assert (i < FIRST_PSEUDO_REGISTER);
275 addr_reg = gen_rtx_REG (Pmode, i);
277 for (offset = 1 << (HOST_BITS_PER_INT / 2); offset; offset >>= 1)
279 address = gen_rtx_PLUS (Pmode, addr_reg, GEN_INT (offset));
281 for (i = 0; i < FIRST_PSEUDO_REGISTER; i++)
282 if (regno_save_mode[i][1] != VOIDmode
283 && ! strict_memory_address_p (regno_save_mode[i][1], address))
284 break;
286 if (i == FIRST_PSEUDO_REGISTER)
287 break;
290 /* If we didn't find a valid address, we must use register indirect. */
291 if (offset == 0)
292 address = addr_reg;
294 /* Next we try to form an insn to save and restore the register. We
295 see if such an insn is recognized and meets its constraints.
297 To avoid lots of unnecessary RTL allocation, we construct all the RTL
298 once, then modify the memory and register operands in-place. */
300 test_reg = gen_rtx_REG (VOIDmode, 0);
301 test_mem = gen_rtx_MEM (VOIDmode, address);
302 savepat = gen_rtx_SET (VOIDmode, test_mem, test_reg);
303 restpat = gen_rtx_SET (VOIDmode, test_reg, test_mem);
305 saveinsn = gen_rtx_INSN (VOIDmode, 0, 0, 0, 0, 0, savepat, -1, 0);
306 restinsn = gen_rtx_INSN (VOIDmode, 0, 0, 0, 0, 0, restpat, -1, 0);
308 for (i = 0; i < FIRST_PSEUDO_REGISTER; i++)
309 for (j = 1; j <= MOVE_MAX_WORDS; j++)
310 if (reg_save_code (i,regno_save_mode[i][j]) == -1)
312 regno_save_mode[i][j] = VOIDmode;
313 if (j == 1)
315 call_fixed_regs[i] = 1;
316 SET_HARD_REG_BIT (call_fixed_reg_set, i);
317 if (call_used_regs[i])
318 SET_HARD_REG_BIT (no_caller_save_reg_set, i);
325 /* Initialize save areas by showing that we haven't allocated any yet. */
327 void
328 init_save_areas (void)
330 int i, j;
332 for (i = 0; i < FIRST_PSEUDO_REGISTER; i++)
333 for (j = 1; j <= MOVE_MAX_WORDS; j++)
334 regno_save_mem[i][j] = 0;
335 save_slots_num = 0;
339 /* The structure represents a hard register which should be saved
340 through the call. It is used when the integrated register
341 allocator (IRA) is used and sharing save slots is on. */
342 struct saved_hard_reg
344 /* Order number starting with 0. */
345 int num;
346 /* The hard regno. */
347 int hard_regno;
348 /* Execution frequency of all calls through which given hard
349 register should be saved. */
350 int call_freq;
351 /* Stack slot reserved to save the hard register through calls. */
352 rtx slot;
353 /* True if it is first hard register in the chain of hard registers
354 sharing the same stack slot. */
355 int first_p;
356 /* Order number of the next hard register structure with the same
357 slot in the chain. -1 represents end of the chain. */
358 int next;
361 /* Map: hard register number to the corresponding structure. */
362 static struct saved_hard_reg *hard_reg_map[FIRST_PSEUDO_REGISTER];
364 /* The number of all structures representing hard registers should be
365 saved, in order words, the number of used elements in the following
366 array. */
367 static int saved_regs_num;
369 /* Pointers to all the structures. Index is the order number of the
370 corresponding structure. */
371 static struct saved_hard_reg *all_saved_regs[FIRST_PSEUDO_REGISTER];
373 /* First called function for work with saved hard registers. */
374 static void
375 initiate_saved_hard_regs (void)
377 int i;
379 saved_regs_num = 0;
380 for (i = 0; i < FIRST_PSEUDO_REGISTER; i++)
381 hard_reg_map[i] = NULL;
384 /* Allocate and return new saved hard register with given REGNO and
385 CALL_FREQ. */
386 static struct saved_hard_reg *
387 new_saved_hard_reg (int regno, int call_freq)
389 struct saved_hard_reg *saved_reg;
391 saved_reg
392 = (struct saved_hard_reg *) xmalloc (sizeof (struct saved_hard_reg));
393 hard_reg_map[regno] = all_saved_regs[saved_regs_num] = saved_reg;
394 saved_reg->num = saved_regs_num++;
395 saved_reg->hard_regno = regno;
396 saved_reg->call_freq = call_freq;
397 saved_reg->first_p = FALSE;
398 saved_reg->next = -1;
399 return saved_reg;
402 /* Free memory allocated for the saved hard registers. */
403 static void
404 finish_saved_hard_regs (void)
406 int i;
408 for (i = 0; i < saved_regs_num; i++)
409 free (all_saved_regs[i]);
412 /* The function is used to sort the saved hard register structures
413 according their frequency. */
414 static int
415 saved_hard_reg_compare_func (const void *v1p, const void *v2p)
417 const struct saved_hard_reg *p1 = *(struct saved_hard_reg * const *) v1p;
418 const struct saved_hard_reg *p2 = *(struct saved_hard_reg * const *) v2p;
420 if (flag_omit_frame_pointer)
422 if (p1->call_freq - p2->call_freq != 0)
423 return p1->call_freq - p2->call_freq;
425 else if (p2->call_freq - p1->call_freq != 0)
426 return p2->call_freq - p1->call_freq;
428 return p1->num - p2->num;
431 /* Allocate save areas for any hard registers that might need saving.
432 We take a conservative approach here and look for call-clobbered hard
433 registers that are assigned to pseudos that cross calls. This may
434 overestimate slightly (especially if some of these registers are later
435 used as spill registers), but it should not be significant.
437 For IRA we use priority coloring to decrease stack slots needed for
438 saving hard registers through calls. We build conflicts for them
439 to do coloring.
441 Future work:
443 In the fallback case we should iterate backwards across all possible
444 modes for the save, choosing the largest available one instead of
445 falling back to the smallest mode immediately. (eg TF -> DF -> SF).
447 We do not try to use "move multiple" instructions that exist
448 on some machines (such as the 68k moveml). It could be a win to try
449 and use them when possible. The hard part is doing it in a way that is
450 machine independent since they might be saving non-consecutive
451 registers. (imagine caller-saving d0,d1,a0,a1 on the 68k) */
453 void
454 setup_save_areas (void)
456 int i, j, k;
457 unsigned int r;
458 HARD_REG_SET hard_regs_used;
460 /* Allocate space in the save area for the largest multi-register
461 pseudos first, then work backwards to single register
462 pseudos. */
464 /* Find and record all call-used hard-registers in this function. */
465 CLEAR_HARD_REG_SET (hard_regs_used);
466 for (i = FIRST_PSEUDO_REGISTER; i < max_regno; i++)
467 if (reg_renumber[i] >= 0 && REG_N_CALLS_CROSSED (i) > 0)
469 unsigned int regno = reg_renumber[i];
470 unsigned int endregno
471 = end_hard_regno (GET_MODE (regno_reg_rtx[i]), regno);
472 for (r = regno; r < endregno; r++)
473 if (call_used_regs[r])
474 SET_HARD_REG_BIT (hard_regs_used, r);
477 if (flag_ira && optimize && flag_ira_share_save_slots)
479 rtx insn, slot;
480 struct insn_chain *chain, *next;
481 char *saved_reg_conflicts;
482 unsigned int regno;
483 int next_k, freq;
484 struct saved_hard_reg *saved_reg, *saved_reg2, *saved_reg3;
485 int call_saved_regs_num;
486 struct saved_hard_reg *call_saved_regs[FIRST_PSEUDO_REGISTER];
487 HARD_REG_SET hard_regs_to_save, used_regs, this_insn_sets;
488 reg_set_iterator rsi;
489 int best_slot_num;
490 int prev_save_slots_num;
491 rtx prev_save_slots[FIRST_PSEUDO_REGISTER];
493 initiate_saved_hard_regs ();
494 /* Create hard reg saved regs. */
495 for (chain = reload_insn_chain; chain != 0; chain = next)
497 insn = chain->insn;
498 next = chain->next;
499 if (GET_CODE (insn) != CALL_INSN
500 || find_reg_note (insn, REG_NORETURN, NULL))
501 continue;
502 freq = REG_FREQ_FROM_BB (BLOCK_FOR_INSN (insn));
503 REG_SET_TO_HARD_REG_SET (hard_regs_to_save,
504 &chain->live_throughout);
505 COPY_HARD_REG_SET (used_regs, call_used_reg_set);
507 /* Record all registers set in this call insn. These don't
508 need to be saved. N.B. the call insn might set a subreg
509 of a multi-hard-reg pseudo; then the pseudo is considered
510 live during the call, but the subreg that is set
511 isn't. */
512 CLEAR_HARD_REG_SET (this_insn_sets);
513 note_stores (PATTERN (insn), mark_set_regs, &this_insn_sets);
514 /* Sibcalls are considered to set the return value. */
515 if (SIBLING_CALL_P (insn) && crtl->return_rtx)
516 mark_set_regs (crtl->return_rtx, NULL_RTX, &this_insn_sets);
518 AND_COMPL_HARD_REG_SET (used_regs, call_fixed_reg_set);
519 AND_COMPL_HARD_REG_SET (used_regs, this_insn_sets);
520 AND_HARD_REG_SET (hard_regs_to_save, used_regs);
521 for (regno = 0; regno < FIRST_PSEUDO_REGISTER; regno++)
522 if (TEST_HARD_REG_BIT (hard_regs_to_save, regno))
524 if (hard_reg_map[regno] != NULL)
525 hard_reg_map[regno]->call_freq += freq;
526 else
527 saved_reg = new_saved_hard_reg (regno, freq);
529 /* Look through all live pseudos, mark their hard registers. */
530 EXECUTE_IF_SET_IN_REG_SET
531 (&chain->live_throughout, FIRST_PSEUDO_REGISTER, regno, rsi)
533 int r = reg_renumber[regno];
534 int bound;
536 if (r < 0)
537 continue;
539 bound = r + hard_regno_nregs[r][PSEUDO_REGNO_MODE (regno)];
540 for (; r < bound; r++)
541 if (TEST_HARD_REG_BIT (used_regs, r))
543 if (hard_reg_map[r] != NULL)
544 hard_reg_map[r]->call_freq += freq;
545 else
546 saved_reg = new_saved_hard_reg (r, freq);
547 SET_HARD_REG_BIT (hard_regs_to_save, r);
551 /* Find saved hard register conflicts. */
552 saved_reg_conflicts = (char *) xmalloc (saved_regs_num * saved_regs_num);
553 memset (saved_reg_conflicts, 0, saved_regs_num * saved_regs_num);
554 for (chain = reload_insn_chain; chain != 0; chain = next)
556 call_saved_regs_num = 0;
557 insn = chain->insn;
558 next = chain->next;
559 if (GET_CODE (insn) != CALL_INSN
560 || find_reg_note (insn, REG_NORETURN, NULL))
561 continue;
562 REG_SET_TO_HARD_REG_SET (hard_regs_to_save,
563 &chain->live_throughout);
564 COPY_HARD_REG_SET (used_regs, call_used_reg_set);
566 /* Record all registers set in this call insn. These don't
567 need to be saved. N.B. the call insn might set a subreg
568 of a multi-hard-reg pseudo; then the pseudo is considered
569 live during the call, but the subreg that is set
570 isn't. */
571 CLEAR_HARD_REG_SET (this_insn_sets);
572 note_stores (PATTERN (insn), mark_set_regs, &this_insn_sets);
573 /* Sibcalls are considered to set the return value,
574 compare flow.c:propagate_one_insn. */
575 if (SIBLING_CALL_P (insn) && crtl->return_rtx)
576 mark_set_regs (crtl->return_rtx, NULL_RTX, &this_insn_sets);
578 AND_COMPL_HARD_REG_SET (used_regs, call_fixed_reg_set);
579 AND_COMPL_HARD_REG_SET (used_regs, this_insn_sets);
580 AND_HARD_REG_SET (hard_regs_to_save, used_regs);
581 for (regno = 0; regno < FIRST_PSEUDO_REGISTER; regno++)
582 if (TEST_HARD_REG_BIT (hard_regs_to_save, regno))
584 gcc_assert (hard_reg_map[regno] != NULL);
585 call_saved_regs[call_saved_regs_num++] = hard_reg_map[regno];
587 /* Look through all live pseudos, mark their hard registers. */
588 EXECUTE_IF_SET_IN_REG_SET
589 (&chain->live_throughout, FIRST_PSEUDO_REGISTER, regno, rsi)
591 int r = reg_renumber[regno];
592 int bound;
594 if (r < 0)
595 continue;
597 bound = r + hard_regno_nregs[r][PSEUDO_REGNO_MODE (regno)];
598 for (; r < bound; r++)
599 if (TEST_HARD_REG_BIT (used_regs, r))
600 call_saved_regs[call_saved_regs_num++] = hard_reg_map[r];
602 for (i = 0; i < call_saved_regs_num; i++)
604 saved_reg = call_saved_regs[i];
605 for (j = 0; j < call_saved_regs_num; j++)
606 if (i != j)
608 saved_reg2 = call_saved_regs[j];
609 saved_reg_conflicts[saved_reg->num * saved_regs_num
610 + saved_reg2->num]
611 = saved_reg_conflicts[saved_reg2->num * saved_regs_num
612 + saved_reg->num]
613 = TRUE;
617 /* Sort saved hard regs. */
618 qsort (all_saved_regs, saved_regs_num, sizeof (struct saved_hard_reg *),
619 saved_hard_reg_compare_func);
620 /* Initiate slots available from the previous reload
621 iteration. */
622 prev_save_slots_num = save_slots_num;
623 memcpy (prev_save_slots, save_slots, save_slots_num * sizeof (rtx));
624 save_slots_num = 0;
625 /* Allocate stack slots for the saved hard registers. */
626 for (i = 0; i < saved_regs_num; i++)
628 saved_reg = all_saved_regs[i];
629 regno = saved_reg->hard_regno;
630 for (j = 0; j < i; j++)
632 saved_reg2 = all_saved_regs[j];
633 if (! saved_reg2->first_p)
634 continue;
635 slot = saved_reg2->slot;
636 for (k = j; k >= 0; k = next_k)
638 saved_reg3 = all_saved_regs[k];
639 next_k = saved_reg3->next;
640 if (saved_reg_conflicts[saved_reg->num * saved_regs_num
641 + saved_reg3->num])
642 break;
644 if (k < 0
645 && (GET_MODE_SIZE (regno_save_mode[regno][1])
646 <= GET_MODE_SIZE (regno_save_mode
647 [saved_reg2->hard_regno][1])))
649 saved_reg->slot
650 = adjust_address_nv
651 (slot, regno_save_mode[saved_reg->hard_regno][1], 0);
652 regno_save_mem[regno][1] = saved_reg->slot;
653 saved_reg->next = saved_reg2->next;
654 saved_reg2->next = i;
655 if (dump_file != NULL)
656 fprintf (dump_file, "%d uses slot of %d\n",
657 regno, saved_reg2->hard_regno);
658 break;
661 if (j == i)
663 saved_reg->first_p = TRUE;
664 for (best_slot_num = -1, j = 0; j < prev_save_slots_num; j++)
666 slot = prev_save_slots[j];
667 if (slot == NULL_RTX)
668 continue;
669 if (GET_MODE_SIZE (regno_save_mode[regno][1])
670 <= GET_MODE_SIZE (GET_MODE (slot))
671 && best_slot_num < 0)
672 best_slot_num = j;
673 if (GET_MODE (slot) == regno_save_mode[regno][1])
674 break;
676 if (best_slot_num >= 0)
678 saved_reg->slot = prev_save_slots[best_slot_num];
679 saved_reg->slot
680 = adjust_address_nv
681 (saved_reg->slot,
682 regno_save_mode[saved_reg->hard_regno][1], 0);
683 if (dump_file != NULL)
684 fprintf (dump_file,
685 "%d uses a slot from prev iteration\n", regno);
686 prev_save_slots[best_slot_num] = NULL_RTX;
687 if (best_slot_num + 1 == prev_save_slots_num)
688 prev_save_slots_num--;
690 else
692 saved_reg->slot
693 = assign_stack_local
694 (regno_save_mode[regno][1],
695 GET_MODE_SIZE (regno_save_mode[regno][1]), 0);
696 if (dump_file != NULL)
697 fprintf (dump_file, "%d uses a new slot\n", regno);
699 regno_save_mem[regno][1] = saved_reg->slot;
700 save_slots[save_slots_num++] = saved_reg->slot;
703 free (saved_reg_conflicts);
704 finish_saved_hard_regs ();
706 else
708 /* Now run through all the call-used hard-registers and allocate
709 space for them in the caller-save area. Try to allocate space
710 in a manner which allows multi-register saves/restores to be done. */
712 for (i = 0; i < FIRST_PSEUDO_REGISTER; i++)
713 for (j = MOVE_MAX_WORDS; j > 0; j--)
715 int do_save = 1;
717 /* If no mode exists for this size, try another. Also break out
718 if we have already saved this hard register. */
719 if (regno_save_mode[i][j] == VOIDmode || regno_save_mem[i][1] != 0)
720 continue;
722 /* See if any register in this group has been saved. */
723 for (k = 0; k < j; k++)
724 if (regno_save_mem[i + k][1])
726 do_save = 0;
727 break;
729 if (! do_save)
730 continue;
732 for (k = 0; k < j; k++)
733 if (! TEST_HARD_REG_BIT (hard_regs_used, i + k))
735 do_save = 0;
736 break;
738 if (! do_save)
739 continue;
741 /* We have found an acceptable mode to store in. */
742 regno_save_mem[i][j]
743 = assign_stack_local (regno_save_mode[i][j],
744 GET_MODE_SIZE (regno_save_mode[i][j]), 0);
746 /* Setup single word save area just in case... */
747 for (k = 0; k < j; k++)
748 /* This should not depend on WORDS_BIG_ENDIAN.
749 The order of words in regs is the same as in memory. */
750 regno_save_mem[i + k][1]
751 = adjust_address_nv (regno_save_mem[i][j],
752 regno_save_mode[i + k][1],
753 k * UNITS_PER_WORD);
757 /* Now loop again and set the alias set of any save areas we made to
758 the alias set used to represent frame objects. */
759 for (i = 0; i < FIRST_PSEUDO_REGISTER; i++)
760 for (j = MOVE_MAX_WORDS; j > 0; j--)
761 if (regno_save_mem[i][j] != 0)
762 set_mem_alias_set (regno_save_mem[i][j], get_frame_alias_set ());
767 /* This page contains code for global analysis for better placement
768 save/restore insn when IRA is used.
770 First, we calculate local BB info in function calculate_local_save_info:
771 o hard registers set/mentioned in basic blocks (they prevents
772 moving corresponding saves/restores further up/down in CFG) --
773 see comments for save_kill/restore_kill.
774 o hard registers needed to be saved/restored around calls and
775 which are not set/mentioned before/after the call in the basic
776 block -- see comments for save_gen/restore_gen.
777 o free hard registers at the basic block start -- see comments
778 for free_gen. This set can be different from save_gen. For
779 example, in the following situation
783 insn using R
785 call through which R live through and should be saved/restored.
788 save_gen contains R because we can put save at the BB start but
789 free_gen does not contain R because we can not use R for other
790 purposes from the BB start till the insn using R.
792 Second, we calculate the global info for placement saves/restores
793 in functions -- see comments for save_in, save_out, restore_in,
794 restore_out, free_in, free_out.
796 The equations for the global info calculation are not trivial
797 because we can not put saves/restores on CFG edges as the reload
798 can not work when new BBs added. For example, it means that all
799 save_out set of BB should be a subset of intersections of save_in
800 of all successors of the BB. Preventing putting the code on edges
801 makes data flow problem bidirectional. The data flow functions are
802 also complicated by necessity to propagate saving modes (e.g. x86
803 fp register can be saved in XFmode or DFmode).
805 The equations do not permit to propagate info into the loops
806 because we can not put saves/restores in more frequently executed
807 points. But if the loop does not contains references for a hard
808 register, we permit to propagate info for the register into the
809 loop because the info will be propagated through all the loop and
810 as consequence corresponding saves/restores will be moved through
811 the loop.
813 Third, using this info we place saves/restores and set up member
814 `saved' in function save_call_clobbered_regs.
818 /* The following structure contains basic block data flow information
819 used to optimize save/restore placement. Data flow equations are
820 bidirectional because we don't want to put save/restore code on CFG
821 edges. */
822 struct bb_info
824 /* True if save_out/restore_in should be empty for this block. */
825 int empty_save_out_p, empty_restore_in_p;
826 /* Hard registers set/mentioned in the BB. */
827 HARD_REG_SET save_kill;
828 HARD_REG_SET restore_kill;
829 /* Hard registers needed to be saved and this save not killed (see above)
830 by an insn in the BB before that. */
831 HARD_REG_SET save_gen;
832 /* Hard registers needed to be restored and this restore not killed
833 by an insn in the BB after that. */
834 HARD_REG_SET restore_gen;
835 /* Hard registers free and not killed by an insn in the BB before
836 that. */
837 HARD_REG_SET free_gen;
838 /* Registers needed to be saved/restored at the start and end of the
839 basic block. */
840 HARD_REG_SET save_in, save_out, restore_in, restore_out;
841 /* Registers free at the start and end of the basic block. */
842 HARD_REG_SET free_in, free_out;
843 /* Hard registers living at the start/end of the basic block. */
844 HARD_REG_SET live_at_start, live_at_end;
845 /* We don't want to generate save/restore insns on edges because it
846 changes CFG during the reload. To prevent this we use the
847 following set. This set defines what hard registers should be
848 saved/restored at the start/end of basic block. */
849 HARD_REG_SET save_here, restore_here;
850 /* It corresponds to set SAVE_GEN right after the call of
851 calculate_local_save_info. */
852 unsigned char save_in_mode[FIRST_PSEUDO_REGISTER];
853 /* Saving modes at the end of the basic block. */
854 unsigned char save_out_mode[FIRST_PSEUDO_REGISTER];
855 /* Restoring modes at the start of the basic block. */
856 unsigned char restore_in_mode[FIRST_PSEUDO_REGISTER];
857 /* It corresponds to set RESTORE_GEN right after the call of
858 calculate_local_save_info. */
859 unsigned char restore_out_mode[FIRST_PSEUDO_REGISTER];
860 /* Analogous but the corresponding pseudo-register numbers. */
861 int save_in_pseudo[FIRST_PSEUDO_REGISTER];
862 int save_out_pseudo[FIRST_PSEUDO_REGISTER];
863 int restore_in_pseudo[FIRST_PSEUDO_REGISTER];
864 int restore_out_pseudo[FIRST_PSEUDO_REGISTER];
867 /* Macros for accessing data flow information of basic blocks. */
868 #define BB_INFO(BB) ((struct bb_info *) (BB)->aux)
869 #define BB_INFO_BY_INDEX(N) BB_INFO (BASIC_BLOCK(N))
871 /* The following structure contains loop info necessary for
872 save/restore placement optimization. */
873 struct loop_info
875 /* All hard registers mentioned in the loop. If a pseudo is
876 mentioned in the loop, the hard registers assigned to it are also
877 believed to be mentioned in the loop. */
878 HARD_REG_SET mentioned_regs;
881 /* Macro for accessing data flow information of LOOP. */
882 #define LOOP_INFO(LOOP) ((struct loop_info *) (LOOP)->aux)
884 /* The function calculates sets KILL, GEN, LIVE_AT_START and
885 RESTORE_OUT_MODES corresponding to GEN for basic blocks. */
886 static void
887 calculate_local_save_info (void)
889 int i, empty_save_out_p, empty_restore_in_p;
890 struct insn_chain *chain, *next;
891 struct bb_info *bb_info;
892 /* Computed in mark_set_regs, holds all registers set by the current
893 instruction. */
894 HARD_REG_SET save_kill, restore_kill;
895 HARD_REG_SET this_insn_sets, save_gen, restore_gen, free_gen, temp_set;
896 unsigned regno;
897 reg_set_iterator rsi;
898 /* A map: hard register being saved to the mode used for its
899 saving. */
900 enum machine_mode save_mode[FIRST_PSEUDO_REGISTER];
901 /* A map: hard register being saved to the pseudo-register assigned
902 to the hard register at given point. */
903 int save_pseudo[FIRST_PSEUDO_REGISTER];
905 CLEAR_HARD_REG_SET (save_gen);
906 CLEAR_HARD_REG_SET (restore_gen);
907 CLEAR_HARD_REG_SET (free_gen);
908 CLEAR_HARD_REG_SET (save_kill);
909 CLEAR_HARD_REG_SET (restore_kill);
910 empty_save_out_p = FALSE;
911 empty_restore_in_p = FALSE;
912 for (i = 0; i < FIRST_PSEUDO_REGISTER; i++)
914 save_mode[i] = VOIDmode;
915 save_pseudo[i] = -1;
918 for (chain = reload_insn_chain; chain != 0; chain = next)
920 rtx insn = chain->insn;
921 enum rtx_code code = GET_CODE (insn);
923 next = chain->next;
925 bb_info = BB_INFO_BY_INDEX (chain->block);
926 if (INSN_P (insn))
928 CLEAR_HARD_REG_SET (referenced_regs);
929 CLEAR_HARD_REG_SET (modified_regs);
930 mark_referenced_regs (PATTERN (insn), FALSE);
931 AND_COMPL_HARD_REG_SET (restore_gen, referenced_regs);
932 IOR_HARD_REG_SET (restore_kill, referenced_regs);
933 IOR_HARD_REG_SET (save_kill, modified_regs);
935 if (code == CALL_INSN && find_reg_note (insn, REG_NORETURN, NULL))
937 SET_HARD_REG_SET (save_kill);
938 SET_HARD_REG_SET (restore_kill);
940 else if (code == CALL_INSN)
942 HARD_REG_SET hard_regs_to_save, used_regs;
944 /* Use the register life information in CHAIN to compute
945 which regs are live during the call. */
946 REG_SET_TO_HARD_REG_SET (hard_regs_to_save,
947 &chain->live_throughout);
948 /* Look through all live pseudos, mark their hard registers
949 and choose proper mode for saving. */
950 EXECUTE_IF_SET_IN_REG_SET
951 (&chain->live_throughout, FIRST_PSEUDO_REGISTER, regno, rsi)
953 int r = reg_renumber[regno];
954 int nregs;
955 enum machine_mode mode;
957 /* Remember live_throughout can contain spilled
958 registers when IRA is used. */
959 if (flag_ira && optimize && r < 0)
960 continue;
961 gcc_assert (r >= 0);
963 nregs = hard_regno_nregs[r][PSEUDO_REGNO_MODE (regno)];
964 mode = HARD_REGNO_CALLER_SAVE_MODE
965 (r, nregs, PSEUDO_REGNO_MODE (regno));
966 if (nregs == 1)
968 SET_HARD_REG_BIT (hard_regs_to_save, r);
969 save_mode[r] = mode;
970 save_pseudo[r] = regno;
972 else
974 while (nregs-- > 0)
976 SET_HARD_REG_BIT (hard_regs_to_save, r + nregs);
977 save_mode[r + nregs]
978 = regno_save_mode[r + nregs][1];
979 save_pseudo[r + nregs] = regno;
984 /* Record all registers set in this call insn. These
985 don't need to be saved. N.B. the call insn might set
986 a subreg of a multi-hard-reg pseudo; then the pseudo
987 is considered live during the call, but the subreg
988 that is set isn't. */
989 CLEAR_HARD_REG_SET (this_insn_sets);
990 note_stores (PATTERN (insn), mark_set_regs, &this_insn_sets);
991 /* Sibcalls are considered to set the return value. */
992 if (SIBLING_CALL_P (insn) && crtl->return_rtx)
993 mark_set_regs (crtl->return_rtx, NULL_RTX, &this_insn_sets);
995 /* Compute which hard regs must be saved before this call. */
996 AND_COMPL_HARD_REG_SET (hard_regs_to_save, call_fixed_reg_set);
997 AND_COMPL_HARD_REG_SET (hard_regs_to_save, this_insn_sets);
998 COPY_HARD_REG_SET (used_regs, call_used_reg_set);
999 AND_HARD_REG_SET (hard_regs_to_save, used_regs);
1000 IOR_HARD_REG_SET (restore_gen, hard_regs_to_save);
1001 COPY_HARD_REG_SET (temp_set, hard_regs_to_save);
1002 AND_COMPL_HARD_REG_SET (temp_set, save_kill);
1003 AND_COMPL_HARD_REG_SET (temp_set, save_gen);
1004 IOR_HARD_REG_SET (save_gen, temp_set);
1005 for (i = 0; i < FIRST_PSEUDO_REGISTER; i++)
1006 if (TEST_HARD_REG_BIT (temp_set, i))
1008 bb_info->save_in_mode[i] = save_mode[i];
1009 bb_info->save_in_pseudo[i] = save_pseudo[i];
1011 COPY_HARD_REG_SET (temp_set, hard_regs_to_save);
1012 AND_COMPL_HARD_REG_SET (temp_set, save_kill);
1013 AND_COMPL_HARD_REG_SET (temp_set, restore_kill);
1014 IOR_HARD_REG_SET (free_gen, temp_set);
1018 if (chain->next == 0 || chain->next->block != chain->block)
1020 basic_block bb = BASIC_BLOCK (chain->block);
1021 edge e;
1022 edge_iterator ei;
1023 loop_p loop;
1025 if (! empty_save_out_p)
1026 FOR_EACH_EDGE (e, ei, bb->succs)
1027 if (e->flags & EDGE_ABNORMAL)
1029 empty_save_out_p = TRUE;
1030 break;
1032 bb_info->empty_save_out_p = empty_save_out_p;
1033 empty_save_out_p = FALSE;
1034 if (! empty_restore_in_p)
1035 FOR_EACH_EDGE (e, ei, bb->preds)
1036 if (e->flags & EDGE_ABNORMAL)
1038 empty_restore_in_p = TRUE;
1039 break;
1041 bb_info->empty_restore_in_p = empty_restore_in_p;
1042 empty_restore_in_p = FALSE;
1043 COPY_HARD_REG_SET (bb_info->save_gen, save_gen);
1044 COPY_HARD_REG_SET (bb_info->restore_gen, restore_gen);
1045 COPY_HARD_REG_SET (bb_info->free_gen, free_gen);
1046 CLEAR_HARD_REG_SET (bb_info->restore_in);
1047 for (i = 0; i < FIRST_PSEUDO_REGISTER; i++)
1049 bb_info->save_out_mode[i] = VOIDmode;
1050 bb_info->save_out_pseudo[i] = -1;
1051 if (!TEST_HARD_REG_BIT (save_gen, i))
1053 bb_info->save_in_mode[i] = VOIDmode;
1054 bb_info->save_in_pseudo[i] = -1;
1056 bb_info->restore_in_mode[i] = VOIDmode;
1057 bb_info->restore_in_pseudo[i] = -1;
1058 if (TEST_HARD_REG_BIT (restore_gen, i))
1060 bb_info->restore_out_mode[i] = save_mode[i];
1061 bb_info->restore_out_pseudo[i] = save_pseudo[i];
1063 else
1065 bb_info->restore_out_mode[i] = VOIDmode;
1066 bb_info->restore_out_pseudo[i] = -1;
1069 COPY_HARD_REG_SET (bb_info->save_kill, save_kill);
1070 COPY_HARD_REG_SET (bb_info->restore_kill, restore_kill);
1071 for (loop = bb->loop_father;
1072 loop != current_loops->tree_root;
1073 loop = loop_outer (loop))
1075 struct loop_info *loop_info = LOOP_INFO (loop);
1077 IOR_HARD_REG_SET (loop_info->mentioned_regs, save_kill);
1078 IOR_HARD_REG_SET (loop_info->mentioned_regs, restore_kill);
1080 CLEAR_HARD_REG_SET (bb_info->save_in);
1081 CLEAR_HARD_REG_SET (bb_info->restore_out);
1082 CLEAR_HARD_REG_SET (bb_info->free_in);
1083 /* We don't use LIVE for IRA. */
1084 REG_SET_TO_HARD_REG_SET
1085 (bb_info->live_at_end,
1086 flag_ira ? DF_LR_OUT (bb) : DF_LIVE_OUT (bb));
1087 EXECUTE_IF_SET_IN_REG_SET
1088 ((flag_ira ? DF_LR_OUT (bb) : DF_LIVE_OUT (bb)),
1089 FIRST_PSEUDO_REGISTER, regno, rsi)
1091 int r = reg_renumber[regno];
1092 int nregs;
1094 if (r < 0)
1095 continue;
1096 nregs = hard_regno_nregs[r][PSEUDO_REGNO_MODE (regno)];
1097 while (nregs-- > 0)
1098 SET_HARD_REG_BIT (bb_info->live_at_end, r + nregs);
1100 REG_SET_TO_HARD_REG_SET
1101 (bb_info->live_at_start,
1102 flag_ira ? DF_LR_IN (bb) : DF_LIVE_IN (bb));
1103 EXECUTE_IF_SET_IN_REG_SET
1104 ((flag_ira ? DF_LR_IN (bb) : DF_LIVE_IN (bb)),
1105 FIRST_PSEUDO_REGISTER, regno, rsi)
1107 int r = reg_renumber[regno];
1108 int nregs;
1110 if (r < 0)
1111 continue;
1112 nregs = hard_regno_nregs[r][PSEUDO_REGNO_MODE (regno)];
1113 while (nregs-- > 0)
1114 SET_HARD_REG_BIT (bb_info->live_at_start, r + nregs);
1116 CLEAR_HARD_REG_SET (bb_info->save_here);
1117 CLEAR_HARD_REG_SET (bb_info->restore_here);
1118 CLEAR_HARD_REG_SET (save_gen);
1119 CLEAR_HARD_REG_SET (restore_gen);
1120 CLEAR_HARD_REG_SET (free_gen);
1121 CLEAR_HARD_REG_SET (save_kill);
1122 CLEAR_HARD_REG_SET (restore_kill);
1123 for (i = 0; i < FIRST_PSEUDO_REGISTER; i++)
1125 save_mode[i] = VOIDmode;
1126 save_pseudo[i] = -1;
1132 /* The transfer function used by the DF equation solver to propagate restore
1133 info through block with BB_INDEX according to the following
1134 equation:
1136 bb.restore_out = (bb.restore_in - bb.restore_kill) U bb.restore_gen.
1138 The function also propagates saving modes of the hard
1139 registers. */
1140 static bool
1141 restore_trans_fun (int bb_index)
1143 int i;
1144 HARD_REG_SET temp_set;
1145 struct bb_info *bb_info = BB_INFO_BY_INDEX (bb_index);
1147 COPY_HARD_REG_SET (temp_set, bb_info->restore_in);
1148 AND_COMPL_HARD_REG_SET (temp_set, bb_info->restore_kill);
1149 for (i = 0; i < FIRST_PSEUDO_REGISTER; i++)
1150 if (TEST_HARD_REG_BIT (temp_set, i)
1151 && ! TEST_HARD_REG_BIT (bb_info->restore_gen, i))
1153 gcc_assert (bb_info->restore_out_mode[i] == VOIDmode
1154 || ((bb_info->restore_out_mode[i]
1155 == bb_info->restore_in_mode[i])
1156 && (bb_info->restore_out_pseudo[i]
1157 == bb_info->restore_in_pseudo[i])));
1158 bb_info->restore_out_mode[i] = bb_info->restore_in_mode[i];
1159 bb_info->restore_out_pseudo[i] = bb_info->restore_in_pseudo[i];
1161 IOR_HARD_REG_SET (temp_set, bb_info->restore_gen);
1162 if (! hard_reg_set_equal_p (temp_set, bb_info->restore_out))
1164 COPY_HARD_REG_SET (bb_info->restore_out, temp_set);
1165 return true;
1167 return false;
1170 /* The confluence function used by the DF equation solver to set up restore info
1171 for a block BB without predecessor. */
1172 static void
1173 restore_con_fun_0 (basic_block bb)
1175 CLEAR_HARD_REG_SET (BB_INFO (bb)->restore_in);
1178 /* The confluence function used by the DF equation solver to propagate
1179 restore info from predecessor to successor on edge E (pred->bb)
1180 according to the following equation:
1182 bb.restore_in = empty for entry block or one with empty_restore_in_p
1183 | ^(pred.restore_out - pred.restore_here) ^ bb.live_at_start
1185 If the edge is a loop enter we do not propagate the info because we
1186 don't want to put restores in more frequently executed places.
1189 static void
1190 restore_con_fun_n (edge e)
1192 int i;
1193 HARD_REG_SET temp_set;
1194 basic_block pred = e->src;
1195 basic_block bb = e->dest;
1196 struct bb_info *bb_info = BB_INFO (bb);
1198 if (bb_info->empty_restore_in_p)
1199 return;
1201 COPY_HARD_REG_SET (temp_set, BB_INFO (pred)->restore_out);
1202 AND_COMPL_HARD_REG_SET (temp_set, BB_INFO (pred)->restore_here);
1203 if (bb->loop_depth > pred->loop_depth)
1204 AND_COMPL_HARD_REG_SET (temp_set,
1205 LOOP_INFO (bb->loop_father)->mentioned_regs);
1206 AND_HARD_REG_SET (temp_set, bb_info->live_at_start);
1207 if (EDGE_PRED (bb, 0) == e)
1208 COPY_HARD_REG_SET (bb_info->restore_in, temp_set);
1209 else
1210 AND_HARD_REG_SET (bb_info->restore_in, temp_set);
1211 for (i = 0; i < FIRST_PSEUDO_REGISTER; i++)
1212 if (TEST_HARD_REG_BIT (temp_set, i))
1214 if (bb_info->restore_in_mode[i] != VOIDmode
1215 && BB_INFO (pred)->restore_out_mode[i] != VOIDmode
1216 && (bb_info->restore_in_mode[i]
1217 != BB_INFO (pred)->restore_out_mode[i]
1218 || bb_info->restore_in_pseudo[i]
1219 != BB_INFO (pred)->restore_out_pseudo[i]))
1220 CLEAR_HARD_REG_BIT (bb_info->restore_in, i);
1221 else if (BB_INFO (pred)->restore_out_mode[i] != VOIDmode)
1223 bb_info->restore_in_mode[i]
1224 = BB_INFO (pred)->restore_out_mode[i];
1225 bb_info->restore_in_pseudo[i]
1226 = BB_INFO (pred)->restore_out_pseudo[i];
1231 /* Basic blocks for data flow problem -- all bocks except the special
1232 ones. */
1233 static bitmap all_blocks;
1235 /* The function calculates global restore information according
1236 to the following equations:
1238 bb.restore_in = empty for entry block or one with empty_restore_in_p
1239 | ^(pred.restore_out - pred.restore_here) ^ bb.live_at_start
1240 bb.restore_out = (bb.restore_in - bb.restore_kill) U bb.restore_gen.
1242 The function also calculates restore_in_mode and restore_out_mode.
1244 static void
1245 calculate_restore_in_out (void)
1247 df_simple_dataflow (DF_FORWARD, NULL, restore_con_fun_0, restore_con_fun_n,
1248 restore_trans_fun, all_blocks,
1249 df_get_postorder (DF_FORWARD),
1250 df_get_n_blocks (DF_FORWARD));
1253 /* The function calculates RESTORE_HERE according to the equation
1254 bb.restore_here = U ((bb.restore_out - succ.restore_in)
1255 ^ succ.live_at_start). */
1256 static int
1257 calculate_restore_here (void)
1259 basic_block bb;
1260 edge e;
1261 edge_iterator ei;
1262 HARD_REG_SET restore_here, temp_set;
1263 int changed_p = FALSE;
1265 FOR_EACH_BB (bb)
1267 CLEAR_HARD_REG_SET (restore_here);
1268 FOR_EACH_EDGE (e, ei, bb->succs)
1270 basic_block dest = e->dest;
1272 COPY_HARD_REG_SET (temp_set, BB_INFO (bb)->restore_out);
1273 AND_COMPL_HARD_REG_SET (temp_set, BB_INFO (dest)->restore_in);
1274 AND_HARD_REG_SET (temp_set, BB_INFO (dest)->live_at_start);
1275 IOR_HARD_REG_SET (restore_here, temp_set);
1278 if (! hard_reg_set_equal_p (restore_here, BB_INFO (bb)->restore_here))
1280 COPY_HARD_REG_SET (BB_INFO (bb)->restore_here, restore_here);
1281 changed_p = TRUE;
1284 return changed_p;
1287 /* The transfer function used by the DF equation solver to propagate save info
1288 through block with BB_INDEX according to the following
1289 equation:
1291 bb.save_in = ((bb.save_out - bb.save_kill) U bb.save_gen) - bb.restore_in.
1293 The function also propagates saving modes of the hard
1294 registers. */
1296 static bool
1297 save_trans_fun (int bb_index)
1299 int i;
1300 HARD_REG_SET temp_set;
1301 struct bb_info *bb_info = BB_INFO_BY_INDEX (bb_index);
1303 COPY_HARD_REG_SET (temp_set, bb_info->save_out);
1304 AND_COMPL_HARD_REG_SET (temp_set, bb_info->save_kill);
1305 for (i = 0; i < FIRST_PSEUDO_REGISTER; i++)
1306 if (TEST_HARD_REG_BIT (temp_set, i)
1307 && ! TEST_HARD_REG_BIT (bb_info->save_gen, i))
1309 gcc_assert (bb_info->save_in_mode[i] == VOIDmode
1310 || ((bb_info->save_in_mode[i]
1311 == bb_info->save_out_mode[i])
1312 && (bb_info->save_in_pseudo[i]
1313 == bb_info->save_out_pseudo[i])));
1314 bb_info->save_in_mode[i] = bb_info->save_out_mode[i];
1315 bb_info->save_in_pseudo[i] = bb_info->save_out_pseudo[i];
1317 IOR_HARD_REG_SET (temp_set, bb_info->save_gen);
1318 AND_COMPL_HARD_REG_SET (temp_set, bb_info->restore_in);
1319 if (! hard_reg_set_equal_p (temp_set, bb_info->save_in))
1321 COPY_HARD_REG_SET (bb_info->save_in, temp_set);
1322 return true;
1324 return false;
1327 /* The confluence function used by the DF equation solver to set up
1328 save info for a block BB without successor. */
1329 static void
1330 save_con_fun_0 (basic_block bb)
1332 CLEAR_HARD_REG_SET (BB_INFO (bb)->save_out);
1335 /* The confluence function used by the DF equation solver to propagate
1336 save info from successor to predecessor on edge E (bb->succ)
1337 according to the following equation:
1339 bb.save_out = empty for exit block or one with empty_save_out_p
1340 | ^(succ.save_in - succ.save_here) ^ bb.live_at_end
1342 If the edge is a loop exit we do not propagate the info because we
1343 don't want to put saves in more frequently executed places.
1345 static void
1346 save_con_fun_n (edge e)
1348 int i;
1349 HARD_REG_SET temp_set;
1350 basic_block succ = e->dest;
1351 basic_block bb = e->src;
1352 struct bb_info *bb_info = BB_INFO (bb);
1354 if (bb_info->empty_save_out_p)
1355 return;
1357 COPY_HARD_REG_SET (temp_set, BB_INFO (succ)->save_in);
1358 AND_COMPL_HARD_REG_SET (temp_set, BB_INFO (succ)->save_here);
1359 if (bb->loop_depth > succ->loop_depth)
1360 AND_COMPL_HARD_REG_SET (temp_set,
1361 LOOP_INFO (bb->loop_father)->mentioned_regs);
1362 AND_HARD_REG_SET (temp_set, bb_info->live_at_end);
1363 if (EDGE_SUCC (bb, 0) == e)
1364 COPY_HARD_REG_SET (bb_info->save_out, temp_set);
1365 else
1366 AND_HARD_REG_SET (bb_info->save_out, temp_set);
1367 for (i = 0; i < FIRST_PSEUDO_REGISTER; i++)
1368 if (TEST_HARD_REG_BIT (temp_set, i))
1370 if (bb_info->save_out_mode[i] != VOIDmode
1371 && BB_INFO (succ)->save_in_mode[i] != VOIDmode
1372 && ((bb_info->save_out_mode[i]
1373 != BB_INFO (succ)->save_in_mode[i])
1374 || (bb_info->save_out_pseudo[i]
1375 != BB_INFO (succ)->save_in_pseudo[i])))
1376 CLEAR_HARD_REG_BIT (bb_info->save_out, i);
1377 else if (BB_INFO (succ)->save_in_mode[i] != VOIDmode)
1379 bb_info->save_out_mode[i]
1380 = BB_INFO (succ)->save_in_mode[i];
1381 bb_info->save_out_pseudo[i]
1382 = BB_INFO (succ)->save_in_pseudo[i];
1387 /* The transfer function calculates global save information according
1388 to the following equations:
1390 bb.save_out = empty for exit block or one with empty_save_out_p
1391 | ^(succ.save_in - succ.save_here) ^ bb.live_at_end
1392 bb.save_in = ((bb.save_out - bb.save_kill) U bb.save_gen) - bb.restore_in.
1394 The function also calculates save_in_mode and save_out_mode.
1396 static void
1397 calculate_save_in_out (void)
1399 df_simple_dataflow (DF_BACKWARD, NULL, save_con_fun_0, save_con_fun_n,
1400 save_trans_fun, all_blocks,
1401 df_get_postorder (DF_BACKWARD),
1402 df_get_n_blocks (DF_BACKWARD));
1405 /* The function calculates SAVE_HERE according to the equation
1406 bb.save_here = U ((bb.save_in - pred.save_out) ^ pred.live_at_end). */
1407 static int
1408 calculate_save_here (void)
1410 basic_block bb;
1411 edge e;
1412 edge_iterator ei;
1413 HARD_REG_SET save_here, temp_set;
1414 int changed_p = FALSE;
1416 FOR_EACH_BB (bb)
1418 CLEAR_HARD_REG_SET (save_here);
1419 FOR_EACH_EDGE (e, ei, bb->preds)
1421 basic_block src = e->src;
1423 COPY_HARD_REG_SET (temp_set, BB_INFO (bb)->save_in);
1424 AND_COMPL_HARD_REG_SET (temp_set, BB_INFO (src)->save_out);
1425 AND_HARD_REG_SET (temp_set, BB_INFO (src)->live_at_end);
1426 IOR_HARD_REG_SET (save_here, temp_set);
1429 if (! hard_reg_set_equal_p (save_here, BB_INFO (bb)->save_here))
1431 COPY_HARD_REG_SET (BB_INFO (bb)->save_here, save_here);
1432 changed_p = TRUE;
1435 return changed_p;
1438 /* The transfer function used by the DF equation solver to propagate
1439 free info through block with BB_INDEX according to the following
1440 equation:
1442 bb.free_in = ((bb.free_out - bb.save_kill - bb.restore_kill) U bb.free_gen)
1443 - bb.save_here)
1446 static bool
1447 free_trans_fun (int bb_index)
1449 HARD_REG_SET temp_set;
1450 struct bb_info *bb_info = BB_INFO_BY_INDEX (bb_index);
1452 COPY_HARD_REG_SET (temp_set, bb_info->free_out);
1453 AND_COMPL_HARD_REG_SET (temp_set, bb_info->save_kill);
1454 AND_COMPL_HARD_REG_SET (temp_set, bb_info->restore_kill);
1455 IOR_HARD_REG_SET (temp_set, bb_info->free_gen);
1456 AND_COMPL_HARD_REG_SET (temp_set, bb_info->save_here);
1457 if (! hard_reg_set_equal_p (temp_set, bb_info->free_in))
1459 COPY_HARD_REG_SET (bb_info->free_in, temp_set);
1460 return true;
1462 return false;
1465 /* The confluence function used by the DF equation solver to set up
1466 free info for a block BB without successor. */
1467 static void
1468 free_con_fun_0 (basic_block bb)
1470 CLEAR_HARD_REG_SET (BB_INFO (bb)->free_out);
1473 /* The confluence function used by the DF equation solver to propagate
1474 free info from successor to predecessor on edge E (bb->succ)
1475 according to the following equation:
1477 bb.free_out = ^(succ.free_in ^ bb.live_at_end)
1479 static void
1480 free_con_fun_n (edge e)
1482 HARD_REG_SET temp_set;
1483 basic_block succ = e->dest;
1484 basic_block bb = e->src;
1485 struct bb_info *bb_info = BB_INFO (bb);
1487 COPY_HARD_REG_SET (temp_set, BB_INFO (succ)->free_in);
1488 if (EDGE_SUCC (bb, 0) == e)
1489 COPY_HARD_REG_SET (bb_info->free_out, temp_set);
1490 else
1491 AND_HARD_REG_SET (bb_info->free_out, temp_set);
1492 AND_HARD_REG_SET (bb_info->free_out, bb_info->live_at_end);
1495 /* The function calculates global free information according
1496 to the following equations:
1498 bb.free_out = ^(succ.free_in ^ bb.live_at_end)
1499 bb.free_in = ((bb.free_out - bb.save_kill - bb.restore_kill) U bb.free_gen)
1500 - bb.save_here)
1502 The function also calculates free_in_mode and free_out_mode.
1504 static void
1505 calculate_free_in_out (void)
1507 basic_block bb;
1509 FOR_EACH_BB (bb)
1511 struct bb_info *bb_info = BB_INFO (bb);
1513 COPY_HARD_REG_SET (bb_info->free_in, bb_info->save_in);
1514 IOR_HARD_REG_SET (bb_info->free_in, bb_info->restore_in);
1515 COPY_HARD_REG_SET (bb_info->free_out, bb_info->save_out);
1516 IOR_HARD_REG_SET (bb_info->free_out, bb_info->restore_out);
1518 df_simple_dataflow (DF_BACKWARD, NULL, free_con_fun_0, free_con_fun_n,
1519 free_trans_fun, all_blocks,
1520 df_get_postorder (DF_BACKWARD),
1521 df_get_n_blocks (DF_BACKWARD));
1524 /* The function calculates the global save/restore information used to put
1525 save/restore code without generating new blocks. This is a
1526 bidirectional data flow problem (calculation of SAVE_IN and
1527 SAVE_OUT is a backward data flow problem and SAVE_HERE is forward
1528 one; calculation of RESTORE_IN and RESTORE_OUT is a forward data
1529 flow problem and RESTORE_HERE is backward one). It is complicated
1530 by necessity of calculation of modes for saving/restoring callee
1531 clobbered hard registers. */
1532 static void
1533 make_global_save_analysis (void)
1535 basic_block bb;
1536 int iter, changed_p;
1538 all_blocks = BITMAP_ALLOC (NULL);
1539 FOR_ALL_BB (bb)
1541 bitmap_set_bit (all_blocks, bb->index);
1543 calculate_local_save_info ();
1544 for (iter = 1;; iter++)
1546 calculate_restore_in_out ();
1547 changed_p = calculate_restore_here ();
1548 if (! changed_p)
1549 break;
1551 if (dump_file != NULL)
1552 fprintf (dump_file, " Number of global restore analysis iterations %d\n",
1553 iter);
1554 for (iter = 1;; iter++)
1556 calculate_save_in_out ();
1557 changed_p = calculate_save_here ();
1558 if (! changed_p)
1559 break;
1561 if (dump_file != NULL)
1562 fprintf (dump_file, " Number of global save analysis iterations %d\n",
1563 iter);
1564 calculate_free_in_out ();
1565 BITMAP_FREE (all_blocks);
1568 /* Print hard registers in SET to file F. The registers are printed
1569 with its mode given in MODES and corresponding pseudo given in
1570 PSEUDOS. */
1571 static void
1572 print_annotated_hard_reg_set (FILE *f, HARD_REG_SET set,
1573 unsigned char *modes, int *pseudos)
1575 int i;
1577 for (i = 0; i < FIRST_PSEUDO_REGISTER; i++)
1578 if (TEST_HARD_REG_BIT (set, i))
1580 fprintf (f, " %d", i);
1581 if (pseudos[i] >= 0)
1582 fprintf (f, "(%d)", pseudos[i]);
1583 fprintf (f, ":%s %s", GET_MODE_NAME (modes[i]), reg_names[i]);
1587 /* Print hard registers in SET to file F. */
1588 static void
1589 print_hard_reg_set (FILE *f, HARD_REG_SET set)
1591 int i;
1593 for (i = 0; i < FIRST_PSEUDO_REGISTER; i++)
1594 if (TEST_HARD_REG_BIT (set, i))
1595 fprintf (f, " %d %s", i, reg_names[i]);
1598 /* Print the save information for each block to file F. */
1599 static void
1600 print_save_data (FILE *f)
1602 basic_block bb;
1603 struct bb_info *bb_info;
1604 edge e;
1605 edge_iterator ei;
1607 FOR_EACH_BB (bb)
1609 bb_info = BB_INFO (bb);
1610 fprintf (f, "bb %d (preds", bb->index);
1611 FOR_EACH_EDGE (e, ei, bb->preds)
1613 fprintf (f, " %d", e->src->index);
1615 fprintf (f, ") (succs");
1616 FOR_EACH_EDGE (e, ei, bb->succs)
1618 fprintf (f, " %d", e->dest->index);
1620 fprintf (f, ")\n empty_save_out_p=%d", bb_info->empty_save_out_p);
1621 fprintf (f, ", empty_restore_in_p=%d", bb_info->empty_restore_in_p);
1622 fprintf (f, "\n save_in:");
1623 print_annotated_hard_reg_set
1624 (f, bb_info->save_in, bb_info->save_in_mode, bb_info->save_in_pseudo);
1625 fprintf (f, "\n save_out:");
1626 print_annotated_hard_reg_set
1627 (f, bb_info->save_out,
1628 bb_info->save_out_mode, bb_info->save_out_pseudo);
1629 fprintf (f, "\n restore_in:");
1630 print_annotated_hard_reg_set
1631 (f, bb_info->restore_in,
1632 bb_info->restore_in_mode, bb_info->restore_in_pseudo);
1633 fprintf (f, "\n restore_out:");
1634 print_annotated_hard_reg_set
1635 (f, bb_info->restore_out,
1636 bb_info->restore_out_mode, bb_info->restore_out_pseudo);
1637 fprintf (f, "\n free_in:");
1638 print_hard_reg_set (f, bb_info->free_in);
1639 fprintf (f, "\n free_out:");
1640 print_hard_reg_set (f, bb_info->free_out);
1641 fprintf (f, "\n live_at_start:");
1642 print_hard_reg_set (f, bb_info->live_at_start);
1643 fprintf (f, "\n live_at_end:");
1644 print_hard_reg_set (f, bb_info->live_at_end);
1645 fprintf (f, "\n save_here:");
1646 print_hard_reg_set (f, bb_info->save_here);
1647 fprintf (f, "\n restore_here:");
1648 print_hard_reg_set (f, bb_info->restore_here);
1649 fprintf (f, "\n save_kill:");
1650 print_hard_reg_set (f, bb_info->save_kill);
1651 fprintf (f, "\n restore_kill:");
1652 print_hard_reg_set (f, bb_info->restore_kill);
1653 fprintf (f, "\n save_gen:");
1654 print_hard_reg_set (f, bb_info->save_gen);
1655 fprintf (f, "\n restore_gen:");
1656 print_hard_reg_set (f, bb_info->restore_gen);
1657 fprintf (f, "\n free_gen:");
1658 print_hard_reg_set (f, bb_info->save_gen);
1659 fprintf (f, "\n\n");
1663 /* Print the save information for each block to stderr. */
1664 void
1665 debug_save_data (void)
1667 print_save_data (stderr);
1671 /* Setup hard registers in SET to save. Setup also their save modes
1672 in SAVE_MODE from FROM_SAVE_MODE and their pseudos in SAVE_PSEUDO
1673 from FROM_SAVE_PSEUDO. */
1674 static void
1675 set_hard_reg_saved (HARD_REG_SET set, unsigned char *from_saved_mode,
1676 enum machine_mode *save_mode, int *from_saved_pseudo,
1677 int *save_pseudo)
1679 int regno;
1681 n_regs_saved = 0;
1682 COPY_HARD_REG_SET (hard_regs_saved, set);
1683 for (regno = 0; regno < FIRST_PSEUDO_REGISTER; regno++)
1684 if (TEST_HARD_REG_BIT (set, regno))
1686 n_regs_saved++;
1687 save_mode[regno] = from_saved_mode[regno];
1688 save_pseudo[regno] = from_saved_pseudo[regno];
1690 else
1692 save_mode[regno] = VOIDmode;
1693 save_pseudo[regno] = -1;
1699 /* Find the places where hard regs are live across calls and save them. */
1701 void
1702 save_call_clobbered_regs (void)
1704 unsigned int regno;
1705 /* Computed in mark_set_regs, holds all registers set by the current
1706 instruction. */
1707 HARD_REG_SET this_insn_sets, hard_regs_to_save, saved;
1708 struct insn_chain *chain, *last, *next, *prev, *last_restore_chain, *where;
1709 struct bb_info *bb_info;
1710 enum machine_mode save_mode[FIRST_PSEUDO_REGISTER];
1711 loop_p loop;
1712 loop_iterator li;
1713 int save_pseudo[FIRST_PSEUDO_REGISTER];
1714 int free_pseudo[FIRST_PSEUDO_REGISTER];
1716 if (flag_ira && optimize)
1718 /* Do global analysis for better placement of spill code. */
1719 alloc_aux_for_blocks (sizeof (struct bb_info));
1720 FOR_EACH_LOOP (li, loop, 0)
1722 loop->aux = xmalloc (sizeof (struct loop_info));
1723 CLEAR_HARD_REG_SET (LOOP_INFO (loop)->mentioned_regs);
1725 memset (BB_INFO (ENTRY_BLOCK_PTR), 0, sizeof (struct bb_info));
1726 memset (BB_INFO (EXIT_BLOCK_PTR), 0, sizeof (struct bb_info));
1727 make_global_save_analysis ();
1730 CLEAR_HARD_REG_SET (hard_regs_saved);
1731 n_regs_saved = 0;
1733 if (flag_ira && reload_insn_chain != NULL)
1735 bb_info = BB_INFO_BY_INDEX (reload_insn_chain->block);
1736 set_hard_reg_saved (bb_info->restore_in,
1737 bb_info->restore_in_mode, save_mode,
1738 bb_info->restore_in_pseudo, save_pseudo);
1741 last = NULL;
1742 for (chain = reload_insn_chain; chain != 0; chain = next)
1744 rtx insn = chain->insn;
1745 enum rtx_code code = GET_CODE (insn);
1747 last = chain;
1748 next = chain->next;
1750 if (INSN_P (insn))
1752 if (n_regs_saved)
1754 if (!flag_ira && code == JUMP_INSN)
1755 /* Restore all registers if this is a JUMP_INSN. */
1756 COPY_HARD_REG_SET (referenced_regs, hard_regs_saved);
1757 else
1759 CLEAR_HARD_REG_SET (referenced_regs);
1760 CLEAR_HARD_REG_SET (modified_regs);
1761 mark_referenced_regs (PATTERN (insn), FALSE);
1762 AND_HARD_REG_SET (referenced_regs, hard_regs_saved);
1765 /* If some registers have been saved, see if INSN
1766 references any of them. We must restore them before
1767 the insn if so. */
1769 for (regno = 0; regno < FIRST_PSEUDO_REGISTER; regno++)
1770 if (TEST_HARD_REG_BIT (referenced_regs, regno))
1772 unsigned int before = regno;
1774 regno += insert_restore (chain, 1, regno, MOVE_MAX_WORDS,
1775 save_mode, save_pseudo);
1776 if (flag_ira && optimize)
1778 gcc_assert (before == regno);
1779 save_mode[before] = VOIDmode;
1780 save_pseudo[before] = -1;
1785 if (code == CALL_INSN
1786 && ! SIBLING_CALL_P (insn)
1787 && ! find_reg_note (insn, REG_NORETURN, NULL))
1789 HARD_REG_SET used_regs;
1790 reg_set_iterator rsi;
1792 /* Use the register life information in CHAIN to compute which
1793 regs are live during the call. */
1794 REG_SET_TO_HARD_REG_SET (hard_regs_to_save,
1795 &chain->live_throughout);
1796 /* Save hard registers always in the widest mode available. */
1797 for (regno = 0; regno < FIRST_PSEUDO_REGISTER; regno++)
1799 if (TEST_HARD_REG_BIT (hard_regs_to_save, regno))
1800 save_mode[regno] = regno_save_mode[regno][1];
1801 else
1802 save_mode[regno] = VOIDmode;
1803 save_pseudo[regno] = -1;
1806 /* Look through all live pseudos, mark their hard registers
1807 and choose proper mode for saving. */
1808 EXECUTE_IF_SET_IN_REG_SET
1809 (&chain->live_throughout, FIRST_PSEUDO_REGISTER, regno, rsi)
1811 int r = reg_renumber[regno];
1812 int nregs;
1813 enum machine_mode mode;
1815 /* Remember live_throughout can contain spilled
1816 registers when IRA is used. */
1817 if (flag_ira && optimize && r < 0)
1818 continue;
1819 gcc_assert (r >= 0);
1821 nregs = hard_regno_nregs[r][PSEUDO_REGNO_MODE (regno)];
1822 mode = HARD_REGNO_CALLER_SAVE_MODE
1823 (r, nregs, PSEUDO_REGNO_MODE (regno));
1824 if (GET_MODE_BITSIZE (mode)
1825 > GET_MODE_BITSIZE (save_mode[r]))
1826 save_mode[r] = mode;
1827 while (nregs-- > 0)
1829 SET_HARD_REG_BIT (hard_regs_to_save, r + nregs);
1830 save_pseudo[r + nregs] = regno;
1834 /* Record all registers set in this call insn. These don't need
1835 to be saved. N.B. the call insn might set a subreg of a
1836 multi-hard-reg pseudo; then the pseudo is considered live
1837 during the call, but the subreg that is set isn't. */
1838 CLEAR_HARD_REG_SET (this_insn_sets);
1839 note_stores (PATTERN (insn), mark_set_regs, &this_insn_sets);
1841 /* Compute which hard regs must be saved before this call. */
1842 AND_COMPL_HARD_REG_SET (hard_regs_to_save, call_fixed_reg_set);
1843 AND_COMPL_HARD_REG_SET (hard_regs_to_save, this_insn_sets);
1844 AND_COMPL_HARD_REG_SET (hard_regs_to_save, hard_regs_saved);
1845 COPY_HARD_REG_SET (used_regs, call_used_reg_set);
1846 AND_HARD_REG_SET (hard_regs_to_save, used_regs);
1848 if (flag_ira)
1849 IOR_HARD_REG_SET (hard_regs_saved, hard_regs_to_save);
1850 else
1852 for (regno = 0; regno < FIRST_PSEUDO_REGISTER; regno++)
1853 if (TEST_HARD_REG_BIT (hard_regs_to_save, regno))
1854 regno += insert_save (chain, 1, regno,
1855 &hard_regs_to_save, save_mode,
1856 save_pseudo);
1860 /* Must recompute n_regs_saved. */
1861 n_regs_saved = 0;
1862 for (regno = 0; regno < FIRST_PSEUDO_REGISTER; regno++)
1863 if (TEST_HARD_REG_BIT (hard_regs_saved, regno))
1864 n_regs_saved++;
1868 if (chain->next == 0 || chain->next->block != chain->block)
1870 struct bb_info *next_bb_info;
1872 next_bb_info = (chain->next != NULL
1873 ? BB_INFO_BY_INDEX (chain->next->block) : NULL);
1875 /* At the end of the basic block, we must restore any registers that
1876 remain saved. If the last insn in the block is a JUMP_INSN, put
1877 the restore before the insn, otherwise, put it after the insn. */
1879 if (flag_ira && optimize)
1880 set_hard_reg_saved
1881 (BB_INFO_BY_INDEX (chain->block)->restore_here,
1882 BB_INFO_BY_INDEX (chain->block)->restore_out_mode, save_mode,
1883 BB_INFO_BY_INDEX (chain->block)->restore_out_pseudo,
1884 save_pseudo);
1886 if (n_regs_saved)
1887 for (regno = 0; regno < FIRST_PSEUDO_REGISTER; regno++)
1888 if (TEST_HARD_REG_BIT (hard_regs_saved, regno))
1890 unsigned int before = regno;
1892 regno += insert_restore (chain, JUMP_P (insn),
1893 regno, MOVE_MAX_WORDS, save_mode,
1894 save_pseudo);
1895 if (flag_ira && optimize)
1897 gcc_assert (before == regno);
1898 save_mode[before] = VOIDmode;
1899 save_pseudo[before] = -1;
1903 if (flag_ira && optimize && next_bb_info != NULL)
1904 set_hard_reg_saved (next_bb_info->restore_in,
1905 next_bb_info->restore_in_mode, save_mode,
1906 next_bb_info->restore_in_pseudo, save_pseudo);
1911 if (!flag_ira)
1912 return;
1914 CLEAR_HARD_REG_SET (hard_regs_to_save);
1915 n_regs_saved = 0;
1916 last_restore_chain = NULL;
1918 if (last == NULL)
1919 CLEAR_HARD_REG_SET (saved);
1920 else
1922 bb_info = BB_INFO_BY_INDEX (last->block);
1923 set_hard_reg_saved (bb_info->save_out,
1924 bb_info->save_out_mode, save_mode,
1925 bb_info->save_out_pseudo, save_pseudo);
1926 COPY_HARD_REG_SET (hard_regs_to_save, hard_regs_saved);
1927 COPY_HARD_REG_SET (saved, bb_info->free_out);
1928 for (regno = 0; regno < FIRST_PSEUDO_REGISTER; regno++)
1929 if (!TEST_HARD_REG_BIT (saved, regno))
1930 free_pseudo[regno] = -1;
1931 else
1933 if (TEST_HARD_REG_BIT (bb_info->save_out, regno))
1934 free_pseudo [regno] = bb_info->save_out_pseudo [regno];
1935 else
1937 gcc_assert (TEST_HARD_REG_BIT (bb_info->restore_out, regno));
1938 free_pseudo [regno] = bb_info->restore_out_pseudo [regno];
1942 for (chain = last; chain != 0; chain = prev)
1944 rtx insn = chain->insn;
1945 enum rtx_code code = GET_CODE (insn);
1947 prev = chain->prev;
1949 if (INSN_P (insn))
1951 bb_info = BB_INFO_BY_INDEX (chain->block);
1953 CLEAR_HARD_REG_SET (referenced_regs);
1954 CLEAR_HARD_REG_SET (modified_regs);
1955 mark_referenced_regs (PATTERN (insn), FALSE);
1956 AND_HARD_REG_SET (modified_regs, hard_regs_to_save);
1957 AND_COMPL_HARD_REG_SET (saved, referenced_regs);
1958 AND_COMPL_HARD_REG_SET (saved, modified_regs);
1960 if (chain->is_caller_save_insn)
1962 if (last_restore_chain == NULL)
1963 last_restore_chain = chain;
1965 else
1967 /* If some registers have been saved, see if INSN
1968 references any of them. We must restore them before
1969 the insn if so. */
1971 for (regno = 0; regno < FIRST_PSEUDO_REGISTER; regno++)
1972 if (TEST_HARD_REG_BIT (modified_regs, regno))
1974 unsigned int before = regno;
1976 /* We should put save insns before restore insns
1977 between the two calls because the same stack
1978 slot for different hard registers can be used
1979 for restoring in the first call and saving for
1980 the second call. */
1981 regno += insert_save (last_restore_chain != NULL
1982 ? last_restore_chain : chain,
1983 last_restore_chain == NULL
1984 && JUMP_P (chain->insn),
1985 regno, &hard_regs_to_save,
1986 save_mode, save_pseudo);
1987 gcc_assert (before == regno);
1989 CLEAR_HARD_REG_BIT (saved, regno);
1990 CLEAR_HARD_REG_BIT (hard_regs_to_save, regno);
1991 save_mode[before] = VOIDmode;
1992 save_pseudo[before] = -1;
1993 free_pseudo[before] = -1;
1997 for (regno = 0; regno < FIRST_PSEUDO_REGISTER; regno++)
1998 if (TEST_HARD_REG_BIT (saved, regno) && free_pseudo[regno] >= 0)
1999 SET_REGNO_REG_SET (&chain->saved, free_pseudo[regno]);
2000 if (chain->is_caller_save_insn && chain->saved_pseudo_regno >= 0)
2002 int i;
2003 enum machine_mode mode;
2005 mode = GET_MODE (regno_reg_rtx[chain->saved_pseudo_regno]);
2006 regno = reg_renumber[chain->saved_pseudo_regno];
2007 add_to_hard_reg_set (&saved, mode, regno);
2008 for (i = hard_regno_nregs[regno][mode] - 1; i >= 0; i--)
2009 free_pseudo[regno + i] = chain->saved_pseudo_regno;
2011 if (code == CALL_INSN
2012 && ! SIBLING_CALL_P (insn)
2013 && ! find_reg_note (insn, REG_NORETURN, NULL))
2015 HARD_REG_SET used_regs;
2016 reg_set_iterator rsi;
2018 last_restore_chain = NULL;
2020 /* Use the register life information in CHAIN to
2021 compute which regs are live during the call. */
2022 REG_SET_TO_HARD_REG_SET (hard_regs_to_save,
2023 &chain->live_throughout);
2024 /* Save hard registers always in the widest mode
2025 available. */
2026 for (regno = 0; regno < FIRST_PSEUDO_REGISTER; regno++)
2028 if (TEST_HARD_REG_BIT (hard_regs_to_save, regno))
2030 CLEAR_HARD_REG_SET (this_insn_sets);
2031 note_stores (PATTERN (insn), mark_set_regs,
2032 &this_insn_sets);
2033 save_mode[regno] = regno_save_mode[regno][1];
2035 else
2036 save_mode[regno] = VOIDmode;
2037 save_pseudo[regno] = -1;
2040 /* Look through all live pseudos, mark their hard
2041 registers and choose proper mode for saving. */
2042 EXECUTE_IF_SET_IN_REG_SET
2043 (&chain->live_throughout, FIRST_PSEUDO_REGISTER, regno, rsi)
2045 int r = reg_renumber[regno];
2046 int nregs;
2047 enum machine_mode mode;
2049 /* Remember live_throughout can contain spilled
2050 registers when IRA is used. */
2051 if (flag_ira && r < 0)
2052 continue;
2053 gcc_assert (r >= 0);
2055 nregs = hard_regno_nregs[r][PSEUDO_REGNO_MODE (regno)];
2056 mode = HARD_REGNO_CALLER_SAVE_MODE
2057 (r, nregs, PSEUDO_REGNO_MODE (regno));
2058 if (GET_MODE_BITSIZE (mode)
2059 > GET_MODE_BITSIZE (save_mode[r]))
2060 save_mode[r] = mode;
2061 while (nregs-- > 0)
2063 SET_HARD_REG_BIT (hard_regs_to_save, r + nregs);
2064 save_pseudo[r + nregs] = regno;
2065 free_pseudo[r + nregs] = regno;
2069 /* Record all registers set in this call insn. These
2070 don't need to be saved. N.B. the call insn might set
2071 a subreg of a multi-hard-reg pseudo; then the pseudo
2072 is considered live during the call, but the subreg
2073 that is set isn't. */
2074 CLEAR_HARD_REG_SET (this_insn_sets);
2075 note_stores (PATTERN (insn), mark_set_regs, &this_insn_sets);
2077 /* Compute which hard regs must be saved before this call. */
2078 AND_COMPL_HARD_REG_SET (hard_regs_to_save, call_fixed_reg_set);
2079 AND_COMPL_HARD_REG_SET (hard_regs_to_save, this_insn_sets);
2080 COPY_HARD_REG_SET (used_regs, call_used_reg_set);
2081 AND_HARD_REG_SET (hard_regs_to_save, used_regs);
2082 COPY_HARD_REG_SET (saved, hard_regs_to_save);
2083 for (regno = 0; regno < FIRST_PSEUDO_REGISTER; regno++)
2084 if (!TEST_HARD_REG_BIT (saved, regno))
2086 free_pseudo[regno] = -1;
2087 save_pseudo[regno] = -1;
2088 save_mode[regno] = VOIDmode;
2093 if (chain->prev == 0 || chain->prev->block != chain->block)
2095 struct bb_info *prev_bb_info;
2097 prev_bb_info = (chain->prev != NULL
2098 ? BB_INFO_BY_INDEX (chain->prev->block) : NULL);
2100 /* At the start of the basic block, we must save any
2101 registers from save_here. */
2103 set_hard_reg_saved
2104 (BB_INFO_BY_INDEX (chain->block)->save_here,
2105 BB_INFO_BY_INDEX (chain->block)->save_in_mode, save_mode,
2106 BB_INFO_BY_INDEX (chain->block)->save_in_pseudo, save_pseudo);
2107 COPY_HARD_REG_SET (hard_regs_to_save, hard_regs_saved);
2109 where = (last_restore_chain != NULL
2110 ? last_restore_chain->next : chain);
2111 /* An addr_vec is placed outside any basic block but its
2112 chain has the same block as the block of subsequent insn.
2113 So skip to the real start of the basic block. */
2114 if (GET_CODE (where->insn) == CODE_LABEL && where->next != NULL
2115 && JUMP_P (where->next->insn)
2116 && (GET_CODE (PATTERN (where->next->insn)) == ADDR_DIFF_VEC
2117 || GET_CODE (PATTERN (where->next->insn)) == ADDR_VEC)
2118 && where->next->next != NULL)
2119 where = where->next->next;
2121 if (!hard_reg_set_empty_p (hard_regs_to_save))
2122 for (regno = 0; regno < FIRST_PSEUDO_REGISTER; regno++)
2123 if (TEST_HARD_REG_BIT (hard_regs_to_save, regno))
2125 unsigned int before = regno;
2127 regno += insert_save (where, INSN_P (where->insn), regno,
2128 &hard_regs_to_save, save_mode,
2129 save_pseudo);
2130 gcc_assert (before == regno);
2131 save_mode[before] = VOIDmode;
2132 save_pseudo[before] = -1;
2135 if (prev_bb_info != NULL)
2137 last_restore_chain = NULL;
2138 set_hard_reg_saved (prev_bb_info->save_out,
2139 prev_bb_info->save_out_mode, save_mode,
2140 prev_bb_info->save_out_pseudo, save_pseudo);
2141 COPY_HARD_REG_SET (hard_regs_to_save, hard_regs_saved);
2142 COPY_HARD_REG_SET (saved, prev_bb_info->free_out);
2143 for (regno = 0; regno < FIRST_PSEUDO_REGISTER; regno++)
2144 if (!TEST_HARD_REG_BIT (saved, regno))
2145 free_pseudo[regno] = -1;
2146 else
2148 if (TEST_HARD_REG_BIT (prev_bb_info->save_out, regno))
2149 free_pseudo [regno]
2150 = prev_bb_info->save_out_pseudo [regno];
2151 else
2153 gcc_assert (TEST_HARD_REG_BIT (prev_bb_info->restore_out, regno));
2154 free_pseudo [regno]
2155 = prev_bb_info->restore_out_pseudo [regno];
2162 FOR_EACH_LOOP (li, loop, 0)
2164 free (loop->aux);
2166 if (flag_ira && optimize)
2167 free_aux_for_blocks ();
2170 /* Here from note_stores, or directly from save_call_clobbered_regs,
2171 when an insn stores a value in a register. Set the proper bit or
2172 bits in this_insn_sets. */
2173 static void
2174 mark_set_regs (rtx reg, const_rtx setter ATTRIBUTE_UNUSED, void *data)
2176 int regno, endregno, i;
2177 HARD_REG_SET *this_insn_sets = (HARD_REG_SET *) data;
2179 if (GET_CODE (reg) == SUBREG)
2181 rtx inner = SUBREG_REG (reg);
2182 if (!REG_P (inner))
2183 return;
2184 if ((regno = REGNO (inner)) >= FIRST_PSEUDO_REGISTER)
2186 if (reg_renumber[regno] < 0)
2187 return;
2188 regno = reg_renumber[regno];
2189 endregno
2190 = hard_regno_nregs[regno][PSEUDO_REGNO_MODE (REGNO (inner))];
2192 else
2194 regno = subreg_regno (reg);
2195 endregno = regno + subreg_nregs (reg);
2198 else if (REG_P (reg))
2200 if ((regno = REGNO (reg)) < FIRST_PSEUDO_REGISTER)
2201 endregno = END_HARD_REGNO (reg);
2202 else
2204 if (reg_renumber[regno] < 0)
2205 return;
2206 regno = reg_renumber[regno];
2207 endregno
2208 = hard_regno_nregs[regno][PSEUDO_REGNO_MODE (REGNO (reg))];
2211 else
2212 return;
2214 for (i = regno; i < endregno; i++)
2215 SET_HARD_REG_BIT (*this_insn_sets, i);
2218 /* Here from note_stores when an insn stores a value in a register.
2219 Set the proper bit or bits in the passed regset. All pseudos that have
2220 been assigned hard regs have had their register number changed already,
2221 so we can ignore pseudos. */
2222 static void
2223 add_stored_regs (rtx reg, const_rtx setter, void *data)
2225 int regno, endregno, i;
2226 enum machine_mode mode = GET_MODE (reg);
2227 int offset = 0;
2229 if (GET_CODE (setter) == CLOBBER)
2230 return;
2232 if (GET_CODE (reg) == SUBREG
2233 && REG_P (SUBREG_REG (reg))
2234 && REGNO (SUBREG_REG (reg)) < FIRST_PSEUDO_REGISTER)
2236 offset = subreg_regno_offset (REGNO (SUBREG_REG (reg)),
2237 GET_MODE (SUBREG_REG (reg)),
2238 SUBREG_BYTE (reg),
2239 GET_MODE (reg));
2240 regno = REGNO (SUBREG_REG (reg)) + offset;
2241 endregno = regno + subreg_nregs (reg);
2243 else
2245 if (!REG_P (reg) || REGNO (reg) >= FIRST_PSEUDO_REGISTER)
2246 return;
2248 regno = REGNO (reg) + offset;
2249 endregno = end_hard_regno (mode, regno);
2252 for (i = regno; i < endregno; i++)
2253 SET_REGNO_REG_SET ((regset) data, i);
2256 /* Walk X and record all referenced registers in REFERENCED_REG and
2257 modified registers in MODIFIED_REGS. */
2258 static void
2259 mark_referenced_regs (rtx x, int set_p)
2261 enum rtx_code code;
2262 const char *fmt;
2263 int i, j;
2264 int stop_p;
2266 for (stop_p = FALSE; !stop_p; )
2268 while (GET_CODE (x) == STRICT_LOW_PART || GET_CODE (x) == ZERO_EXTRACT)
2269 x = XEXP (x, 0);
2270 code = GET_CODE (x);
2271 switch (code)
2273 case SET:
2274 gcc_assert (!set_p);
2275 mark_referenced_regs (SET_DEST (x), TRUE);
2276 x = SET_SRC (x);
2277 break;
2278 case CLOBBER:
2279 mark_referenced_regs (SET_DEST (x), TRUE);
2280 return;
2281 case PRE_INC:
2282 case POST_INC:
2283 case PRE_DEC:
2284 case POST_DEC:
2285 gcc_assert (!set_p);
2286 x = XEXP (x, 0);
2287 mark_referenced_regs (x, TRUE);
2288 break;
2289 case PRE_MODIFY:
2290 case POST_MODIFY:
2291 set_p = FALSE;
2292 mark_referenced_regs (XEXP (x, 0), FALSE);
2293 mark_referenced_regs (XEXP (x, 0), TRUE);
2294 x = XEXP (x, 1);
2295 mark_referenced_regs (x, FALSE);
2296 break;
2297 case SUBREG:
2298 x = SUBREG_REG (x);
2299 break;
2300 case REG:
2302 int regno = REGNO (x);
2303 int hardregno = (regno < FIRST_PSEUDO_REGISTER ? regno
2304 : reg_renumber[regno]);
2306 if (hardregno >= 0)
2308 if (set_p)
2309 add_to_hard_reg_set (&modified_regs, GET_MODE (x), hardregno);
2310 add_to_hard_reg_set (&referenced_regs, GET_MODE (x), hardregno);
2312 /* If this is a pseudo that did not get a hard register, scan
2313 its memory location, since it might involve the use of
2314 another register, which might be saved. */
2315 else if (reg_equiv_mem[regno] != 0)
2316 mark_referenced_regs (XEXP (reg_equiv_mem[regno], 0), set_p);
2317 else if (reg_equiv_address[regno] != 0)
2318 mark_referenced_regs (reg_equiv_address[regno], set_p);
2319 return;
2321 default:
2322 fmt = GET_RTX_FORMAT (code);
2323 for (i = GET_RTX_LENGTH (code) - 1; i >= 0; i--)
2325 if (fmt[i] == 'e')
2326 mark_referenced_regs (XEXP (x, i), FALSE);
2327 else if (fmt[i] == 'E')
2328 for (j = XVECLEN (x, i) - 1; j >= 0; j--)
2329 mark_referenced_regs (XVECEXP (x, i, j), FALSE);
2331 stop_p = TRUE;
2332 break;
2337 /* Insert a sequence of insns to restore. Place these insns in front
2338 of CHAIN if BEFORE_P is nonzero, behind the insn otherwise.
2339 MAXRESTORE is the maximum number of registers which should be
2340 restored during this call. It should never be less than 1 since we
2341 only work with entire registers. SAVE_PSEUDO maps hard registers
2342 into the corresponding pseudos.
2344 Note that we have verified in init_caller_save that we can do this
2345 with a simple SET, so use it. Set INSN_CODE to what we save there
2346 since the address might not be valid so the insn might not be recognized.
2347 These insns will be reloaded and have register elimination done by
2348 find_reload, so we need not worry about that here.
2350 Return the extra number of registers saved. */
2352 static int
2353 insert_restore (struct insn_chain *chain, int before_p, int regno,
2354 int maxrestore, enum machine_mode *save_mode,
2355 int *save_pseudo)
2357 int i, k;
2358 rtx pat = NULL_RTX;
2359 int code;
2360 unsigned int numregs = 0;
2361 struct insn_chain *new_chain;
2362 rtx mem;
2364 /* A common failure mode if register status is not correct in the
2365 RTL is for this routine to be called with a REGNO we didn't
2366 expect to save. That will cause us to write an insn with a (nil)
2367 SET_DEST or SET_SRC. Instead of doing so and causing a crash
2368 later, check for this common case here instead. This will remove
2369 one step in debugging such problems. */
2370 gcc_assert (regno_save_mem[regno][1]);
2372 /* Get the pattern to emit and update our status.
2374 See if we can restore `maxrestore' registers at once. Work
2375 backwards to the single register case. */
2376 for (i = maxrestore; i > 0; i--)
2378 int j;
2379 int ok = 1;
2381 if (regno_save_mem[regno][i] == 0)
2382 continue;
2384 for (j = 0; j < i; j++)
2385 if (! TEST_HARD_REG_BIT (hard_regs_saved, regno + j))
2387 ok = 0;
2388 break;
2390 /* Must do this one restore at a time. */
2391 if (! ok)
2392 continue;
2394 numregs = i;
2395 break;
2398 mem = regno_save_mem[regno][numregs];
2399 if (save_mode[regno] != VOIDmode
2400 && save_mode[regno] != GET_MODE (mem)
2401 && numregs == (unsigned int) hard_regno_nregs[regno][save_mode[regno]]
2402 && reg_save_code (regno, save_mode[regno]) >= 0)
2403 mem = adjust_address_nv (mem, save_mode[regno], 0);
2404 else
2405 mem = copy_rtx (mem);
2407 /* Verify that the alignment of spill space is equal to or greater
2408 than required. */
2409 gcc_assert (MIN (PREFERRED_STACK_BOUNDARY,
2410 GET_MODE_ALIGNMENT (GET_MODE (mem))) <= MEM_ALIGN (mem));
2412 pat = gen_rtx_SET (VOIDmode,
2413 gen_rtx_REG (GET_MODE (mem),
2414 regno), mem);
2415 code = reg_restore_code (regno, GET_MODE (mem));
2416 new_chain = insert_one_insn (chain, before_p, code, pat);
2417 new_chain->saved_pseudo_regno = save_pseudo[regno];
2419 if (dump_file != NULL)
2420 fprintf (dump_file, "inserting restore insn %u for pseudo %d %s %u\n",
2421 INSN_UID (new_chain->insn), save_pseudo[regno],
2422 before_p ? "before" : "after", INSN_UID (chain->insn));
2424 /* Clear status for all registers we restored. */
2425 for (k = 0; k < i; k++)
2427 CLEAR_HARD_REG_BIT (hard_regs_saved, regno + k);
2428 SET_REGNO_REG_SET (&new_chain->dead_or_set, regno + k);
2429 n_regs_saved--;
2432 /* Tell our callers how many extra registers we saved/restored. */
2433 return numregs - 1;
2436 /* Like insert_restore above, but save registers instead. */
2438 static int
2439 insert_save (struct insn_chain *chain, int before_p, int regno,
2440 HARD_REG_SET (*to_save), enum machine_mode *save_mode,
2441 int *save_pseudo)
2443 int i;
2444 unsigned int k;
2445 rtx pat = NULL_RTX;
2446 int code;
2447 unsigned int numregs = 0;
2448 struct insn_chain *new_chain;
2449 rtx mem;
2451 /* A common failure mode if register status is not correct in the
2452 RTL is for this routine to be called with a REGNO we didn't
2453 expect to save. That will cause us to write an insn with a (nil)
2454 SET_DEST or SET_SRC. Instead of doing so and causing a crash
2455 later, check for this common case here. This will remove one
2456 step in debugging such problems. */
2457 gcc_assert (regno_save_mem[regno][1]);
2459 /* Get the pattern to emit and update our status.
2461 See if we can save several registers with a single instruction.
2462 Work backwards to the single register case. */
2463 for (i = MOVE_MAX_WORDS; i > 0; i--)
2465 int j;
2466 int ok = 1;
2467 if (regno_save_mem[regno][i] == 0)
2468 continue;
2470 for (j = 0; j < i; j++)
2471 if (! TEST_HARD_REG_BIT (*to_save, regno + j))
2473 ok = 0;
2474 break;
2476 /* Must do this one save at a time. */
2477 if (! ok)
2478 continue;
2480 numregs = i;
2481 break;
2484 mem = regno_save_mem[regno][numregs];
2485 if (save_mode[regno] != VOIDmode
2486 && save_mode[regno] != GET_MODE (mem)
2487 && numregs == (unsigned int) hard_regno_nregs[regno][save_mode[regno]]
2488 && reg_save_code (regno, save_mode[regno]) >= 0)
2489 mem = adjust_address_nv (mem, save_mode[regno], 0);
2490 else
2491 mem = copy_rtx (mem);
2493 /* Verify that the alignment of spill space is equal to or greater
2494 than required. */
2495 gcc_assert (MIN (PREFERRED_STACK_BOUNDARY,
2496 GET_MODE_ALIGNMENT (GET_MODE (mem))) <= MEM_ALIGN (mem));
2498 pat = gen_rtx_SET (VOIDmode, mem,
2499 gen_rtx_REG (GET_MODE (mem),
2500 regno));
2501 code = reg_save_code (regno, GET_MODE (mem));
2502 new_chain = insert_one_insn (chain, before_p, code, pat);
2503 new_chain->saved_pseudo_regno = save_pseudo[regno];
2505 if (dump_file != NULL)
2506 fprintf (dump_file, "inserting save insn %u for pseudo %d %s %u\n",
2507 INSN_UID (new_chain->insn), save_pseudo[regno],
2508 before_p ? "before" : "after", INSN_UID (chain->insn));
2510 /* Set hard_regs_saved and dead_or_set for all the registers we saved. */
2511 for (k = 0; k < numregs; k++)
2513 SET_HARD_REG_BIT (hard_regs_saved, regno + k);
2514 SET_REGNO_REG_SET (&new_chain->dead_or_set, regno + k);
2515 n_regs_saved++;
2518 /* Tell our callers how many extra registers we saved/restored. */
2519 return numregs - 1;
2522 /* Emit a new caller-save insn and set the code. */
2523 static struct insn_chain *
2524 insert_one_insn (struct insn_chain *chain, int before_p, int code, rtx pat)
2526 rtx insn = chain->insn;
2527 struct insn_chain *new_chain;
2529 #ifdef HAVE_cc0
2530 /* If INSN references CC0, put our insns in front of the insn that sets
2531 CC0. This is always safe, since the only way we could be passed an
2532 insn that references CC0 is for a restore, and doing a restore earlier
2533 isn't a problem. We do, however, assume here that CALL_INSNs don't
2534 reference CC0. Guard against non-INSN's like CODE_LABEL. */
2536 if ((NONJUMP_INSN_P (insn) || JUMP_P (insn))
2537 && before_p
2538 && reg_referenced_p (cc0_rtx, PATTERN (insn)))
2539 chain = chain->prev, insn = chain->insn;
2540 #endif
2542 new_chain = new_insn_chain ();
2543 if (before_p)
2545 rtx link;
2547 new_chain->prev = chain->prev;
2548 if (new_chain->prev != 0)
2549 new_chain->prev->next = new_chain;
2550 else
2551 reload_insn_chain = new_chain;
2553 chain->prev = new_chain;
2554 new_chain->next = chain;
2555 new_chain->insn = emit_insn_before (pat, insn);
2556 /* ??? It would be nice if we could exclude the already / still saved
2557 registers from the live sets. */
2558 COPY_REG_SET (&new_chain->live_throughout, &chain->live_throughout);
2559 /* Registers that die in CHAIN->INSN still live in the new insn. */
2560 for (link = REG_NOTES (chain->insn); link; link = XEXP (link, 1))
2562 if (REG_NOTE_KIND (link) == REG_DEAD)
2564 rtx reg = XEXP (link, 0);
2565 int regno, i;
2567 gcc_assert (REG_P (reg));
2568 regno = REGNO (reg);
2569 if (regno >= FIRST_PSEUDO_REGISTER)
2570 regno = reg_renumber[regno];
2571 if (regno < 0)
2572 continue;
2573 for (i = hard_regno_nregs[regno][GET_MODE (reg)] - 1;
2574 i >= 0; i--)
2575 SET_REGNO_REG_SET (&new_chain->live_throughout, regno + i);
2579 /* If CHAIN->INSN is a call, then the registers which contain
2580 the arguments to the function are live in the new insn. */
2581 if (CALL_P (chain->insn))
2583 for (link = CALL_INSN_FUNCTION_USAGE (chain->insn);
2584 link != NULL_RTX;
2585 link = XEXP (link, 1))
2587 rtx arg = XEXP (link, 0);
2589 if (GET_CODE (arg) == USE)
2591 rtx reg = XEXP (arg, 0);
2593 if (REG_P (reg))
2595 int i, regno = REGNO (reg);
2597 /* Registers in CALL_INSN_FUNCTION_USAGE are always
2598 hard registers. */
2599 gcc_assert (regno < FIRST_PSEUDO_REGISTER);
2601 for (i = hard_regno_nregs[regno][GET_MODE (reg)] - 1;
2602 i >= 0; i--)
2603 SET_REGNO_REG_SET (&new_chain->live_throughout, regno + i);
2610 CLEAR_REG_SET (&new_chain->dead_or_set);
2611 if (chain->insn == BB_HEAD (BASIC_BLOCK (chain->block)))
2612 BB_HEAD (BASIC_BLOCK (chain->block)) = new_chain->insn;
2614 else
2616 new_chain->next = chain->next;
2617 if (new_chain->next != 0)
2618 new_chain->next->prev = new_chain;
2619 chain->next = new_chain;
2620 new_chain->prev = chain;
2621 if (GET_CODE (insn) != CODE_LABEL)
2622 new_chain->insn = emit_insn_after (pat, insn);
2623 else
2625 /* Put the insn after bb note in a empty basic block. */
2626 gcc_assert (NEXT_INSN (insn) && NOTE_P (NEXT_INSN (insn)));
2627 new_chain->insn = emit_insn_after (pat, NEXT_INSN (insn));
2629 /* ??? It would be nice if we could exclude the already / still saved
2630 registers from the live sets, and observe REG_UNUSED notes. */
2631 COPY_REG_SET (&new_chain->live_throughout, &chain->live_throughout);
2632 /* Registers that are set in CHAIN->INSN live in the new insn.
2633 (Unless there is a REG_UNUSED note for them, but we don't
2634 look for them here.) */
2635 if (INSN_P (chain->insn))
2636 note_stores (PATTERN (chain->insn), add_stored_regs,
2637 &new_chain->live_throughout);
2638 CLEAR_REG_SET (&new_chain->dead_or_set);
2639 if (chain->insn == BB_END (BASIC_BLOCK (chain->block)))
2640 BB_END (BASIC_BLOCK (chain->block)) = new_chain->insn;
2642 new_chain->block = chain->block;
2643 new_chain->is_caller_save_insn = 1;
2645 INSN_CODE (new_chain->insn) = code;
2646 return new_chain;
2648 #include "gt-caller-save.h"