1 /* Save and restore call-clobbered registers which are live across a call.
2 Copyright (C) 1989, 1992 Free Software Foundation, Inc.
4 This file is part of GNU CC.
6 GNU CC is free software; you can redistribute it and/or modify
7 it under the terms of the GNU General Public License as published by
8 the Free Software Foundation; either version 2, or (at your option)
11 GNU CC is distributed in the hope that it will be useful,
12 but WITHOUT ANY WARRANTY; without even the implied warranty of
13 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 GNU General Public License for more details.
16 You should have received a copy of the GNU General Public License
17 along with GNU CC; see the file COPYING. If not, write to
18 the Free Software Foundation, 675 Mass Ave, Cambridge, MA 02139, USA. */
22 #include "insn-config.h"
25 #include "hard-reg-set.h"
27 #include "basic-block.h"
31 /* A mode for each hard register that we can save. This mode is wide enough
32 to save the entire contents of the register and will be used whenever the
33 register must be saved because it is live. */
35 static enum machine_mode regno_save_mode
[FIRST_PSEUDO_REGISTER
];
37 /* For each hard register, a place on the stack where it can be saved,
40 static rtx regno_save_mem
[FIRST_PSEUDO_REGISTER
];
42 /* We will only make a register eligible for caller-save if it can be
43 saved in its widest mode with a simple SET insn as long as the memory
44 address is valid. We record the INSN_CODE is those insns here since
45 when we emit them, the addresses might not be valid, so they might not
48 static enum insn_code reg_save_code
[FIRST_PSEUDO_REGISTER
];
49 static enum insn_code reg_restore_code
[FIRST_PSEUDO_REGISTER
];
51 /* Set of hard regs currently live (during scan of all insns). */
53 static HARD_REG_SET hard_regs_live
;
55 /* Set of hard regs currently residing in save area (during insn scan). */
57 static HARD_REG_SET hard_regs_saved
;
59 /* Number of registers currently in hard_regs_saved. */
63 static void set_reg_live ();
64 static void clear_reg_live ();
65 static void restore_referenced_regs ();
66 static void insert_save_restore ();
68 /* Return a machine mode that is legitimate for hard reg REGNO and large
69 enough to save the whole register. If we can't find one,
72 static enum machine_mode
73 choose_hard_reg_mode (regno
)
76 enum machine_mode found_mode
= VOIDmode
, mode
;
78 /* We first look for the largest integer mode that can be validly
79 held in REGNO. If none, we look for the largest floating-point mode.
80 If we still didn't find a valid mode, try CCmode. */
82 for (mode
= GET_CLASS_NARROWEST_MODE (MODE_INT
); mode
!= VOIDmode
;
83 mode
= GET_MODE_WIDER_MODE (mode
))
84 if (HARD_REGNO_NREGS (regno
, mode
) == 1
85 && HARD_REGNO_MODE_OK (regno
, mode
))
88 if (found_mode
!= VOIDmode
)
91 for (mode
= GET_CLASS_NARROWEST_MODE (MODE_FLOAT
); mode
!= VOIDmode
;
92 mode
= GET_MODE_WIDER_MODE (mode
))
93 if (HARD_REGNO_NREGS (regno
, mode
) == 1
94 && HARD_REGNO_MODE_OK (regno
, mode
))
97 if (found_mode
!= VOIDmode
)
100 if (HARD_REGNO_NREGS (regno
, CCmode
) == 1
101 && HARD_REGNO_MODE_OK (regno
, CCmode
))
104 /* We can't find a mode valid for this register. */
108 /* Initialize for caller-save.
110 Look at all the hard registers that are used by a call and for which
111 regclass.c has not already excluded from being used across a call.
113 Ensure that we can find a mode to save the register and that there is a
114 simple insn to save and restore the register. This latter check avoids
115 problems that would occur if we tried to save the MQ register of some
116 machines directly into memory. */
121 char *first_obj
= (char *) oballoc (0);
127 /* First find all the registers that we need to deal with and all
128 the modes that they can have. If we can't find a mode to use,
129 we can't have the register live over calls. */
131 for (i
= 0; i
< FIRST_PSEUDO_REGISTER
; i
++)
133 if (call_used_regs
[i
] && ! call_fixed_regs
[i
])
135 regno_save_mode
[i
] = choose_hard_reg_mode (i
);
136 if (regno_save_mode
[i
] == VOIDmode
)
138 call_fixed_regs
[i
] = 1;
139 SET_HARD_REG_BIT (call_fixed_reg_set
, i
);
143 regno_save_mode
[i
] = VOIDmode
;
146 /* The following code tries to approximate the conditions under which
147 we can easily save and restore a register without scratch registers or
148 other complexities. It will usually work, except under conditions where
149 the validity of an insn operand is dependent on the address offset.
150 No such cases are currently known.
152 We first find a typical offset from some BASE_REG_CLASS register.
153 This address is chosen by finding the first register in the class
154 and by finding the smallest power of two that is a valid offset from
155 that register in every mode we will use to save registers. */
157 for (i
= 0; i
< FIRST_PSEUDO_REGISTER
; i
++)
158 if (TEST_HARD_REG_BIT (reg_class_contents
[(int) BASE_REG_CLASS
], i
))
161 if (i
== FIRST_PSEUDO_REGISTER
)
164 addr_reg
= gen_rtx (REG
, Pmode
, i
);
166 for (offset
= 1 << (HOST_BITS_PER_INT
/ 2); offset
; offset
>>= 1)
168 address
= gen_rtx (PLUS
, Pmode
, addr_reg
, GEN_INT (offset
));
170 for (i
= 0; i
< FIRST_PSEUDO_REGISTER
; i
++)
171 if (regno_save_mode
[i
] != VOIDmode
172 && ! strict_memory_address_p (regno_save_mode
[i
], address
))
175 if (i
== FIRST_PSEUDO_REGISTER
)
179 /* If we didn't find a valid address, we must use register indirect. */
183 /* Next we try to form an insn to save and restore the register. We
184 see if such an insn is recognized and meets its constraints. */
188 for (i
= 0; i
< FIRST_PSEUDO_REGISTER
; i
++)
189 if (regno_save_mode
[i
] != VOIDmode
)
191 rtx mem
= gen_rtx (MEM
, regno_save_mode
[i
], address
);
192 rtx reg
= gen_rtx (REG
, regno_save_mode
[i
], i
);
193 rtx savepat
= gen_rtx (SET
, VOIDmode
, mem
, reg
);
194 rtx restpat
= gen_rtx (SET
, VOIDmode
, reg
, mem
);
195 rtx saveinsn
= emit_insn (savepat
);
196 rtx restinsn
= emit_insn (restpat
);
199 reg_save_code
[i
] = recog_memoized (saveinsn
);
200 reg_restore_code
[i
] = recog_memoized (restinsn
);
202 /* Now extract both insns and see if we can meet their constraints. */
203 ok
= (reg_save_code
[i
] != -1 && reg_restore_code
[i
] != -1);
206 insn_extract (saveinsn
);
207 ok
= constrain_operands (reg_save_code
[i
], 1);
208 insn_extract (restinsn
);
209 ok
&= constrain_operands (reg_restore_code
[i
], 1);
214 call_fixed_regs
[i
] = 1;
215 SET_HARD_REG_BIT (call_fixed_reg_set
, i
);
224 /* Initialize save areas by showing that we haven't allocated any yet. */
231 for (i
= 0; i
< FIRST_PSEUDO_REGISTER
; i
++)
232 regno_save_mem
[i
] = 0;
235 /* Allocate save areas for any hard registers that might need saving.
236 We take a conservative approach here and look for call-clobbered hard
237 registers that are assigned to pseudos that cross calls. This may
238 overestimate slightly (especially if some of these registers are later
239 used as spill registers), but it should not be significant.
241 Then perform register elimination in the addresses of the save area
242 locations; return 1 if all eliminated addresses are strictly valid.
243 We assume that our caller has set up the elimination table to the
244 worst (largest) possible offsets.
246 Set *PCHANGED to 1 if we had to allocate some memory for the save area. */
249 setup_save_areas (pchanged
)
255 for (i
= FIRST_PSEUDO_REGISTER
; i
< max_regno
; i
++)
256 if (reg_renumber
[i
] >= 0 && reg_n_calls_crossed
[i
] > 0)
258 int regno
= reg_renumber
[i
];
260 = regno
+ HARD_REGNO_NREGS (regno
, GET_MODE (regno_reg_rtx
[i
]));
263 for (j
= regno
; j
< endregno
; j
++)
264 if (call_used_regs
[j
] && regno_save_mem
[j
] == 0)
267 = assign_stack_local (regno_save_mode
[j
],
268 GET_MODE_SIZE (regno_save_mode
[j
]), 0);
273 for (i
= 0; i
< FIRST_PSEUDO_REGISTER
; i
++)
274 if (regno_save_mem
[i
] != 0)
275 ok
&= strict_memory_address_p (regno_save_mode
[i
],
276 XEXP (eliminate_regs (regno_save_mem
[i
],
283 /* Find the places where hard regs are live across calls and save them.
285 INSN_MODE is the mode to assign to any insns that we add. This is used
286 by reload to determine whether or not reloads or register eliminations
287 need be done on these insns. */
290 save_call_clobbered_regs (insn_mode
)
291 enum machine_mode insn_mode
;
296 for (b
= 0; b
< n_basic_blocks
; b
++)
298 regset regs_live
= basic_block_live_at_start
[b
];
303 /* Compute hard regs live at start of block -- this is the
304 real hard regs marked live, plus live pseudo regs that
305 have been renumbered to hard regs. No registers have yet been
306 saved because we restore all of them before the end of the basic
310 hard_regs_live
= *regs_live
;
312 COPY_HARD_REG_SET (hard_regs_live
, regs_live
);
315 CLEAR_HARD_REG_SET (hard_regs_saved
);
318 for (offset
= 0, i
= 0; offset
< regset_size
; offset
++)
320 if (regs_live
[offset
] == 0)
321 i
+= REGSET_ELT_BITS
;
323 for (bit
= 1; bit
&& i
< max_regno
; bit
<<= 1, i
++)
324 if ((regs_live
[offset
] & bit
)
325 && (regno
= reg_renumber
[i
]) >= 0)
327 j
< regno
+ HARD_REGNO_NREGS (regno
,
328 PSEUDO_REGNO_MODE (i
));
330 SET_HARD_REG_BIT (hard_regs_live
, j
);
333 /* Now scan the insns in the block, keeping track of what hard
334 regs are live as we go. When we see a call, save the live
335 call-clobbered hard regs. */
337 for (insn
= basic_block_head
[b
]; ; insn
= NEXT_INSN (insn
))
339 RTX_CODE code
= GET_CODE (insn
);
341 if (GET_RTX_CLASS (code
) == 'i')
345 /* If some registers have been saved, see if INSN references
346 any of them. We must restore them before the insn if so. */
349 restore_referenced_regs (PATTERN (insn
), insn
, insn_mode
);
351 /* NB: the normal procedure is to first enliven any
352 registers set by insn, then deaden any registers that
353 had their last use at insn. This is incorrect now,
354 since multiple pseudos may have been mapped to the
355 same hard reg, and the death notes are ambiguous. So
356 it must be done in the other, safe, order. */
358 for (link
= REG_NOTES (insn
); link
; link
= XEXP (link
, 1))
359 if (REG_NOTE_KIND (link
) == REG_DEAD
)
360 clear_reg_live (XEXP (link
, 0));
362 /* When we reach a call, we need to save all registers that are
363 live, call-used, not fixed, and not already saved. We must
364 test at this point because registers that die in a CALL_INSN
365 are not live across the call and likewise for registers that
366 are born in the CALL_INSN. */
368 if (code
== CALL_INSN
)
369 for (regno
= 0; regno
< FIRST_PSEUDO_REGISTER
; regno
++)
370 if (call_used_regs
[regno
] && ! call_fixed_regs
[regno
]
371 && TEST_HARD_REG_BIT (hard_regs_live
, regno
)
372 && ! TEST_HARD_REG_BIT (hard_regs_saved
, regno
))
373 insert_save_restore (insn
, 1, regno
, insn_mode
);
375 note_stores (PATTERN (insn
), set_reg_live
);
377 for (link
= REG_NOTES (insn
); link
; link
= XEXP (link
, 1))
378 if (REG_NOTE_KIND (link
) == REG_UNUSED
)
379 clear_reg_live (XEXP (link
, 0));
382 if (insn
== basic_block_end
[b
])
386 /* At the end of the basic block, we must restore any registers that
387 remain saved. If the last insn in the block is a JUMP_INSN, put
388 the restore before the insn, otherwise, put it after the insn. */
391 for (regno
= 0; regno
< FIRST_PSEUDO_REGISTER
; regno
++)
392 if (TEST_HARD_REG_BIT (hard_regs_saved
, regno
))
393 insert_save_restore ((GET_CODE (insn
) == JUMP_INSN
394 ? insn
: NEXT_INSN (insn
)),
395 0, regno
, insn_mode
);
399 /* Here from note_stores when an insn stores a value in a register.
400 Set the proper bit or bits in hard_regs_live. All pseudos that have
401 been assigned hard regs have had their register number changed already,
402 so we can ignore pseudos. */
405 set_reg_live (reg
, setter
)
408 register int regno
, endregno
, i
;
409 enum machine_mode mode
= GET_MODE (reg
);
412 if (GET_CODE (reg
) == SUBREG
)
414 word
= SUBREG_WORD (reg
);
415 reg
= SUBREG_REG (reg
);
418 if (GET_CODE (reg
) != REG
|| REGNO (reg
) >= FIRST_PSEUDO_REGISTER
)
421 regno
= REGNO (reg
) + word
;
422 endregno
= regno
+ HARD_REGNO_NREGS (regno
, mode
);
424 for (i
= regno
; i
< endregno
; i
++)
425 SET_HARD_REG_BIT (hard_regs_live
, i
);
428 /* Here when a REG_DEAD note records the last use of a reg. Clear
429 the appropriate bit or bits in hard_regs_live. Again we can ignore
436 register int regno
, endregno
, i
;
438 if (GET_CODE (reg
) != REG
|| REGNO (reg
) >= FIRST_PSEUDO_REGISTER
)
442 endregno
= regno
+ HARD_REGNO_NREGS (regno
, GET_MODE (reg
));
444 for (i
= regno
; i
< endregno
; i
++)
445 CLEAR_HARD_REG_BIT (hard_regs_live
, i
);
448 /* If any register currently residing in the save area is referenced in X,
449 which is part of INSN, emit code to restore the register in front of INSN.
450 INSN_MODE is the mode to assign to any insns that we add. */
453 restore_referenced_regs (x
, insn
, insn_mode
)
456 enum machine_mode insn_mode
;
458 enum rtx_code code
= GET_CODE (x
);
464 int regno
= REGNO (x
);
466 /* If this is a pseudo, scan its memory location, since it might
467 involve the use of another register, which might be saved. */
469 if (regno
>= FIRST_PSEUDO_REGISTER
470 && reg_equiv_mem
[regno
] != 0)
471 restore_referenced_regs (XEXP (reg_equiv_mem
[regno
], 0),
473 else if (regno
>= FIRST_PSEUDO_REGISTER
474 && reg_equiv_address
[regno
] != 0)
475 restore_referenced_regs (reg_equiv_address
[regno
],
478 /* Otherwise if this is a hard register, restore any piece of it that
479 is currently saved. */
481 else if (regno
< FIRST_PSEUDO_REGISTER
)
483 int endregno
= regno
+ HARD_REGNO_NREGS (regno
, GET_MODE (x
));
485 for (i
= regno
; i
< endregno
; i
++)
486 if (TEST_HARD_REG_BIT (hard_regs_saved
, i
))
487 insert_save_restore (insn
, 0, i
, insn_mode
);
493 fmt
= GET_RTX_FORMAT (code
);
494 for (i
= GET_RTX_LENGTH (code
) - 1; i
>= 0; i
--)
497 restore_referenced_regs (XEXP (x
, i
), insn
, insn_mode
);
498 else if (fmt
[i
] == 'E')
499 for (j
= XVECLEN (x
, i
) - 1; j
>= 0; j
--)
500 restore_referenced_regs (XVECEXP (x
, i
, j
), insn
, insn_mode
);
504 /* Insert a sequence of insns to save or restore, SAVE_P says which,
505 REGNO. Place these insns in front of INSN. INSN_MODE is the mode
506 to assign to these insns.
508 Note that we have verified in init_caller_save that we can do this
509 with a simple SET, so use it. Set INSN_CODE to what we save there
510 since the address might not be valid so the insn might not be recognized.
511 These insns will be reloaded and have register elimination done by
512 find_reload, so we need not worry about that here. */
515 insert_save_restore (insn
, save_p
, regno
, insn_mode
)
519 enum machine_mode insn_mode
;
525 /* A common failure mode if register status is not correct in the RTL
526 is for this routine to be called with a REGNO we didn't expect to
527 save. That will cause us to write an insn with a (nil) SET_DEST
528 or SET_SRC. Instead of doing so and causing a crash later, check
529 for this common case and abort here instead. This will remove one
530 step in debugging such problems. */
532 if (regno_save_mem
[regno
] == 0)
535 /* If INSN is a CALL_INSN, we must insert our insns before any
536 USE insns in front of the CALL_INSN. */
538 if (GET_CODE (insn
) == CALL_INSN
)
539 while (GET_CODE (PREV_INSN (insn
)) == INSN
540 && GET_CODE (PATTERN (PREV_INSN (insn
))) == USE
)
541 insn
= PREV_INSN (insn
);
544 /* If INSN references CC0, put our insns in front of the insn that sets
545 CC0. This is always safe, since the only way we could be passed an
546 insn that references CC0 is for a restore, and doing a restore earlier
547 isn't a problem. We do, however, assume here that CALL_INSNs don't
548 reference CC0. Guard against non-INSN's like CODE_LABEL. */
550 if ((GET_CODE (insn
) == INSN
|| GET_CODE (insn
) == JUMP_INSN
)
551 && reg_referenced_p (cc0_rtx
, PATTERN (insn
)))
552 insn
= prev_nonnote_insn (insn
);
555 /* Get the pattern to emit and update our status. */
558 pat
= gen_rtx (SET
, VOIDmode
, regno_save_mem
[regno
],
559 gen_rtx (REG
, regno_save_mode
[regno
], regno
));
560 code
= reg_save_code
[regno
];
561 SET_HARD_REG_BIT (hard_regs_saved
, regno
);
566 pat
= gen_rtx (SET
, VOIDmode
,
567 gen_rtx (REG
, regno_save_mode
[regno
], regno
),
568 regno_save_mem
[regno
]);
569 code
= reg_restore_code
[regno
];
570 CLEAR_HARD_REG_BIT (hard_regs_saved
, regno
);
574 /* Emit the insn and set the code and mode. */
576 insn
= emit_insn_before (pat
, insn
);
577 PUT_MODE (insn
, insn_mode
);
578 INSN_CODE (insn
) = code
;