2008-07-03 Vladimir Makarov <vmakarov@redhat.com>
[official-gcc.git] / gcc / caller-save.c
bloba191f6d8f745b723132d64649dafd3796cc1a91b
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 "ira.h"
41 #include "df.h"
42 #include "ggc.h"
44 /* Call used hard registers which can not be saved because there is no
45 insn for this. */
46 HARD_REG_SET no_caller_save_reg_set;
48 #ifndef MAX_MOVE_MAX
49 #define MAX_MOVE_MAX MOVE_MAX
50 #endif
52 #ifndef MIN_UNITS_PER_WORD
53 #define MIN_UNITS_PER_WORD UNITS_PER_WORD
54 #endif
56 #define MOVE_MAX_WORDS (MOVE_MAX / UNITS_PER_WORD)
58 /* Modes for each hard register that we can save. The smallest mode is wide
59 enough to save the entire contents of the register. When saving the
60 register because it is live we first try to save in multi-register modes.
61 If that is not possible the save is done one register at a time. */
63 static enum machine_mode
64 regno_save_mode[FIRST_PSEUDO_REGISTER][MAX_MOVE_MAX / MIN_UNITS_PER_WORD + 1];
66 /* For each hard register, a place on the stack where it can be saved,
67 if needed. */
69 static rtx
70 regno_save_mem[FIRST_PSEUDO_REGISTER][MAX_MOVE_MAX / MIN_UNITS_PER_WORD + 1];
72 /* The number of elements in the subsequent array. */
73 static int save_slots_num;
75 /* Allocated slots so far. */
76 static rtx save_slots[FIRST_PSEUDO_REGISTER];
78 /* We will only make a register eligible for caller-save if it can be
79 saved in its widest mode with a simple SET insn as long as the memory
80 address is valid. We record the INSN_CODE is those insns here since
81 when we emit them, the addresses might not be valid, so they might not
82 be recognized. */
84 static int
85 cached_reg_save_code[FIRST_PSEUDO_REGISTER][MAX_MACHINE_MODE];
86 static int
87 cached_reg_restore_code[FIRST_PSEUDO_REGISTER][MAX_MACHINE_MODE];
89 /* Set of hard regs currently residing in save area (during insn scan). */
91 static HARD_REG_SET hard_regs_saved;
93 /* Number of registers currently in hard_regs_saved. */
95 static int n_regs_saved;
97 /* Computed by mark_referenced_regs, all regs referenced in a given
98 insn. */
99 static HARD_REG_SET referenced_regs;
100 /* Computed by mark_referenced_regs, all regs modified in a given insn. */
101 static HARD_REG_SET modified_regs;
104 static int reg_save_code (int, enum machine_mode);
105 static int reg_restore_code (int, enum machine_mode);
107 struct saved_hard_reg;
108 static void initiate_saved_hard_regs (void);
109 static struct saved_hard_reg *new_saved_hard_reg (int, int);
110 static void finish_saved_hard_regs (void);
111 static int saved_hard_reg_compare_func (const void *, const void *);
112 static void calculate_local_save_info (void);
113 static bool restore_trans_fun (int);
114 static void restore_con_fun_0 (basic_block);
115 static void restore_con_fun_n (edge);
116 static void calculate_restore_in_out (void);
117 static int calculate_restore_here (void);
118 static bool save_trans_fun (int);
119 static void save_con_fun_0 (basic_block);
120 static void save_con_fun_n (edge);
121 static void calculate_save_in_out (void);
122 static int calculate_save_here (void);
123 static bool free_trans_fun (int);
124 static void free_con_fun_0 (basic_block);
125 static void free_con_fun_n (edge);
126 static void calculate_free_in_out (void);
128 static void make_global_save_analysis (void);
129 static void print_annotated_hard_reg_set (FILE *, HARD_REG_SET,
130 unsigned char *, int *);
131 static void print_hard_reg_set (FILE *, HARD_REG_SET);
132 static void print_save_data (FILE *);
133 static void set_hard_reg_saved (HARD_REG_SET, unsigned char *,
134 enum machine_mode *, int *, int *);
136 static void mark_set_regs (rtx, const_rtx, void *);
137 static void add_stored_regs (rtx, const_rtx, void *);
138 static void mark_referenced_regs (rtx, int);
139 static int insert_save (struct insn_chain *, int, int, HARD_REG_SET *,
140 enum machine_mode *, int *);
141 static int insert_restore (struct insn_chain *, int, int, int,
142 enum machine_mode *, int *);
143 static struct insn_chain *insert_one_insn (struct insn_chain *, int, int,
144 rtx);
145 static void add_stored_regs (rtx, const_rtx, void *);
149 static GTY(()) rtx savepat;
150 static GTY(()) rtx restpat;
151 static GTY(()) rtx test_reg;
152 static GTY(()) rtx test_mem;
153 static GTY(()) rtx saveinsn;
154 static GTY(()) rtx restinsn;
156 /* Return the INSN_CODE used to save register REG in mode MODE. */
157 static int
158 reg_save_code (int reg, enum machine_mode mode)
160 bool ok;
161 if (cached_reg_save_code[reg][mode])
162 return cached_reg_save_code[reg][mode];
163 if (!HARD_REGNO_MODE_OK (reg, mode))
165 cached_reg_save_code[reg][mode] = -1;
166 cached_reg_restore_code[reg][mode] = -1;
167 return -1;
170 /* Update the register number and modes of the register
171 and memory operand. */
172 SET_REGNO (test_reg, reg);
173 PUT_MODE (test_reg, mode);
174 PUT_MODE (test_mem, mode);
176 /* Force re-recognition of the modified insns. */
177 INSN_CODE (saveinsn) = -1;
178 INSN_CODE (restinsn) = -1;
180 cached_reg_save_code[reg][mode] = recog_memoized (saveinsn);
181 cached_reg_restore_code[reg][mode] = recog_memoized (restinsn);
183 /* Now extract both insns and see if we can meet their
184 constraints. */
185 ok = (cached_reg_save_code[reg][mode] != -1
186 && cached_reg_restore_code[reg][mode] != -1);
187 if (ok)
189 extract_insn (saveinsn);
190 ok = constrain_operands (1);
191 extract_insn (restinsn);
192 ok &= constrain_operands (1);
195 if (! ok)
197 cached_reg_save_code[reg][mode] = -1;
198 cached_reg_restore_code[reg][mode] = -1;
200 gcc_assert (cached_reg_save_code[reg][mode]);
201 return cached_reg_save_code[reg][mode];
204 /* Return the INSN_CODE used to restore register REG in mode MODE. */
205 static int
206 reg_restore_code (int reg, enum machine_mode mode)
208 if (cached_reg_restore_code[reg][mode])
209 return cached_reg_restore_code[reg][mode];
210 /* Populate our cache. */
211 reg_save_code (reg, mode);
212 return cached_reg_restore_code[reg][mode];
215 /* Initialize for caller-save.
217 Look at all the hard registers that are used by a call and for which
218 regclass.c has not already excluded from being used across a call.
220 Ensure that we can find a mode to save the register and that there is a
221 simple insn to save and restore the register. This latter check avoids
222 problems that would occur if we tried to save the MQ register of some
223 machines directly into memory. */
225 void
226 init_caller_save (void)
228 rtx addr_reg;
229 int offset;
230 rtx address;
231 int i, j;
233 CLEAR_HARD_REG_SET (no_caller_save_reg_set);
234 /* First find all the registers that we need to deal with and all
235 the modes that they can have. If we can't find a mode to use,
236 we can't have the register live over calls. */
238 for (i = 0; i < FIRST_PSEUDO_REGISTER; i++)
240 if (call_used_regs[i] && ! call_fixed_regs[i])
242 for (j = 1; j <= MOVE_MAX_WORDS; j++)
244 regno_save_mode[i][j] = HARD_REGNO_CALLER_SAVE_MODE (i, j,
245 VOIDmode);
246 if (regno_save_mode[i][j] == VOIDmode && j == 1)
248 call_fixed_regs[i] = 1;
249 SET_HARD_REG_BIT (call_fixed_reg_set, i);
253 else
254 regno_save_mode[i][1] = VOIDmode;
257 /* The following code tries to approximate the conditions under which
258 we can easily save and restore a register without scratch registers or
259 other complexities. It will usually work, except under conditions where
260 the validity of an insn operand is dependent on the address offset.
261 No such cases are currently known.
263 We first find a typical offset from some BASE_REG_CLASS register.
264 This address is chosen by finding the first register in the class
265 and by finding the smallest power of two that is a valid offset from
266 that register in every mode we will use to save registers. */
268 for (i = 0; i < FIRST_PSEUDO_REGISTER; i++)
269 if (TEST_HARD_REG_BIT
270 (reg_class_contents
271 [(int) base_reg_class (regno_save_mode[i][1], PLUS, CONST_INT)], i))
272 break;
274 gcc_assert (i < FIRST_PSEUDO_REGISTER);
276 addr_reg = gen_rtx_REG (Pmode, i);
278 for (offset = 1 << (HOST_BITS_PER_INT / 2); offset; offset >>= 1)
280 address = gen_rtx_PLUS (Pmode, addr_reg, GEN_INT (offset));
282 for (i = 0; i < FIRST_PSEUDO_REGISTER; i++)
283 if (regno_save_mode[i][1] != VOIDmode
284 && ! strict_memory_address_p (regno_save_mode[i][1], address))
285 break;
287 if (i == FIRST_PSEUDO_REGISTER)
288 break;
291 /* If we didn't find a valid address, we must use register indirect. */
292 if (offset == 0)
293 address = addr_reg;
295 /* Next we try to form an insn to save and restore the register. We
296 see if such an insn is recognized and meets its constraints.
298 To avoid lots of unnecessary RTL allocation, we construct all the RTL
299 once, then modify the memory and register operands in-place. */
301 test_reg = gen_rtx_REG (VOIDmode, 0);
302 test_mem = gen_rtx_MEM (VOIDmode, address);
303 savepat = gen_rtx_SET (VOIDmode, test_mem, test_reg);
304 restpat = gen_rtx_SET (VOIDmode, test_reg, test_mem);
306 saveinsn = gen_rtx_INSN (VOIDmode, 0, 0, 0, 0, 0, savepat, -1, 0);
307 restinsn = gen_rtx_INSN (VOIDmode, 0, 0, 0, 0, 0, restpat, -1, 0);
309 for (i = 0; i < FIRST_PSEUDO_REGISTER; i++)
310 for (j = 1; j <= MOVE_MAX_WORDS; j++)
311 if (reg_save_code (i,regno_save_mode[i][j]) == -1)
313 regno_save_mode[i][j] = VOIDmode;
314 if (j == 1)
316 call_fixed_regs[i] = 1;
317 SET_HARD_REG_BIT (call_fixed_reg_set, i);
318 if (call_used_regs[i])
319 SET_HARD_REG_BIT (no_caller_save_reg_set, i);
326 /* Initialize save areas by showing that we haven't allocated any yet. */
328 void
329 init_save_areas (void)
331 int i, j;
333 for (i = 0; i < FIRST_PSEUDO_REGISTER; i++)
334 for (j = 1; j <= MOVE_MAX_WORDS; j++)
335 regno_save_mem[i][j] = 0;
336 save_slots_num = 0;
340 /* The structure represents a hard register which should be saved
341 through the call. It is used when the integrated register
342 allocator (IRA) is used and sharing save slots is on. */
343 struct saved_hard_reg
345 /* Order number starting with 0. */
346 int num;
347 /* The hard regno. */
348 int hard_regno;
349 /* Execution frequency of all calls through which given hard
350 register should be saved. */
351 int call_freq;
352 /* Stack slot reserved to save the hard register through calls. */
353 rtx slot;
354 /* True if it is first hard register in the chain of hard registers
355 sharing the same stack slot. */
356 int first_p;
357 /* Order number of the next hard register structure with the same
358 slot in the chain. -1 represents end of the chain. */
359 int next;
362 /* Map: hard register number to the corresponding structure. */
363 static struct saved_hard_reg *hard_reg_map[FIRST_PSEUDO_REGISTER];
365 /* The number of all structures representing hard registers should be
366 saved, in order words, the number of used elements in the following
367 array. */
368 static int saved_regs_num;
370 /* Pointers to all the structures. Index is the order number of the
371 corresponding structure. */
372 static struct saved_hard_reg *all_saved_regs[FIRST_PSEUDO_REGISTER];
374 /* First called function for work with saved hard registers. */
375 static void
376 initiate_saved_hard_regs (void)
378 int i;
380 saved_regs_num = 0;
381 for (i = 0; i < FIRST_PSEUDO_REGISTER; i++)
382 hard_reg_map[i] = NULL;
385 /* Allocate and return new saved hard register with given REGNO and
386 CALL_FREQ. */
387 static struct saved_hard_reg *
388 new_saved_hard_reg (int regno, int call_freq)
390 struct saved_hard_reg *saved_reg;
392 saved_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 struct saved_hard_reg *p1 = *(struct saved_hard_reg **) v1p;
418 struct saved_hard_reg *p2 = *(struct saved_hard_reg **) 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 = 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 SET_HARD_REG_SET (bb_info->save_in);
1081 AND_COMPL_HARD_REG_SET (bb_info->save_in, call_fixed_reg_set);
1082 SET_HARD_REG_SET (bb_info->restore_out);
1083 AND_COMPL_HARD_REG_SET (bb_info->restore_out, call_fixed_reg_set);
1084 SET_HARD_REG_SET (bb_info->free_in);
1085 AND_COMPL_HARD_REG_SET (bb_info->free_in, call_fixed_reg_set);
1086 /* We don't use LIVE for IRA. */
1087 REG_SET_TO_HARD_REG_SET
1088 (bb_info->live_at_end,
1089 flag_ira ? DF_LR_OUT (bb) : DF_LIVE_OUT (bb));
1090 EXECUTE_IF_SET_IN_REG_SET
1091 ((flag_ira ? DF_LR_OUT (bb) : DF_LIVE_OUT (bb)),
1092 FIRST_PSEUDO_REGISTER, regno, rsi)
1094 int r = reg_renumber[regno];
1095 int nregs;
1097 if (r < 0)
1098 continue;
1099 nregs = hard_regno_nregs[r][PSEUDO_REGNO_MODE (regno)];
1100 while (nregs-- > 0)
1101 SET_HARD_REG_BIT (bb_info->live_at_end, r + nregs);
1103 REG_SET_TO_HARD_REG_SET
1104 (bb_info->live_at_start,
1105 flag_ira ? DF_LR_IN (bb) : DF_LIVE_IN (bb));
1106 EXECUTE_IF_SET_IN_REG_SET
1107 ((flag_ira ? DF_LR_IN (bb) : DF_LIVE_IN (bb)),
1108 FIRST_PSEUDO_REGISTER, regno, rsi)
1110 int r = reg_renumber[regno];
1111 int nregs;
1113 if (r < 0)
1114 continue;
1115 nregs = hard_regno_nregs[r][PSEUDO_REGNO_MODE (regno)];
1116 while (nregs-- > 0)
1117 SET_HARD_REG_BIT (bb_info->live_at_start, r + nregs);
1119 CLEAR_HARD_REG_SET (bb_info->save_here);
1120 CLEAR_HARD_REG_SET (bb_info->restore_here);
1121 CLEAR_HARD_REG_SET (save_gen);
1122 CLEAR_HARD_REG_SET (restore_gen);
1123 CLEAR_HARD_REG_SET (free_gen);
1124 CLEAR_HARD_REG_SET (save_kill);
1125 CLEAR_HARD_REG_SET (restore_kill);
1126 for (i = 0; i < FIRST_PSEUDO_REGISTER; i++)
1128 save_mode[i] = VOIDmode;
1129 save_pseudo[i] = -1;
1135 /* The transfer function used by the DF equation solver to propagate restore
1136 info through block with BB_INDEX according to the following
1137 equation:
1139 bb.restore_out = (bb.restore_in - bb.restore_kill) U bb.restore_gen.
1141 The function also propagates saving modes of the hard
1142 registers. */
1143 static bool
1144 restore_trans_fun (int bb_index)
1146 int i;
1147 HARD_REG_SET temp_set;
1148 struct bb_info *bb_info = BB_INFO_BY_INDEX (bb_index);
1150 COPY_HARD_REG_SET (temp_set, bb_info->restore_in);
1151 AND_COMPL_HARD_REG_SET (temp_set, bb_info->restore_kill);
1152 for (i = 0; i < FIRST_PSEUDO_REGISTER; i++)
1153 if (TEST_HARD_REG_BIT (temp_set, i)
1154 && ! TEST_HARD_REG_BIT (bb_info->restore_gen, i))
1156 gcc_assert (bb_info->restore_out_mode[i] == VOIDmode
1157 || ((bb_info->restore_out_mode[i]
1158 == bb_info->restore_in_mode[i])
1159 && (bb_info->restore_out_pseudo[i]
1160 == bb_info->restore_in_pseudo[i])));
1161 bb_info->restore_out_mode[i] = bb_info->restore_in_mode[i];
1162 bb_info->restore_out_pseudo[i] = bb_info->restore_in_pseudo[i];
1164 IOR_HARD_REG_SET (temp_set, bb_info->restore_gen);
1165 if (! hard_reg_set_equal_p (temp_set, bb_info->restore_out))
1167 COPY_HARD_REG_SET (bb_info->restore_out, temp_set);
1168 return true;
1170 return false;
1173 /* The confluence function used by the DF equation solver to set up restore info
1174 for a block BB without predecessor. */
1175 static void
1176 restore_con_fun_0 (basic_block bb)
1178 CLEAR_HARD_REG_SET (BB_INFO (bb)->restore_in);
1181 /* The confluence function used by the DF equation solver to propagate
1182 restore info from predecessor to successor on edge E (pred->bb)
1183 according to the following equation:
1185 bb.restore_in = empty for entry block or one with empty_restore_in_p
1186 | ^(pred.restore_out - pred.restore_here) ^ bb.live_at_start
1188 If the edge is a loop enter we do not propagate the info because we
1189 don't want to put restores in more frequently executed places.
1192 static void
1193 restore_con_fun_n (edge e)
1195 int i;
1196 HARD_REG_SET temp_set;
1197 basic_block pred = e->src;
1198 basic_block bb = e->dest;
1199 struct bb_info *bb_info = BB_INFO (bb);
1201 if (bb_info->empty_restore_in_p)
1202 return;
1204 COPY_HARD_REG_SET (temp_set, BB_INFO (pred)->restore_out);
1205 AND_COMPL_HARD_REG_SET (temp_set, BB_INFO (pred)->restore_here);
1206 if (bb->loop_depth > pred->loop_depth)
1207 AND_COMPL_HARD_REG_SET (temp_set,
1208 LOOP_INFO (bb->loop_father)->mentioned_regs);
1209 AND_HARD_REG_SET (temp_set, bb_info->live_at_start);
1210 if (EDGE_PRED (bb, 0) == e)
1211 COPY_HARD_REG_SET (bb_info->restore_in, temp_set);
1212 else
1213 AND_HARD_REG_SET (bb_info->restore_in, temp_set);
1214 for (i = 0; i < FIRST_PSEUDO_REGISTER; i++)
1215 if (TEST_HARD_REG_BIT (temp_set, i))
1217 gcc_assert
1218 (bb_info->restore_in_mode[i] == VOIDmode
1219 || BB_INFO (pred)->restore_out_mode[i] == VOIDmode
1220 || ((bb_info->restore_in_mode[i]
1221 == BB_INFO (pred)->restore_out_mode[i])
1222 && (bb_info->restore_in_pseudo[i]
1223 == BB_INFO (pred)->restore_out_pseudo[i])));
1224 if (BB_INFO (pred)->restore_out_mode[i] != VOIDmode)
1226 bb_info->restore_in_mode[i]
1227 = BB_INFO (pred)->restore_out_mode[i];
1228 bb_info->restore_in_pseudo[i]
1229 = BB_INFO (pred)->restore_out_pseudo[i];
1234 /* Basic blocks for data flow problem -- all bocks except the special
1235 ones. */
1236 static bitmap all_blocks;
1238 /* The function calculates global restore information according
1239 to the following equations:
1241 bb.restore_in = empty for entry block or one with empty_restore_in_p
1242 | ^(pred.restore_out - pred.restore_here) ^ bb.live_at_start
1243 bb.restore_out = (bb.restore_in - bb.restore_kill) U bb.restore_gen.
1245 The function also calculates restore_in_mode and restore_out_mode.
1247 static void
1248 calculate_restore_in_out (void)
1250 df_simple_dataflow (DF_FORWARD, NULL, restore_con_fun_0, restore_con_fun_n,
1251 restore_trans_fun, all_blocks,
1252 df_get_postorder (DF_FORWARD),
1253 df_get_n_blocks (DF_FORWARD));
1256 /* The function calculates RESTORE_HERE according to the equation
1257 bb.restore_here = U ((bb.restore_out - succ.restore_in)
1258 ^ succ.live_at_start). */
1259 static int
1260 calculate_restore_here (void)
1262 basic_block bb;
1263 edge e;
1264 edge_iterator ei;
1265 HARD_REG_SET restore_here, temp_set;
1266 int changed_p = FALSE;
1268 FOR_EACH_BB (bb)
1270 CLEAR_HARD_REG_SET (restore_here);
1271 FOR_EACH_EDGE (e, ei, bb->succs)
1273 basic_block dest = e->dest;
1275 COPY_HARD_REG_SET (temp_set, BB_INFO (bb)->restore_out);
1276 AND_COMPL_HARD_REG_SET (temp_set, BB_INFO (dest)->restore_in);
1277 AND_HARD_REG_SET (temp_set, BB_INFO (dest)->live_at_start);
1278 IOR_HARD_REG_SET (restore_here, temp_set);
1281 if (! hard_reg_set_equal_p (restore_here, BB_INFO (bb)->restore_here))
1283 COPY_HARD_REG_SET (BB_INFO (bb)->restore_here, restore_here);
1284 changed_p = TRUE;
1287 return changed_p;
1290 /* The transfer function used by the DF equation solver to propagate save info
1291 through block with BB_INDEX according to the following
1292 equation:
1294 bb.save_in = ((bb.save_out - bb.save_kill) U bb.save_gen) - bb.restore_in.
1296 The function also propagates saving modes of the hard
1297 registers. */
1299 static bool
1300 save_trans_fun (int bb_index)
1302 int i;
1303 HARD_REG_SET temp_set;
1304 struct bb_info *bb_info = BB_INFO_BY_INDEX (bb_index);
1306 COPY_HARD_REG_SET (temp_set, bb_info->save_out);
1307 AND_COMPL_HARD_REG_SET (temp_set, bb_info->save_kill);
1308 for (i = 0; i < FIRST_PSEUDO_REGISTER; i++)
1309 if (TEST_HARD_REG_BIT (temp_set, i)
1310 && ! TEST_HARD_REG_BIT (bb_info->save_gen, i))
1312 gcc_assert (bb_info->save_in_mode[i] == VOIDmode
1313 || ((bb_info->save_in_mode[i]
1314 == bb_info->save_out_mode[i])
1315 && (bb_info->save_in_pseudo[i]
1316 == bb_info->save_out_pseudo[i])));
1317 bb_info->save_in_mode[i] = bb_info->save_out_mode[i];
1318 bb_info->save_in_pseudo[i] = bb_info->save_out_pseudo[i];
1320 IOR_HARD_REG_SET (temp_set, bb_info->save_gen);
1321 AND_COMPL_HARD_REG_SET (temp_set, bb_info->restore_in);
1322 if (! hard_reg_set_equal_p (temp_set, bb_info->save_in))
1324 COPY_HARD_REG_SET (bb_info->save_in, temp_set);
1325 return true;
1327 return false;
1330 /* The confluence function used by the DF equation solver to set up
1331 save info for a block BB without successor. */
1332 static void
1333 save_con_fun_0 (basic_block bb)
1335 CLEAR_HARD_REG_SET (BB_INFO (bb)->save_out);
1338 /* The confluence function used by the DF equation solver to propagate
1339 save info from successor to predecessor on edge E (bb->succ)
1340 according to the following equation:
1342 bb.save_out = empty for exit block or one with empty_save_out_p
1343 | ^(succ.save_in - succ.save_here) ^ bb.live_at_end
1345 If the edge is a loop exit we do not propagate the info because we
1346 don't want to put saves in more frequently executed places.
1348 static void
1349 save_con_fun_n (edge e)
1351 int i;
1352 HARD_REG_SET temp_set;
1353 basic_block succ = e->dest;
1354 basic_block bb = e->src;
1355 struct bb_info *bb_info = BB_INFO (bb);
1357 if (bb_info->empty_save_out_p)
1358 return;
1360 COPY_HARD_REG_SET (temp_set, BB_INFO (succ)->save_in);
1361 AND_COMPL_HARD_REG_SET (temp_set, BB_INFO (succ)->save_here);
1362 if (bb->loop_depth > succ->loop_depth)
1363 AND_COMPL_HARD_REG_SET (temp_set,
1364 LOOP_INFO (bb->loop_father)->mentioned_regs);
1365 AND_HARD_REG_SET (temp_set, bb_info->live_at_end);
1366 if (EDGE_SUCC (bb, 0) == e)
1367 COPY_HARD_REG_SET (bb_info->save_out, temp_set);
1368 else
1369 AND_HARD_REG_SET (bb_info->save_out, temp_set);
1370 for (i = 0; i < FIRST_PSEUDO_REGISTER; i++)
1371 if (TEST_HARD_REG_BIT (temp_set, i))
1373 gcc_assert
1374 (bb_info->save_out_mode[i] == VOIDmode
1375 || BB_INFO (succ)->save_in_mode[i] == VOIDmode
1376 || ((bb_info->save_out_mode[i]
1377 == BB_INFO (succ)->save_in_mode[i])
1378 && (bb_info->save_out_pseudo[i]
1379 == BB_INFO (succ)->save_in_pseudo[i])));
1380 if (BB_INFO (succ)->save_in_mode[i] != VOIDmode)
1382 bb_info->save_out_mode[i]
1383 = BB_INFO (succ)->save_in_mode[i];
1384 bb_info->save_out_pseudo[i]
1385 = BB_INFO (succ)->save_in_pseudo[i];
1390 /* The transfer function calculates global save information according
1391 to the following equations:
1393 bb.save_out = empty for exit block or one with empty_save_out_p
1394 | ^(succ.save_in - succ.save_here) ^ bb.live_at_end
1395 bb.save_in = ((bb.save_out - bb.save_kill) U bb.save_gen) - bb.restore_in.
1397 The function also calculates save_in_mode and save_out_mode.
1399 static void
1400 calculate_save_in_out (void)
1402 df_simple_dataflow (DF_BACKWARD, NULL, save_con_fun_0, save_con_fun_n,
1403 save_trans_fun, all_blocks,
1404 df_get_postorder (DF_BACKWARD),
1405 df_get_n_blocks (DF_BACKWARD));
1408 /* The function calculates SAVE_HERE according to the equation
1409 bb.save_here = U ((bb.save_in - pred.save_out) ^ pred.live_at_end). */
1410 static int
1411 calculate_save_here (void)
1413 basic_block bb;
1414 edge e;
1415 edge_iterator ei;
1416 HARD_REG_SET save_here, temp_set;
1417 int changed_p = FALSE;
1419 FOR_EACH_BB (bb)
1421 CLEAR_HARD_REG_SET (save_here);
1422 FOR_EACH_EDGE (e, ei, bb->preds)
1424 basic_block src = e->src;
1426 COPY_HARD_REG_SET (temp_set, BB_INFO (bb)->save_in);
1427 AND_COMPL_HARD_REG_SET (temp_set, BB_INFO (src)->save_out);
1428 AND_HARD_REG_SET (temp_set, BB_INFO (src)->live_at_end);
1429 IOR_HARD_REG_SET (save_here, temp_set);
1432 if (! hard_reg_set_equal_p (save_here, BB_INFO (bb)->save_here))
1434 COPY_HARD_REG_SET (BB_INFO (bb)->save_here, save_here);
1435 changed_p = TRUE;
1438 return changed_p;
1441 /* The transfer function used by the DF equation solver to propagate
1442 free info through block with BB_INDEX according to the following
1443 equation:
1445 bb.free_in = ((bb.free_out - bb.save_kill - bb.restore_kill) U bb.free_gen)
1446 - bb.save_here)
1449 static bool
1450 free_trans_fun (int bb_index)
1452 HARD_REG_SET temp_set;
1453 struct bb_info *bb_info = BB_INFO_BY_INDEX (bb_index);
1455 COPY_HARD_REG_SET (temp_set, bb_info->free_out);
1456 AND_COMPL_HARD_REG_SET (temp_set, bb_info->save_kill);
1457 AND_COMPL_HARD_REG_SET (temp_set, bb_info->restore_kill);
1458 IOR_HARD_REG_SET (temp_set, bb_info->free_gen);
1459 AND_COMPL_HARD_REG_SET (temp_set, bb_info->save_here);
1460 if (! hard_reg_set_equal_p (temp_set, bb_info->free_in))
1462 COPY_HARD_REG_SET (bb_info->free_in, temp_set);
1463 return true;
1465 return false;
1468 /* The confluence function used by the DF equation solver to set up
1469 free info for a block BB without successor. */
1470 static void
1471 free_con_fun_0 (basic_block bb)
1473 CLEAR_HARD_REG_SET (BB_INFO (bb)->free_out);
1476 /* The confluence function used by the DF equation solver to propagate
1477 free info from successor to predecessor on edge E (bb->succ)
1478 according to the following equation:
1480 bb.free_out = ^succ.free_in
1482 static void
1483 free_con_fun_n (edge e)
1485 HARD_REG_SET temp_set;
1486 basic_block succ = e->dest;
1487 basic_block bb = e->src;
1488 struct bb_info *bb_info = BB_INFO (bb);
1490 COPY_HARD_REG_SET (temp_set, BB_INFO (succ)->free_in);
1491 if (EDGE_SUCC (bb, 0) == e)
1492 COPY_HARD_REG_SET (bb_info->free_out, temp_set);
1493 else
1494 AND_HARD_REG_SET (bb_info->free_out, temp_set);
1497 /* The function calculates global free information according
1498 to the following equations:
1500 bb.free_out = ^succ.free_in
1501 bb.free_in = ((bb.free_out - bb.save_kill - bb.restore_kill) U bb.free_gen)
1502 - bb.save_here)
1504 The function also calculates free_in_mode and free_out_mode.
1506 static void
1507 calculate_free_in_out (void)
1509 basic_block bb;
1511 FOR_EACH_BB (bb)
1513 struct bb_info *bb_info = BB_INFO (bb);
1515 COPY_HARD_REG_SET (bb_info->free_in, bb_info->save_in);
1516 IOR_HARD_REG_SET (bb_info->free_in, bb_info->restore_in);
1517 COPY_HARD_REG_SET (bb_info->free_out, bb_info->save_out);
1518 IOR_HARD_REG_SET (bb_info->free_out, bb_info->restore_out);
1520 df_simple_dataflow (DF_BACKWARD, NULL, free_con_fun_0, free_con_fun_n,
1521 free_trans_fun, all_blocks,
1522 df_get_postorder (DF_BACKWARD),
1523 df_get_n_blocks (DF_BACKWARD));
1526 /* The function calculates the global save/restore information used to put
1527 save/restore code without generating new blocks. This is a
1528 bidirectional data flow problem (calculation of SAVE_IN and
1529 SAVE_OUT is a backward data flow problem and SAVE_HERE is forward
1530 one; calculation of RESTORE_IN and RESTORE_OUT is a forward data
1531 flow problem and RESTORE_HERE is backward one). It is complicated
1532 by necessity of calculation of modes for saving/restoring callee
1533 clobbered hard registers. */
1534 static void
1535 make_global_save_analysis (void)
1537 basic_block bb;
1538 int iter, changed_p;
1540 all_blocks = BITMAP_ALLOC (NULL);
1541 FOR_ALL_BB (bb)
1543 bitmap_set_bit (all_blocks, bb->index);
1545 calculate_local_save_info ();
1546 for (iter = 1;; iter++)
1548 calculate_restore_in_out ();
1549 changed_p = calculate_restore_here ();
1550 if (! changed_p)
1551 break;
1553 if (dump_file != NULL)
1554 fprintf (dump_file, " Number of global restore analysis iterations %d\n",
1555 iter);
1556 for (iter = 1;; iter++)
1558 calculate_save_in_out ();
1559 changed_p = calculate_save_here ();
1560 if (! changed_p)
1561 break;
1563 if (dump_file != NULL)
1564 fprintf (dump_file, " Number of global save analysis iterations %d\n",
1565 iter);
1566 calculate_free_in_out ();
1567 BITMAP_FREE (all_blocks);
1570 /* Print hard registers in SET to file F. The registers are printed
1571 with its mode given in MODES and corresponding pseudo given in
1572 PSEUDOS. */
1573 static void
1574 print_annotated_hard_reg_set (FILE *f, HARD_REG_SET set,
1575 unsigned char *modes, int *pseudos)
1577 int i;
1579 for (i = 0; i < FIRST_PSEUDO_REGISTER; i++)
1580 if (TEST_HARD_REG_BIT (set, i))
1582 fprintf (f, " %d", i);
1583 if (pseudos[i] >= 0)
1584 fprintf (f, "(%d)", pseudos[i]);
1585 fprintf (f, ":%s %s", GET_MODE_NAME (modes[i]), reg_names[i]);
1589 /* Print hard registers in SET to file F. */
1590 static void
1591 print_hard_reg_set (FILE *f, HARD_REG_SET set)
1593 int i;
1595 for (i = 0; i < FIRST_PSEUDO_REGISTER; i++)
1596 if (TEST_HARD_REG_BIT (set, i))
1597 fprintf (f, " %d %s", i, reg_names[i]);
1600 /* Print the save information for each block to file F. */
1601 static void
1602 print_save_data (FILE *f)
1604 basic_block bb;
1605 struct bb_info *bb_info;
1606 edge e;
1607 edge_iterator ei;
1609 FOR_EACH_BB (bb)
1611 bb_info = BB_INFO (bb);
1612 fprintf (f, "bb %d (preds", bb->index);
1613 FOR_EACH_EDGE (e, ei, bb->preds)
1615 fprintf (f, " %d", e->src->index);
1617 fprintf (f, ") (succs");
1618 FOR_EACH_EDGE (e, ei, bb->succs)
1620 fprintf (f, " %d", e->dest->index);
1622 fprintf (f, ")\n empty_save_out_p=%d", bb_info->empty_save_out_p);
1623 fprintf (f, ", empty_restore_in_p=%d", bb_info->empty_restore_in_p);
1624 fprintf (f, "\n save_in:");
1625 print_annotated_hard_reg_set
1626 (f, bb_info->save_in, bb_info->save_in_mode, bb_info->save_in_pseudo);
1627 fprintf (f, "\n save_out:");
1628 print_annotated_hard_reg_set
1629 (f, bb_info->save_out,
1630 bb_info->save_out_mode, bb_info->save_out_pseudo);
1631 fprintf (f, "\n restore_in:");
1632 print_annotated_hard_reg_set
1633 (f, bb_info->restore_in,
1634 bb_info->restore_in_mode, bb_info->restore_in_pseudo);
1635 fprintf (f, "\n restore_out:");
1636 print_annotated_hard_reg_set
1637 (f, bb_info->restore_out,
1638 bb_info->restore_out_mode, bb_info->restore_out_pseudo);
1639 fprintf (f, "\n free_in:");
1640 print_hard_reg_set (f, bb_info->free_in);
1641 fprintf (f, "\n free_out:");
1642 print_hard_reg_set (f, bb_info->free_out);
1643 fprintf (f, "\n live_at_start:");
1644 print_hard_reg_set (f, bb_info->live_at_start);
1645 fprintf (f, "\n live_at_end:");
1646 print_hard_reg_set (f, bb_info->live_at_end);
1647 fprintf (f, "\n save_here:");
1648 print_hard_reg_set (f, bb_info->save_here);
1649 fprintf (f, "\n restore_here:");
1650 print_hard_reg_set (f, bb_info->restore_here);
1651 fprintf (f, "\n save_kill:");
1652 print_hard_reg_set (f, bb_info->save_kill);
1653 fprintf (f, "\n restore_kill:");
1654 print_hard_reg_set (f, bb_info->restore_kill);
1655 fprintf (f, "\n save_gen:");
1656 print_hard_reg_set (f, bb_info->save_gen);
1657 fprintf (f, "\n restore_gen:");
1658 print_hard_reg_set (f, bb_info->restore_gen);
1659 fprintf (f, "\n free_gen:");
1660 print_hard_reg_set (f, bb_info->save_gen);
1661 fprintf (f, "\n\n");
1665 /* Print the save information for each block to stderr. */
1666 void
1667 debug_save_data (void)
1669 print_save_data (stderr);
1673 /* Setup hard registers in SET to save. Setup also their save modes
1674 in SAVE_MODE from FROM_SAVE_MODE and their pseudos in SAVE_PSEUDO
1675 from FROM_SAVE_PSEUDO. */
1676 static void
1677 set_hard_reg_saved (HARD_REG_SET set, unsigned char *from_saved_mode,
1678 enum machine_mode *save_mode, int *from_saved_pseudo,
1679 int *save_pseudo)
1681 int regno;
1683 n_regs_saved = 0;
1684 COPY_HARD_REG_SET (hard_regs_saved, set);
1685 for (regno = 0; regno < FIRST_PSEUDO_REGISTER; regno++)
1686 if (TEST_HARD_REG_BIT (set, regno))
1688 n_regs_saved++;
1689 save_mode[regno] = from_saved_mode[regno];
1690 save_pseudo[regno] = from_saved_pseudo[regno];
1692 else
1694 save_mode[regno] = VOIDmode;
1695 save_pseudo[regno] = -1;
1701 /* Find the places where hard regs are live across calls and save them. */
1703 void
1704 save_call_clobbered_regs (void)
1706 unsigned int regno;
1707 /* Computed in mark_set_regs, holds all registers set by the current
1708 instruction. */
1709 HARD_REG_SET this_insn_sets, hard_regs_to_save, saved;
1710 struct insn_chain *chain, *last, *next, *prev, *last_restore_chain, *where;
1711 struct bb_info *bb_info;
1712 enum machine_mode save_mode[FIRST_PSEUDO_REGISTER];
1713 loop_p loop;
1714 loop_iterator li;
1715 int save_pseudo[FIRST_PSEUDO_REGISTER];
1716 int free_pseudo[FIRST_PSEUDO_REGISTER];
1718 if (flag_ira && optimize)
1720 /* Do global analysis for better placement of spill code. */
1721 alloc_aux_for_blocks (sizeof (struct bb_info));
1722 FOR_EACH_LOOP (li, loop, 0)
1724 loop->aux = xmalloc (sizeof (struct loop_info));
1725 CLEAR_HARD_REG_SET (LOOP_INFO (loop)->mentioned_regs);
1727 memset (BB_INFO (ENTRY_BLOCK_PTR), 0, sizeof (struct bb_info));
1728 memset (BB_INFO (EXIT_BLOCK_PTR), 0, sizeof (struct bb_info));
1729 make_global_save_analysis ();
1732 CLEAR_HARD_REG_SET (hard_regs_saved);
1733 n_regs_saved = 0;
1735 if (flag_ira && reload_insn_chain != NULL)
1737 bb_info = BB_INFO_BY_INDEX (reload_insn_chain->block);
1738 set_hard_reg_saved (bb_info->restore_in,
1739 bb_info->restore_in_mode, save_mode,
1740 bb_info->restore_in_pseudo, save_pseudo);
1743 last = NULL;
1744 for (chain = reload_insn_chain; chain != 0; chain = next)
1746 rtx insn = chain->insn;
1747 enum rtx_code code = GET_CODE (insn);
1749 last = chain;
1750 next = chain->next;
1752 if (INSN_P (insn))
1754 if (n_regs_saved)
1756 if (!flag_ira && code == JUMP_INSN)
1757 /* Restore all registers if this is a JUMP_INSN. */
1758 COPY_HARD_REG_SET (referenced_regs, hard_regs_saved);
1759 else
1761 CLEAR_HARD_REG_SET (referenced_regs);
1762 CLEAR_HARD_REG_SET (modified_regs);
1763 mark_referenced_regs (PATTERN (insn), FALSE);
1764 AND_HARD_REG_SET (referenced_regs, hard_regs_saved);
1767 /* If some registers have been saved, see if INSN
1768 references any of them. We must restore them before
1769 the insn if so. */
1771 for (regno = 0; regno < FIRST_PSEUDO_REGISTER; regno++)
1772 if (TEST_HARD_REG_BIT (referenced_regs, regno))
1774 unsigned int before = regno;
1776 regno += insert_restore (chain, 1, regno, MOVE_MAX_WORDS,
1777 save_mode, save_pseudo);
1778 if (flag_ira && optimize)
1780 gcc_assert (before == regno);
1781 save_mode[before] = VOIDmode;
1782 save_pseudo[before] = -1;
1787 if (code == CALL_INSN
1788 && ! SIBLING_CALL_P (insn)
1789 && ! find_reg_note (insn, REG_NORETURN, NULL))
1791 HARD_REG_SET used_regs;
1792 reg_set_iterator rsi;
1794 /* Use the register life information in CHAIN to compute which
1795 regs are live during the call. */
1796 REG_SET_TO_HARD_REG_SET (hard_regs_to_save,
1797 &chain->live_throughout);
1798 /* Save hard registers always in the widest mode available. */
1799 for (regno = 0; regno < FIRST_PSEUDO_REGISTER; regno++)
1801 if (TEST_HARD_REG_BIT (hard_regs_to_save, regno))
1802 save_mode[regno] = regno_save_mode[regno][1];
1803 else
1804 save_mode[regno] = VOIDmode;
1805 save_pseudo[regno] = -1;
1808 /* Look through all live pseudos, mark their hard registers
1809 and choose proper mode for saving. */
1810 EXECUTE_IF_SET_IN_REG_SET
1811 (&chain->live_throughout, FIRST_PSEUDO_REGISTER, regno, rsi)
1813 int r = reg_renumber[regno];
1814 int nregs;
1815 enum machine_mode mode;
1817 /* Remember live_throughout can contain spilled
1818 registers when IRA is used. */
1819 if (flag_ira && optimize && r < 0)
1820 continue;
1821 gcc_assert (r >= 0);
1823 nregs = hard_regno_nregs[r][PSEUDO_REGNO_MODE (regno)];
1824 mode = HARD_REGNO_CALLER_SAVE_MODE
1825 (r, nregs, PSEUDO_REGNO_MODE (regno));
1826 if (GET_MODE_BITSIZE (mode)
1827 > GET_MODE_BITSIZE (save_mode[r]))
1828 save_mode[r] = mode;
1829 while (nregs-- > 0)
1831 SET_HARD_REG_BIT (hard_regs_to_save, r + nregs);
1832 save_pseudo[r + nregs] = regno;
1836 /* Record all registers set in this call insn. These don't need
1837 to be saved. N.B. the call insn might set a subreg of a
1838 multi-hard-reg pseudo; then the pseudo is considered live
1839 during the call, but the subreg that is set isn't. */
1840 CLEAR_HARD_REG_SET (this_insn_sets);
1841 note_stores (PATTERN (insn), mark_set_regs, &this_insn_sets);
1843 /* Compute which hard regs must be saved before this call. */
1844 AND_COMPL_HARD_REG_SET (hard_regs_to_save, call_fixed_reg_set);
1845 AND_COMPL_HARD_REG_SET (hard_regs_to_save, this_insn_sets);
1846 AND_COMPL_HARD_REG_SET (hard_regs_to_save, hard_regs_saved);
1847 COPY_HARD_REG_SET (used_regs, call_used_reg_set);
1848 AND_HARD_REG_SET (hard_regs_to_save, used_regs);
1850 if (flag_ira)
1851 IOR_HARD_REG_SET (hard_regs_saved, hard_regs_to_save);
1852 else
1854 for (regno = 0; regno < FIRST_PSEUDO_REGISTER; regno++)
1855 if (TEST_HARD_REG_BIT (hard_regs_to_save, regno))
1856 regno += insert_save (chain, 1, regno,
1857 &hard_regs_to_save, save_mode,
1858 save_pseudo);
1862 /* Must recompute n_regs_saved. */
1863 n_regs_saved = 0;
1864 for (regno = 0; regno < FIRST_PSEUDO_REGISTER; regno++)
1865 if (TEST_HARD_REG_BIT (hard_regs_saved, regno))
1866 n_regs_saved++;
1870 if (chain->next == 0 || chain->next->block != chain->block)
1872 struct bb_info *next_bb_info;
1874 next_bb_info = (chain->next != NULL
1875 ? BB_INFO_BY_INDEX (chain->next->block) : NULL);
1877 /* At the end of the basic block, we must restore any registers that
1878 remain saved. If the last insn in the block is a JUMP_INSN, put
1879 the restore before the insn, otherwise, put it after the insn. */
1881 if (flag_ira && optimize)
1882 set_hard_reg_saved
1883 (BB_INFO_BY_INDEX (chain->block)->restore_here,
1884 BB_INFO_BY_INDEX (chain->block)->restore_out_mode, save_mode,
1885 BB_INFO_BY_INDEX (chain->block)->restore_out_pseudo,
1886 save_pseudo);
1888 if (n_regs_saved)
1889 for (regno = 0; regno < FIRST_PSEUDO_REGISTER; regno++)
1890 if (TEST_HARD_REG_BIT (hard_regs_saved, regno))
1892 unsigned int before = regno;
1894 regno += insert_restore (chain, JUMP_P (insn),
1895 regno, MOVE_MAX_WORDS, save_mode,
1896 save_pseudo);
1897 if (flag_ira && optimize)
1899 gcc_assert (before == regno);
1900 save_mode[before] = VOIDmode;
1901 save_pseudo[before] = -1;
1905 if (flag_ira && optimize && next_bb_info != NULL)
1906 set_hard_reg_saved (next_bb_info->restore_in,
1907 next_bb_info->restore_in_mode, save_mode,
1908 next_bb_info->restore_in_pseudo, save_pseudo);
1913 if (!flag_ira)
1914 return;
1916 CLEAR_HARD_REG_SET (hard_regs_to_save);
1917 n_regs_saved = 0;
1918 last_restore_chain = NULL;
1920 if (last == NULL)
1921 CLEAR_HARD_REG_SET (saved);
1922 else
1924 bb_info = BB_INFO_BY_INDEX (last->block);
1925 set_hard_reg_saved (bb_info->save_out,
1926 bb_info->save_out_mode, save_mode,
1927 bb_info->save_out_pseudo, save_pseudo);
1928 COPY_HARD_REG_SET (hard_regs_to_save, hard_regs_saved);
1929 COPY_HARD_REG_SET (saved, bb_info->free_out);
1930 for (regno = 0; regno < FIRST_PSEUDO_REGISTER; regno++)
1931 if (!TEST_HARD_REG_BIT (saved, regno))
1932 free_pseudo[regno] = -1;
1933 else
1935 if (TEST_HARD_REG_BIT (bb_info->save_out, regno))
1936 free_pseudo [regno] = bb_info->save_out_pseudo [regno];
1937 else
1939 gcc_assert (TEST_HARD_REG_BIT (bb_info->restore_out, regno));
1940 free_pseudo [regno] = bb_info->restore_out_pseudo [regno];
1944 for (chain = last; chain != 0; chain = prev)
1946 rtx insn = chain->insn;
1947 enum rtx_code code = GET_CODE (insn);
1949 prev = chain->prev;
1951 if (INSN_P (insn))
1953 bb_info = BB_INFO_BY_INDEX (chain->block);
1955 CLEAR_HARD_REG_SET (referenced_regs);
1956 CLEAR_HARD_REG_SET (modified_regs);
1957 mark_referenced_regs (PATTERN (insn), FALSE);
1958 AND_HARD_REG_SET (modified_regs, hard_regs_to_save);
1959 AND_COMPL_HARD_REG_SET (saved, referenced_regs);
1960 AND_COMPL_HARD_REG_SET (saved, modified_regs);
1962 if (chain->is_caller_save_insn)
1964 if (last_restore_chain == NULL)
1965 last_restore_chain = chain;
1967 else
1969 /* If some registers have been saved, see if INSN
1970 references any of them. We must restore them before
1971 the insn if so. */
1973 for (regno = 0; regno < FIRST_PSEUDO_REGISTER; regno++)
1974 if (TEST_HARD_REG_BIT (modified_regs, regno))
1976 unsigned int before = regno;
1978 /* We should put save insns before restore insns
1979 between the two calls because the same stack
1980 slot for different hard registers can be used
1981 for restoring in the first call and saving for
1982 the second call. */
1983 regno += insert_save (last_restore_chain != NULL
1984 ? last_restore_chain : chain,
1985 last_restore_chain == NULL
1986 && JUMP_P (chain->insn),
1987 regno, &hard_regs_to_save,
1988 save_mode, save_pseudo);
1989 gcc_assert (before == regno);
1991 CLEAR_HARD_REG_BIT (saved, regno);
1992 CLEAR_HARD_REG_BIT (hard_regs_to_save, regno);
1993 save_mode[before] = VOIDmode;
1994 save_pseudo[before] = -1;
1995 free_pseudo[before] = -1;
1999 for (regno = 0; regno < FIRST_PSEUDO_REGISTER; regno++)
2000 if (TEST_HARD_REG_BIT (saved, regno) && free_pseudo[regno] >= 0)
2001 SET_REGNO_REG_SET (&chain->saved, free_pseudo[regno]);
2002 if (chain->is_caller_save_insn && chain->saved_pseudo_regno >= 0)
2004 int i;
2005 enum machine_mode mode;
2007 mode = GET_MODE (regno_reg_rtx[chain->saved_pseudo_regno]);
2008 regno = reg_renumber[chain->saved_pseudo_regno];
2009 add_to_hard_reg_set (&saved, mode, regno);
2010 for (i = hard_regno_nregs[regno][mode] - 1; i >= 0; i--)
2011 free_pseudo[regno + i] = chain->saved_pseudo_regno;
2013 if (code == CALL_INSN
2014 && ! SIBLING_CALL_P (insn)
2015 && ! find_reg_note (insn, REG_NORETURN, NULL))
2017 HARD_REG_SET used_regs;
2018 reg_set_iterator rsi;
2020 last_restore_chain = NULL;
2022 /* Use the register life information in CHAIN to
2023 compute which regs are live during the call. */
2024 REG_SET_TO_HARD_REG_SET (hard_regs_to_save,
2025 &chain->live_throughout);
2026 /* Save hard registers always in the widest mode
2027 available. */
2028 for (regno = 0; regno < FIRST_PSEUDO_REGISTER; regno++)
2030 if (TEST_HARD_REG_BIT (hard_regs_to_save, regno))
2032 CLEAR_HARD_REG_SET (this_insn_sets);
2033 note_stores (PATTERN (insn), mark_set_regs,
2034 &this_insn_sets);
2035 save_mode[regno] = regno_save_mode[regno][1];
2037 else
2038 save_mode[regno] = VOIDmode;
2039 save_pseudo[regno] = -1;
2042 /* Look through all live pseudos, mark their hard
2043 registers and choose proper mode for saving. */
2044 EXECUTE_IF_SET_IN_REG_SET
2045 (&chain->live_throughout, FIRST_PSEUDO_REGISTER, regno, rsi)
2047 int r = reg_renumber[regno];
2048 int nregs;
2049 enum machine_mode mode;
2051 /* Remember live_throughout can contain spilled
2052 registers when IRA is used. */
2053 if (flag_ira && r < 0)
2054 continue;
2055 gcc_assert (r >= 0);
2057 nregs = hard_regno_nregs[r][PSEUDO_REGNO_MODE (regno)];
2058 mode = HARD_REGNO_CALLER_SAVE_MODE
2059 (r, nregs, PSEUDO_REGNO_MODE (regno));
2060 if (GET_MODE_BITSIZE (mode)
2061 > GET_MODE_BITSIZE (save_mode[r]))
2062 save_mode[r] = mode;
2063 while (nregs-- > 0)
2065 SET_HARD_REG_BIT (hard_regs_to_save, r + nregs);
2066 save_pseudo[r + nregs] = regno;
2067 free_pseudo[r + nregs] = regno;
2071 /* Record all registers set in this call insn. These
2072 don't need to be saved. N.B. the call insn might set
2073 a subreg of a multi-hard-reg pseudo; then the pseudo
2074 is considered live during the call, but the subreg
2075 that is set isn't. */
2076 CLEAR_HARD_REG_SET (this_insn_sets);
2077 note_stores (PATTERN (insn), mark_set_regs, &this_insn_sets);
2079 /* Compute which hard regs must be saved before this call. */
2080 AND_COMPL_HARD_REG_SET (hard_regs_to_save, call_fixed_reg_set);
2081 AND_COMPL_HARD_REG_SET (hard_regs_to_save, this_insn_sets);
2082 COPY_HARD_REG_SET (used_regs, call_used_reg_set);
2083 AND_HARD_REG_SET (hard_regs_to_save, used_regs);
2084 COPY_HARD_REG_SET (saved, hard_regs_to_save);
2085 for (regno = 0; regno < FIRST_PSEUDO_REGISTER; regno++)
2086 if (!TEST_HARD_REG_BIT (saved, regno))
2088 free_pseudo[regno] = -1;
2089 save_pseudo[regno] = -1;
2090 save_mode[regno] = VOIDmode;
2095 if (chain->prev == 0 || chain->prev->block != chain->block)
2097 struct bb_info *prev_bb_info;
2099 prev_bb_info = (chain->prev != NULL
2100 ? BB_INFO_BY_INDEX (chain->prev->block) : NULL);
2102 /* At the start of the basic block, we must save any
2103 registers from save_here. */
2105 set_hard_reg_saved
2106 (BB_INFO_BY_INDEX (chain->block)->save_here,
2107 BB_INFO_BY_INDEX (chain->block)->save_in_mode, save_mode,
2108 BB_INFO_BY_INDEX (chain->block)->save_in_pseudo, save_pseudo);
2109 COPY_HARD_REG_SET (hard_regs_to_save, hard_regs_saved);
2111 where = (last_restore_chain != NULL
2112 ? last_restore_chain->next : chain);
2113 /* An addr_vec is placed outside any basic block but its
2114 chain has the same block as the block of subsequent insn.
2115 So skip to the real start of the basic block. */
2116 if (GET_CODE (where->insn) == CODE_LABEL && where->next != NULL
2117 && JUMP_P (where->next->insn)
2118 && (GET_CODE (PATTERN (where->next->insn)) == ADDR_DIFF_VEC
2119 || GET_CODE (PATTERN (where->next->insn)) == ADDR_VEC)
2120 && where->next->next != NULL)
2121 where = where->next->next;
2123 if (!hard_reg_set_empty_p (hard_regs_to_save))
2124 for (regno = 0; regno < FIRST_PSEUDO_REGISTER; regno++)
2125 if (TEST_HARD_REG_BIT (hard_regs_to_save, regno))
2127 unsigned int before = regno;
2129 regno += insert_save (where, INSN_P (where->insn), regno,
2130 &hard_regs_to_save, save_mode,
2131 save_pseudo);
2132 gcc_assert (before == regno);
2133 save_mode[before] = VOIDmode;
2134 save_pseudo[before] = -1;
2137 if (prev_bb_info != NULL)
2139 last_restore_chain = NULL;
2140 set_hard_reg_saved (prev_bb_info->save_out,
2141 prev_bb_info->save_out_mode, save_mode,
2142 prev_bb_info->save_out_pseudo, save_pseudo);
2143 COPY_HARD_REG_SET (hard_regs_to_save, hard_regs_saved);
2144 COPY_HARD_REG_SET (saved, prev_bb_info->free_out);
2145 for (regno = 0; regno < FIRST_PSEUDO_REGISTER; regno++)
2146 if (!TEST_HARD_REG_BIT (saved, regno))
2147 free_pseudo[regno] = -1;
2148 else
2150 if (TEST_HARD_REG_BIT (prev_bb_info->save_out, regno))
2151 free_pseudo [regno]
2152 = prev_bb_info->save_out_pseudo [regno];
2153 else
2155 gcc_assert (TEST_HARD_REG_BIT (prev_bb_info->restore_out, regno));
2156 free_pseudo [regno]
2157 = prev_bb_info->restore_out_pseudo [regno];
2164 FOR_EACH_LOOP (li, loop, 0)
2166 free (loop->aux);
2168 if (flag_ira && optimize)
2169 free_aux_for_blocks ();
2172 /* Here from note_stores, or directly from save_call_clobbered_regs,
2173 when an insn stores a value in a register. Set the proper bit or
2174 bits in this_insn_sets. */
2175 static void
2176 mark_set_regs (rtx reg, const_rtx setter ATTRIBUTE_UNUSED, void *data)
2178 int regno, endregno, i;
2179 HARD_REG_SET *this_insn_sets = data;
2181 if (GET_CODE (reg) == SUBREG)
2183 rtx inner = SUBREG_REG (reg);
2184 if (!REG_P (inner))
2185 return;
2186 if ((regno = REGNO (inner)) >= FIRST_PSEUDO_REGISTER)
2188 if (reg_renumber[regno] < 0)
2189 return;
2190 regno = reg_renumber[regno];
2191 endregno
2192 = hard_regno_nregs[regno][PSEUDO_REGNO_MODE (REGNO (inner))];
2194 else
2196 regno = subreg_regno (reg);
2197 endregno = regno + subreg_nregs (reg);
2200 else if (REG_P (reg))
2202 if ((regno = REGNO (reg)) < FIRST_PSEUDO_REGISTER)
2203 endregno = END_HARD_REGNO (reg);
2204 else
2206 if (reg_renumber[regno] < 0)
2207 return;
2208 regno = reg_renumber[regno];
2209 endregno
2210 = hard_regno_nregs[regno][PSEUDO_REGNO_MODE (REGNO (reg))];
2213 else
2214 return;
2216 for (i = regno; i < endregno; i++)
2217 SET_HARD_REG_BIT (*this_insn_sets, i);
2220 /* Here from note_stores when an insn stores a value in a register.
2221 Set the proper bit or bits in the passed regset. All pseudos that have
2222 been assigned hard regs have had their register number changed already,
2223 so we can ignore pseudos. */
2224 static void
2225 add_stored_regs (rtx reg, const_rtx setter, void *data)
2227 int regno, endregno, i;
2228 enum machine_mode mode = GET_MODE (reg);
2229 int offset = 0;
2231 if (GET_CODE (setter) == CLOBBER)
2232 return;
2234 if (GET_CODE (reg) == SUBREG
2235 && REG_P (SUBREG_REG (reg))
2236 && REGNO (SUBREG_REG (reg)) < FIRST_PSEUDO_REGISTER)
2238 offset = subreg_regno_offset (REGNO (SUBREG_REG (reg)),
2239 GET_MODE (SUBREG_REG (reg)),
2240 SUBREG_BYTE (reg),
2241 GET_MODE (reg));
2242 regno = REGNO (SUBREG_REG (reg)) + offset;
2243 endregno = regno + subreg_nregs (reg);
2245 else
2247 if (!REG_P (reg) || REGNO (reg) >= FIRST_PSEUDO_REGISTER)
2248 return;
2250 regno = REGNO (reg) + offset;
2251 endregno = end_hard_regno (mode, regno);
2254 for (i = regno; i < endregno; i++)
2255 SET_REGNO_REG_SET ((regset) data, i);
2258 /* Walk X and record all referenced registers in REFERENCED_REG and
2259 modified registers in MODIFIED_REGS. */
2260 static void
2261 mark_referenced_regs (rtx x, int set_p)
2263 enum rtx_code code;
2264 const char *fmt;
2265 int i, j;
2266 int stop_p;
2268 for (stop_p = FALSE; !stop_p; )
2270 while (GET_CODE (x) == STRICT_LOW_PART || GET_CODE (x) == ZERO_EXTRACT)
2271 x = XEXP (x, 0);
2272 code = GET_CODE (x);
2273 switch (code)
2275 case SET:
2276 gcc_assert (!set_p);
2277 mark_referenced_regs (SET_DEST (x), TRUE);
2278 x = SET_SRC (x);
2279 break;
2280 case CLOBBER:
2281 mark_referenced_regs (SET_DEST (x), TRUE);
2282 return;
2283 case PRE_INC:
2284 case POST_INC:
2285 case PRE_DEC:
2286 case POST_DEC:
2287 gcc_assert (!set_p);
2288 x = XEXP (x, 0);
2289 mark_referenced_regs (x, TRUE);
2290 break;
2291 case PRE_MODIFY:
2292 case POST_MODIFY:
2293 set_p = FALSE;
2294 mark_referenced_regs (XEXP (x, 0), FALSE);
2295 mark_referenced_regs (XEXP (x, 0), TRUE);
2296 x = XEXP (x, 1);
2297 mark_referenced_regs (x, FALSE);
2298 break;
2299 case SUBREG:
2300 x = SUBREG_REG (x);
2301 break;
2302 case REG:
2304 int regno = REGNO (x);
2305 int hardregno = (regno < FIRST_PSEUDO_REGISTER ? regno
2306 : reg_renumber[regno]);
2308 if (hardregno >= 0)
2310 if (set_p)
2311 add_to_hard_reg_set (&modified_regs, GET_MODE (x), hardregno);
2312 add_to_hard_reg_set (&referenced_regs, GET_MODE (x), hardregno);
2314 /* If this is a pseudo that did not get a hard register, scan
2315 its memory location, since it might involve the use of
2316 another register, which might be saved. */
2317 else if (reg_equiv_mem[regno] != 0)
2318 mark_referenced_regs (XEXP (reg_equiv_mem[regno], 0), set_p);
2319 else if (reg_equiv_address[regno] != 0)
2320 mark_referenced_regs (reg_equiv_address[regno], set_p);
2321 return;
2323 default:
2324 fmt = GET_RTX_FORMAT (code);
2325 for (i = GET_RTX_LENGTH (code) - 1; i >= 0; i--)
2327 if (fmt[i] == 'e')
2328 mark_referenced_regs (XEXP (x, i), FALSE);
2329 else if (fmt[i] == 'E')
2330 for (j = XVECLEN (x, i) - 1; j >= 0; j--)
2331 mark_referenced_regs (XVECEXP (x, i, j), FALSE);
2333 stop_p = TRUE;
2334 break;
2339 /* Insert a sequence of insns to restore. Place these insns in front
2340 of CHAIN if BEFORE_P is nonzero, behind the insn otherwise.
2341 MAXRESTORE is the maximum number of registers which should be
2342 restored during this call. It should never be less than 1 since we
2343 only work with entire registers. SAVE_PSEUDO maps hard registers
2344 into the corresponding pseudos.
2346 Note that we have verified in init_caller_save that we can do this
2347 with a simple SET, so use it. Set INSN_CODE to what we save there
2348 since the address might not be valid so the insn might not be recognized.
2349 These insns will be reloaded and have register elimination done by
2350 find_reload, so we need not worry about that here.
2352 Return the extra number of registers saved. */
2354 static int
2355 insert_restore (struct insn_chain *chain, int before_p, int regno,
2356 int maxrestore, enum machine_mode *save_mode,
2357 int *save_pseudo)
2359 int i, k;
2360 rtx pat = NULL_RTX;
2361 int code;
2362 unsigned int numregs = 0;
2363 struct insn_chain *new;
2364 rtx mem;
2366 /* A common failure mode if register status is not correct in the
2367 RTL is for this routine to be called with a REGNO we didn't
2368 expect to save. That will cause us to write an insn with a (nil)
2369 SET_DEST or SET_SRC. Instead of doing so and causing a crash
2370 later, check for this common case here instead. This will remove
2371 one step in debugging such problems. */
2372 gcc_assert (regno_save_mem[regno][1]);
2374 /* Get the pattern to emit and update our status.
2376 See if we can restore `maxrestore' registers at once. Work
2377 backwards to the single register case. */
2378 for (i = maxrestore; i > 0; i--)
2380 int j;
2381 int ok = 1;
2383 if (regno_save_mem[regno][i] == 0)
2384 continue;
2386 for (j = 0; j < i; j++)
2387 if (! TEST_HARD_REG_BIT (hard_regs_saved, regno + j))
2389 ok = 0;
2390 break;
2392 /* Must do this one restore at a time. */
2393 if (! ok)
2394 continue;
2396 numregs = i;
2397 break;
2400 mem = regno_save_mem[regno][numregs];
2401 if (save_mode[regno] != VOIDmode
2402 && save_mode[regno] != GET_MODE (mem)
2403 && numregs == (unsigned int) hard_regno_nregs[regno][save_mode[regno]]
2404 && reg_save_code (regno, save_mode[regno]) >= 0)
2405 mem = adjust_address_nv (mem, save_mode[regno], 0);
2406 else
2407 mem = copy_rtx (mem);
2409 /* Verify that the alignment of spill space is equal to or greater
2410 than required. */
2411 gcc_assert (MIN (PREFERRED_STACK_BOUNDARY,
2412 GET_MODE_ALIGNMENT (GET_MODE (mem))) <= MEM_ALIGN (mem));
2414 pat = gen_rtx_SET (VOIDmode,
2415 gen_rtx_REG (GET_MODE (mem),
2416 regno), mem);
2417 code = reg_restore_code (regno, GET_MODE (mem));
2418 new = insert_one_insn (chain, before_p, code, pat);
2419 new->saved_pseudo_regno = save_pseudo[regno];
2421 if (dump_file != NULL)
2422 fprintf (dump_file, "inserting restore insn %u for pseudo %d %s %u\n",
2423 INSN_UID (new->insn), save_pseudo[regno],
2424 before_p ? "before" : "after", INSN_UID (chain->insn));
2426 /* Clear status for all registers we restored. */
2427 for (k = 0; k < i; k++)
2429 CLEAR_HARD_REG_BIT (hard_regs_saved, regno + k);
2430 SET_REGNO_REG_SET (&new->dead_or_set, regno + k);
2431 n_regs_saved--;
2434 /* Tell our callers how many extra registers we saved/restored. */
2435 return numregs - 1;
2438 /* Like insert_restore above, but save registers instead. */
2440 static int
2441 insert_save (struct insn_chain *chain, int before_p, int regno,
2442 HARD_REG_SET (*to_save), enum machine_mode *save_mode,
2443 int *save_pseudo)
2445 int i;
2446 unsigned int k;
2447 rtx pat = NULL_RTX;
2448 int code;
2449 unsigned int numregs = 0;
2450 struct insn_chain *new;
2451 rtx mem;
2453 /* A common failure mode if register status is not correct in the
2454 RTL is for this routine to be called with a REGNO we didn't
2455 expect to save. That will cause us to write an insn with a (nil)
2456 SET_DEST or SET_SRC. Instead of doing so and causing a crash
2457 later, check for this common case here. This will remove one
2458 step in debugging such problems. */
2459 gcc_assert (regno_save_mem[regno][1]);
2461 /* Get the pattern to emit and update our status.
2463 See if we can save several registers with a single instruction.
2464 Work backwards to the single register case. */
2465 for (i = MOVE_MAX_WORDS; i > 0; i--)
2467 int j;
2468 int ok = 1;
2469 if (regno_save_mem[regno][i] == 0)
2470 continue;
2472 for (j = 0; j < i; j++)
2473 if (! TEST_HARD_REG_BIT (*to_save, regno + j))
2475 ok = 0;
2476 break;
2478 /* Must do this one save at a time. */
2479 if (! ok)
2480 continue;
2482 numregs = i;
2483 break;
2486 mem = regno_save_mem[regno][numregs];
2487 if (save_mode[regno] != VOIDmode
2488 && save_mode[regno] != GET_MODE (mem)
2489 && numregs == (unsigned int) hard_regno_nregs[regno][save_mode[regno]]
2490 && reg_save_code (regno, save_mode[regno]) >= 0)
2491 mem = adjust_address_nv (mem, save_mode[regno], 0);
2492 else
2493 mem = copy_rtx (mem);
2495 /* Verify that the alignment of spill space is equal to or greater
2496 than required. */
2497 gcc_assert (MIN (PREFERRED_STACK_BOUNDARY,
2498 GET_MODE_ALIGNMENT (GET_MODE (mem))) <= MEM_ALIGN (mem));
2500 pat = gen_rtx_SET (VOIDmode, mem,
2501 gen_rtx_REG (GET_MODE (mem),
2502 regno));
2503 code = reg_save_code (regno, GET_MODE (mem));
2504 new = insert_one_insn (chain, before_p, code, pat);
2505 new->saved_pseudo_regno = save_pseudo[regno];
2507 if (dump_file != NULL)
2508 fprintf (dump_file, "inserting save insn %u for pseudo %d %s %u\n",
2509 INSN_UID (new->insn), save_pseudo[regno],
2510 before_p ? "before" : "after", INSN_UID (chain->insn));
2512 /* Set hard_regs_saved and dead_or_set for all the registers we saved. */
2513 for (k = 0; k < numregs; k++)
2515 SET_HARD_REG_BIT (hard_regs_saved, regno + k);
2516 SET_REGNO_REG_SET (&new->dead_or_set, regno + k);
2517 n_regs_saved++;
2520 /* Tell our callers how many extra registers we saved/restored. */
2521 return numregs - 1;
2524 /* Emit a new caller-save insn and set the code. */
2525 static struct insn_chain *
2526 insert_one_insn (struct insn_chain *chain, int before_p, int code, rtx pat)
2528 rtx insn = chain->insn;
2529 struct insn_chain *new;
2531 #ifdef HAVE_cc0
2532 /* If INSN references CC0, put our insns in front of the insn that sets
2533 CC0. This is always safe, since the only way we could be passed an
2534 insn that references CC0 is for a restore, and doing a restore earlier
2535 isn't a problem. We do, however, assume here that CALL_INSNs don't
2536 reference CC0. Guard against non-INSN's like CODE_LABEL. */
2538 if ((NONJUMP_INSN_P (insn) || JUMP_P (insn))
2539 && before_p
2540 && reg_referenced_p (cc0_rtx, PATTERN (insn)))
2541 chain = chain->prev, insn = chain->insn;
2542 #endif
2544 new = new_insn_chain ();
2545 if (before_p)
2547 rtx link;
2549 new->prev = chain->prev;
2550 if (new->prev != 0)
2551 new->prev->next = new;
2552 else
2553 reload_insn_chain = new;
2555 chain->prev = new;
2556 new->next = chain;
2557 new->insn = emit_insn_before (pat, insn);
2558 /* ??? It would be nice if we could exclude the already / still saved
2559 registers from the live sets. */
2560 COPY_REG_SET (&new->live_throughout, &chain->live_throughout);
2561 /* Registers that die in CHAIN->INSN still live in the new insn. */
2562 for (link = REG_NOTES (chain->insn); link; link = XEXP (link, 1))
2564 if (REG_NOTE_KIND (link) == REG_DEAD)
2566 rtx reg = XEXP (link, 0);
2567 int regno, i;
2569 gcc_assert (REG_P (reg));
2570 regno = REGNO (reg);
2571 if (regno >= FIRST_PSEUDO_REGISTER)
2572 regno = reg_renumber[regno];
2573 if (regno < 0)
2574 continue;
2575 for (i = hard_regno_nregs[regno][GET_MODE (reg)] - 1;
2576 i >= 0; i--)
2577 SET_REGNO_REG_SET (&new->live_throughout, regno + i);
2581 /* If CHAIN->INSN is a call, then the registers which contain
2582 the arguments to the function are live in the new insn. */
2583 if (CALL_P (chain->insn))
2585 for (link = CALL_INSN_FUNCTION_USAGE (chain->insn);
2586 link != NULL_RTX;
2587 link = XEXP (link, 1))
2589 rtx arg = XEXP (link, 0);
2591 if (GET_CODE (arg) == USE)
2593 rtx reg = XEXP (arg, 0);
2595 if (REG_P (reg))
2597 int i, regno = REGNO (reg);
2599 /* Registers in CALL_INSN_FUNCTION_USAGE are always
2600 hard registers. */
2601 gcc_assert (regno < FIRST_PSEUDO_REGISTER);
2603 for (i = hard_regno_nregs[regno][GET_MODE (reg)] - 1;
2604 i >= 0; i--)
2605 SET_REGNO_REG_SET (&new->live_throughout, regno + i);
2612 CLEAR_REG_SET (&new->dead_or_set);
2613 if (chain->insn == BB_HEAD (BASIC_BLOCK (chain->block)))
2614 BB_HEAD (BASIC_BLOCK (chain->block)) = new->insn;
2616 else
2618 new->next = chain->next;
2619 if (new->next != 0)
2620 new->next->prev = new;
2621 chain->next = new;
2622 new->prev = chain;
2623 if (GET_CODE (insn) != CODE_LABEL)
2624 new->insn = emit_insn_after (pat, insn);
2625 else
2627 /* Put the insn after bb note in a empty basic block. */
2628 gcc_assert (NEXT_INSN (insn) && NOTE_P (NEXT_INSN (insn)));
2629 new->insn = emit_insn_after (pat, NEXT_INSN (insn));
2631 /* ??? It would be nice if we could exclude the already / still saved
2632 registers from the live sets, and observe REG_UNUSED notes. */
2633 COPY_REG_SET (&new->live_throughout, &chain->live_throughout);
2634 /* Registers that are set in CHAIN->INSN live in the new insn.
2635 (Unless there is a REG_UNUSED note for them, but we don't
2636 look for them here.) */
2637 if (INSN_P (chain->insn))
2638 note_stores (PATTERN (chain->insn), add_stored_regs,
2639 &new->live_throughout);
2640 CLEAR_REG_SET (&new->dead_or_set);
2641 if (chain->insn == BB_END (BASIC_BLOCK (chain->block)))
2642 BB_END (BASIC_BLOCK (chain->block)) = new->insn;
2644 new->block = chain->block;
2645 new->is_caller_save_insn = 1;
2647 INSN_CODE (new->insn) = code;
2648 return new;
2650 #include "gt-caller-save.h"