]> git.ipfire.org Git - thirdparty/gcc.git/blame - gcc/caller-save.c
revert: tree.h (phi_arg_d): New field.
[thirdparty/gcc.git] / gcc / caller-save.c
CommitLineData
c5986054 1/* Save and restore call-clobbered registers which are live across a call.
a446b4e8 2 Copyright (C) 1989, 1992, 1994, 1995, 1997, 1998, 1999, 2000,
e90b336f 3 2001, 2002, 2003, 2004, 2005, 2006, 2007, 2008, 2009, 2010, 2011
66647d44 4 Free Software Foundation, Inc.
c5986054 5
1322177d 6This file is part of GCC.
c5986054 7
1322177d
LB
8GCC is free software; you can redistribute it and/or modify it under
9the terms of the GNU General Public License as published by the Free
9dcd6f09 10Software Foundation; either version 3, or (at your option) any later
1322177d 11version.
c5986054 12
1322177d
LB
13GCC is distributed in the hope that it will be useful, but WITHOUT ANY
14WARRANTY; without even the implied warranty of MERCHANTABILITY or
15FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
16for more details.
c5986054
RS
17
18You should have received a copy of the GNU General Public License
9dcd6f09
NC
19along with GCC; see the file COPYING3. If not see
20<http://www.gnu.org/licenses/>. */
c5986054
RS
21
22#include "config.h"
670ee920 23#include "system.h"
4977bab6
ZW
24#include "coretypes.h"
25#include "tm.h"
c5986054 26#include "rtl.h"
7932a3db 27#include "regs.h"
c5986054
RS
28#include "insn-config.h"
29#include "flags.h"
c5986054
RS
30#include "hard-reg-set.h"
31#include "recog.h"
32#include "basic-block.h"
7a8cba34 33#include "df.h"
c5986054 34#include "reload.h"
49ad7cfa 35#include "function.h"
c5986054 36#include "expr.h"
718f9c0f 37#include "diagnostic-core.h"
63290521 38#include "tm_p.h"
f6c4ed08 39#include "addresses.h"
78187f5a 40#include "ggc.h"
c5986054 41
7609e720
BS
42#define MOVE_MAX_WORDS (MOVE_MAX / UNITS_PER_WORD)
43
bcbaaba1
RS
44#define regno_save_mode \
45 (this_target_reload->x_regno_save_mode)
cc14227f
RS
46#define cached_reg_save_code \
47 (this_target_reload->x_cached_reg_save_code)
48#define cached_reg_restore_code \
49 (this_target_reload->x_cached_reg_restore_code)
c5986054
RS
50
51/* For each hard register, a place on the stack where it can be saved,
52 if needed. */
53
d329e058 54static rtx
ef0e53ce 55 regno_save_mem[FIRST_PSEUDO_REGISTER][MAX_MOVE_MAX / MIN_UNITS_PER_WORD + 1];
c5986054 56
058e97ec
VM
57/* The number of elements in the subsequent array. */
58static int save_slots_num;
59
60/* Allocated slots so far. */
61static rtx save_slots[FIRST_PSEUDO_REGISTER];
62
c5986054
RS
63/* Set of hard regs currently residing in save area (during insn scan). */
64
65static HARD_REG_SET hard_regs_saved;
66
7609e720 67/* Number of registers currently in hard_regs_saved. */
f95361c8 68
7609e720 69static int n_regs_saved;
f95361c8 70
7609e720
BS
71/* Computed by mark_referenced_regs, all regs referenced in a given
72 insn. */
73static HARD_REG_SET referenced_regs;
c5986054 74
7609e720 75
b5b8b0ac
AO
76typedef void refmarker_fn (rtx *loc, enum machine_mode mode, int hardregno,
77 void *mark_arg);
78
058e97ec
VM
79static int reg_save_code (int, enum machine_mode);
80static int reg_restore_code (int, enum machine_mode);
81
82struct saved_hard_reg;
83static void initiate_saved_hard_regs (void);
e16b6fd0 84static void new_saved_hard_reg (int, int);
058e97ec
VM
85static void finish_saved_hard_regs (void);
86static int saved_hard_reg_compare_func (const void *, const void *);
87
7bc980e1 88static void mark_set_regs (rtx, const_rtx, void *);
b5b8b0ac
AO
89static void mark_referenced_regs (rtx *, refmarker_fn *mark, void *mark_arg);
90static refmarker_fn mark_reg_as_referenced;
91static refmarker_fn replace_reg_with_saved_mem;
d329e058
AJ
92static int insert_save (struct insn_chain *, int, int, HARD_REG_SET *,
93 enum machine_mode *);
94static int insert_restore (struct insn_chain *, int, int, int,
95 enum machine_mode *);
96static struct insn_chain *insert_one_insn (struct insn_chain *, int, int,
97 rtx);
7bc980e1 98static void add_stored_regs (rtx, const_rtx, void *);
058e97ec 99
c5986054 100\f
058e97ec 101
78187f5a
JH
102static GTY(()) rtx savepat;
103static GTY(()) rtx restpat;
104static GTY(()) rtx test_reg;
105static GTY(()) rtx test_mem;
106static GTY(()) rtx saveinsn;
107static GTY(()) rtx restinsn;
108
109/* Return the INSN_CODE used to save register REG in mode MODE. */
110static int
111reg_save_code (int reg, enum machine_mode mode)
112{
113 bool ok;
114 if (cached_reg_save_code[reg][mode])
115 return cached_reg_save_code[reg][mode];
116 if (!HARD_REGNO_MODE_OK (reg, mode))
f57e2756
JR
117 {
118 /* Depending on how HARD_REGNO_MODE_OK is defined, range propagation
119 might deduce here that reg >= FIRST_PSEUDO_REGISTER. So the assert
120 below silences a warning. */
121 gcc_assert (reg < FIRST_PSEUDO_REGISTER);
122 cached_reg_save_code[reg][mode] = -1;
123 cached_reg_restore_code[reg][mode] = -1;
124 return -1;
125 }
78187f5a
JH
126
127 /* Update the register number and modes of the register
128 and memory operand. */
5444da31 129 SET_REGNO_RAW (test_reg, reg);
78187f5a
JH
130 PUT_MODE (test_reg, mode);
131 PUT_MODE (test_mem, mode);
132
133 /* Force re-recognition of the modified insns. */
134 INSN_CODE (saveinsn) = -1;
f81e79b5 135 INSN_CODE (restinsn) = -1;
78187f5a
JH
136
137 cached_reg_save_code[reg][mode] = recog_memoized (saveinsn);
138 cached_reg_restore_code[reg][mode] = recog_memoized (restinsn);
139
140 /* Now extract both insns and see if we can meet their
141 constraints. */
142 ok = (cached_reg_save_code[reg][mode] != -1
143 && cached_reg_restore_code[reg][mode] != -1);
144 if (ok)
145 {
146 extract_insn (saveinsn);
147 ok = constrain_operands (1);
148 extract_insn (restinsn);
149 ok &= constrain_operands (1);
150 }
151
152 if (! ok)
153 {
154 cached_reg_save_code[reg][mode] = -1;
155 cached_reg_restore_code[reg][mode] = -1;
156 }
157 gcc_assert (cached_reg_save_code[reg][mode]);
158 return cached_reg_save_code[reg][mode];
159}
160
161/* Return the INSN_CODE used to restore register REG in mode MODE. */
162static int
163reg_restore_code (int reg, enum machine_mode mode)
164{
165 if (cached_reg_restore_code[reg][mode])
166 return cached_reg_restore_code[reg][mode];
167 /* Populate our cache. */
168 reg_save_code (reg, mode);
169 return cached_reg_restore_code[reg][mode];
170}
171\f
c5986054
RS
172/* Initialize for caller-save.
173
174 Look at all the hard registers that are used by a call and for which
328e13b7 175 reginfo.c has not already excluded from being used across a call.
c5986054 176
d329e058 177 Ensure that we can find a mode to save the register and that there is a
c5986054
RS
178 simple insn to save and restore the register. This latter check avoids
179 problems that would occur if we tried to save the MQ register of some
180 machines directly into memory. */
181
182void
d329e058 183init_caller_save (void)
c5986054 184{
c5986054
RS
185 rtx addr_reg;
186 int offset;
187 rtx address;
f95361c8 188 int i, j;
c5986054 189
dc12b70e
JZ
190 if (caller_save_initialized_p)
191 return;
192
193 caller_save_initialized_p = true;
194
058e97ec 195 CLEAR_HARD_REG_SET (no_caller_save_reg_set);
c5986054
RS
196 /* First find all the registers that we need to deal with and all
197 the modes that they can have. If we can't find a mode to use,
198 we can't have the register live over calls. */
199
200 for (i = 0; i < FIRST_PSEUDO_REGISTER; i++)
201 {
adee59ae
AS
202 if (call_used_regs[i]
203 && !TEST_HARD_REG_BIT (call_fixed_reg_set, i))
c5986054 204 {
7609e720 205 for (j = 1; j <= MOVE_MAX_WORDS; j++)
c5986054 206 {
787dc842
JH
207 regno_save_mode[i][j] = HARD_REGNO_CALLER_SAVE_MODE (i, j,
208 VOIDmode);
f95361c8
JL
209 if (regno_save_mode[i][j] == VOIDmode && j == 1)
210 {
f95361c8
JL
211 SET_HARD_REG_BIT (call_fixed_reg_set, i);
212 }
c5986054
RS
213 }
214 }
215 else
f95361c8 216 regno_save_mode[i][1] = VOIDmode;
c5986054
RS
217 }
218
219 /* The following code tries to approximate the conditions under which
220 we can easily save and restore a register without scratch registers or
221 other complexities. It will usually work, except under conditions where
222 the validity of an insn operand is dependent on the address offset.
223 No such cases are currently known.
224
225 We first find a typical offset from some BASE_REG_CLASS register.
226 This address is chosen by finding the first register in the class
227 and by finding the smallest power of two that is a valid offset from
228 that register in every mode we will use to save registers. */
229
230 for (i = 0; i < FIRST_PSEUDO_REGISTER; i++)
3dcc68a4
NC
231 if (TEST_HARD_REG_BIT
232 (reg_class_contents
86fc3d06
UW
233 [(int) base_reg_class (regno_save_mode[i][1], ADDR_SPACE_GENERIC,
234 PLUS, CONST_INT)], i))
c5986054
RS
235 break;
236
366de0ce 237 gcc_assert (i < FIRST_PSEUDO_REGISTER);
c5986054 238
38a448ca 239 addr_reg = gen_rtx_REG (Pmode, i);
c5986054
RS
240
241 for (offset = 1 << (HOST_BITS_PER_INT / 2); offset; offset >>= 1)
242 {
38a448ca 243 address = gen_rtx_PLUS (Pmode, addr_reg, GEN_INT (offset));
c5986054
RS
244
245 for (i = 0; i < FIRST_PSEUDO_REGISTER; i++)
f95361c8
JL
246 if (regno_save_mode[i][1] != VOIDmode
247 && ! strict_memory_address_p (regno_save_mode[i][1], address))
c5986054
RS
248 break;
249
250 if (i == FIRST_PSEUDO_REGISTER)
251 break;
252 }
253
254 /* If we didn't find a valid address, we must use register indirect. */
255 if (offset == 0)
256 address = addr_reg;
257
258 /* Next we try to form an insn to save and restore the register. We
d329e058 259 see if such an insn is recognized and meets its constraints.
bf1660a6
JL
260
261 To avoid lots of unnecessary RTL allocation, we construct all the RTL
262 once, then modify the memory and register operands in-place. */
263
264 test_reg = gen_rtx_REG (VOIDmode, 0);
265 test_mem = gen_rtx_MEM (VOIDmode, address);
266 savepat = gen_rtx_SET (VOIDmode, test_mem, test_reg);
267 restpat = gen_rtx_SET (VOIDmode, test_reg, test_mem);
c5986054 268
418e920f
BS
269 saveinsn = gen_rtx_INSN (VOIDmode, 0, 0, 0, 0, savepat, 0, -1, 0);
270 restinsn = gen_rtx_INSN (VOIDmode, 0, 0, 0, 0, restpat, 0, -1, 0);
0db79a6b 271
787dc842
JH
272 for (i = 0; i < FIRST_PSEUDO_REGISTER; i++)
273 for (j = 1; j <= MOVE_MAX_WORDS; j++)
78187f5a 274 if (reg_save_code (i,regno_save_mode[i][j]) == -1)
787dc842
JH
275 {
276 regno_save_mode[i][j] = VOIDmode;
277 if (j == 1)
278 {
787dc842 279 SET_HARD_REG_BIT (call_fixed_reg_set, i);
058e97ec
VM
280 if (call_used_regs[i])
281 SET_HARD_REG_BIT (no_caller_save_reg_set, i);
787dc842
JH
282 }
283 }
c5986054 284}
058e97ec 285
c5986054 286\f
058e97ec 287
c5986054
RS
288/* Initialize save areas by showing that we haven't allocated any yet. */
289
290void
d329e058 291init_save_areas (void)
c5986054 292{
f95361c8 293 int i, j;
c5986054
RS
294
295 for (i = 0; i < FIRST_PSEUDO_REGISTER; i++)
7609e720 296 for (j = 1; j <= MOVE_MAX_WORDS; j++)
f95361c8 297 regno_save_mem[i][j] = 0;
058e97ec 298 save_slots_num = 0;
b8698a0f 299
058e97ec
VM
300}
301
302/* The structure represents a hard register which should be saved
303 through the call. It is used when the integrated register
304 allocator (IRA) is used and sharing save slots is on. */
305struct saved_hard_reg
306{
307 /* Order number starting with 0. */
308 int num;
309 /* The hard regno. */
310 int hard_regno;
311 /* Execution frequency of all calls through which given hard
312 register should be saved. */
313 int call_freq;
314 /* Stack slot reserved to save the hard register through calls. */
315 rtx slot;
316 /* True if it is first hard register in the chain of hard registers
317 sharing the same stack slot. */
318 int first_p;
319 /* Order number of the next hard register structure with the same
320 slot in the chain. -1 represents end of the chain. */
321 int next;
322};
323
324/* Map: hard register number to the corresponding structure. */
325static struct saved_hard_reg *hard_reg_map[FIRST_PSEUDO_REGISTER];
326
327/* The number of all structures representing hard registers should be
328 saved, in order words, the number of used elements in the following
329 array. */
330static int saved_regs_num;
331
332/* Pointers to all the structures. Index is the order number of the
333 corresponding structure. */
334static struct saved_hard_reg *all_saved_regs[FIRST_PSEUDO_REGISTER];
335
336/* First called function for work with saved hard registers. */
337static void
338initiate_saved_hard_regs (void)
339{
340 int i;
341
342 saved_regs_num = 0;
343 for (i = 0; i < FIRST_PSEUDO_REGISTER; i++)
344 hard_reg_map[i] = NULL;
345}
346
347/* Allocate and return new saved hard register with given REGNO and
348 CALL_FREQ. */
e16b6fd0 349static void
058e97ec
VM
350new_saved_hard_reg (int regno, int call_freq)
351{
352 struct saved_hard_reg *saved_reg;
353
354 saved_reg
355 = (struct saved_hard_reg *) xmalloc (sizeof (struct saved_hard_reg));
356 hard_reg_map[regno] = all_saved_regs[saved_regs_num] = saved_reg;
357 saved_reg->num = saved_regs_num++;
358 saved_reg->hard_regno = regno;
359 saved_reg->call_freq = call_freq;
360 saved_reg->first_p = FALSE;
361 saved_reg->next = -1;
058e97ec
VM
362}
363
364/* Free memory allocated for the saved hard registers. */
365static void
366finish_saved_hard_regs (void)
367{
368 int i;
369
370 for (i = 0; i < saved_regs_num; i++)
371 free (all_saved_regs[i]);
372}
373
374/* The function is used to sort the saved hard register structures
375 according their frequency. */
376static int
377saved_hard_reg_compare_func (const void *v1p, const void *v2p)
378{
379 const struct saved_hard_reg *p1 = *(struct saved_hard_reg * const *) v1p;
380 const struct saved_hard_reg *p2 = *(struct saved_hard_reg * const *) v2p;
b8698a0f 381
058e97ec
VM
382 if (flag_omit_frame_pointer)
383 {
384 if (p1->call_freq - p2->call_freq != 0)
385 return p1->call_freq - p2->call_freq;
386 }
387 else if (p2->call_freq - p1->call_freq != 0)
388 return p2->call_freq - p1->call_freq;
389
390 return p1->num - p2->num;
c5986054
RS
391}
392
393/* Allocate save areas for any hard registers that might need saving.
394 We take a conservative approach here and look for call-clobbered hard
395 registers that are assigned to pseudos that cross calls. This may
396 overestimate slightly (especially if some of these registers are later
397 used as spill registers), but it should not be significant.
398
058e97ec
VM
399 For IRA we use priority coloring to decrease stack slots needed for
400 saving hard registers through calls. We build conflicts for them
401 to do coloring.
402
f95361c8
JL
403 Future work:
404
405 In the fallback case we should iterate backwards across all possible
d329e058 406 modes for the save, choosing the largest available one instead of
f95361c8
JL
407 falling back to the smallest mode immediately. (eg TF -> DF -> SF).
408
409 We do not try to use "move multiple" instructions that exist
d329e058 410 on some machines (such as the 68k moveml). It could be a win to try
f95361c8 411 and use them when possible. The hard part is doing it in a way that is
d329e058 412 machine independent since they might be saving non-consecutive
f95361c8 413 registers. (imagine caller-saving d0,d1,a0,a1 on the 68k) */
c5986054 414
437a710d 415void
d329e058 416setup_save_areas (void)
c5986054 417{
e90b336f 418 int i, j, k, freq;
f95361c8 419 HARD_REG_SET hard_regs_used;
e90b336f
JL
420 struct saved_hard_reg *saved_reg;
421 rtx insn;
422 struct insn_chain *chain, *next;
423 unsigned int regno;
424 HARD_REG_SET hard_regs_to_save, used_regs, this_insn_sets;
425 reg_set_iterator rsi;
f95361c8 426
f95361c8 427 CLEAR_HARD_REG_SET (hard_regs_used);
f95361c8 428
e90b336f
JL
429 /* Find every CALL_INSN and record which hard regs are live across the
430 call into HARD_REG_MAP and HARD_REGS_USED. */
431 initiate_saved_hard_regs ();
432 /* Create hard reg saved regs. */
433 for (chain = reload_insn_chain; chain != 0; chain = next)
058e97ec 434 {
e384e6b5
BS
435 rtx cheap;
436
e90b336f
JL
437 insn = chain->insn;
438 next = chain->next;
439 if (!CALL_P (insn)
440 || find_reg_note (insn, REG_NORETURN, NULL))
441 continue;
442 freq = REG_FREQ_FROM_BB (BLOCK_FOR_INSN (insn));
443 REG_SET_TO_HARD_REG_SET (hard_regs_to_save,
444 &chain->live_throughout);
445 COPY_HARD_REG_SET (used_regs, call_used_reg_set);
446
447 /* Record all registers set in this call insn. These don't
448 need to be saved. N.B. the call insn might set a subreg
449 of a multi-hard-reg pseudo; then the pseudo is considered
450 live during the call, but the subreg that is set
451 isn't. */
452 CLEAR_HARD_REG_SET (this_insn_sets);
453 note_stores (PATTERN (insn), mark_set_regs, &this_insn_sets);
454 /* Sibcalls are considered to set the return value. */
455 if (SIBLING_CALL_P (insn) && crtl->return_rtx)
456 mark_set_regs (crtl->return_rtx, NULL_RTX, &this_insn_sets);
457
458 AND_COMPL_HARD_REG_SET (used_regs, call_fixed_reg_set);
459 AND_COMPL_HARD_REG_SET (used_regs, this_insn_sets);
460 AND_HARD_REG_SET (hard_regs_to_save, used_regs);
461 for (regno = 0; regno < FIRST_PSEUDO_REGISTER; regno++)
462 if (TEST_HARD_REG_BIT (hard_regs_to_save, regno))
463 {
464 if (hard_reg_map[regno] != NULL)
465 hard_reg_map[regno]->call_freq += freq;
466 else
e16b6fd0 467 new_saved_hard_reg (regno, freq);
e90b336f
JL
468 SET_HARD_REG_BIT (hard_regs_used, regno);
469 }
e384e6b5
BS
470 cheap = find_reg_note (insn, REG_RETURNED, NULL);
471 if (cheap)
472 cheap = XEXP (cheap, 0);
e90b336f
JL
473 /* Look through all live pseudos, mark their hard registers. */
474 EXECUTE_IF_SET_IN_REG_SET
475 (&chain->live_throughout, FIRST_PSEUDO_REGISTER, regno, rsi)
058e97ec 476 {
e90b336f
JL
477 int r = reg_renumber[regno];
478 int bound;
058e97ec 479
e384e6b5 480 if (r < 0 || regno_reg_rtx[regno] == cheap)
e90b336f 481 continue;
058e97ec 482
e90b336f
JL
483 bound = r + hard_regno_nregs[r][PSEUDO_REGNO_MODE (regno)];
484 for (; r < bound; r++)
485 if (TEST_HARD_REG_BIT (used_regs, r))
058e97ec 486 {
e90b336f
JL
487 if (hard_reg_map[r] != NULL)
488 hard_reg_map[r]->call_freq += freq;
058e97ec 489 else
e16b6fd0 490 new_saved_hard_reg (r, freq);
e90b336f
JL
491 SET_HARD_REG_BIT (hard_regs_to_save, r);
492 SET_HARD_REG_BIT (hard_regs_used, r);
058e97ec 493 }
e90b336f
JL
494 }
495 }
b8698a0f 496
e90b336f
JL
497 /* If requested, figure out which hard regs can share save slots. */
498 if (optimize && flag_ira_share_save_slots)
499 {
500 rtx slot;
501 char *saved_reg_conflicts;
502 int next_k;
503 struct saved_hard_reg *saved_reg2, *saved_reg3;
504 int call_saved_regs_num;
505 struct saved_hard_reg *call_saved_regs[FIRST_PSEUDO_REGISTER];
506 int best_slot_num;
507 int prev_save_slots_num;
508 rtx prev_save_slots[FIRST_PSEUDO_REGISTER];
b8698a0f 509
058e97ec
VM
510 /* Find saved hard register conflicts. */
511 saved_reg_conflicts = (char *) xmalloc (saved_regs_num * saved_regs_num);
512 memset (saved_reg_conflicts, 0, saved_regs_num * saved_regs_num);
513 for (chain = reload_insn_chain; chain != 0; chain = next)
514 {
e384e6b5 515 rtx cheap;
058e97ec
VM
516 call_saved_regs_num = 0;
517 insn = chain->insn;
518 next = chain->next;
481683e1 519 if (!CALL_P (insn)
058e97ec
VM
520 || find_reg_note (insn, REG_NORETURN, NULL))
521 continue;
e384e6b5
BS
522
523 cheap = find_reg_note (insn, REG_RETURNED, NULL);
524 if (cheap)
525 cheap = XEXP (cheap, 0);
526
058e97ec
VM
527 REG_SET_TO_HARD_REG_SET (hard_regs_to_save,
528 &chain->live_throughout);
529 COPY_HARD_REG_SET (used_regs, call_used_reg_set);
530
531 /* Record all registers set in this call insn. These don't
532 need to be saved. N.B. the call insn might set a subreg
533 of a multi-hard-reg pseudo; then the pseudo is considered
534 live during the call, but the subreg that is set
535 isn't. */
536 CLEAR_HARD_REG_SET (this_insn_sets);
537 note_stores (PATTERN (insn), mark_set_regs, &this_insn_sets);
538 /* Sibcalls are considered to set the return value,
912eb5a4 539 compare df-scan.c:df_get_call_refs. */
058e97ec
VM
540 if (SIBLING_CALL_P (insn) && crtl->return_rtx)
541 mark_set_regs (crtl->return_rtx, NULL_RTX, &this_insn_sets);
542
543 AND_COMPL_HARD_REG_SET (used_regs, call_fixed_reg_set);
544 AND_COMPL_HARD_REG_SET (used_regs, this_insn_sets);
545 AND_HARD_REG_SET (hard_regs_to_save, used_regs);
546 for (regno = 0; regno < FIRST_PSEUDO_REGISTER; regno++)
547 if (TEST_HARD_REG_BIT (hard_regs_to_save, regno))
548 {
549 gcc_assert (hard_reg_map[regno] != NULL);
550 call_saved_regs[call_saved_regs_num++] = hard_reg_map[regno];
551 }
552 /* Look through all live pseudos, mark their hard registers. */
553 EXECUTE_IF_SET_IN_REG_SET
554 (&chain->live_throughout, FIRST_PSEUDO_REGISTER, regno, rsi)
c5986054 555 {
058e97ec
VM
556 int r = reg_renumber[regno];
557 int bound;
b8698a0f 558
e384e6b5 559 if (r < 0 || regno_reg_rtx[regno] == cheap)
058e97ec
VM
560 continue;
561
562 bound = r + hard_regno_nregs[r][PSEUDO_REGNO_MODE (regno)];
563 for (; r < bound; r++)
564 if (TEST_HARD_REG_BIT (used_regs, r))
565 call_saved_regs[call_saved_regs_num++] = hard_reg_map[r];
c5986054 566 }
058e97ec
VM
567 for (i = 0; i < call_saved_regs_num; i++)
568 {
569 saved_reg = call_saved_regs[i];
570 for (j = 0; j < call_saved_regs_num; j++)
571 if (i != j)
572 {
573 saved_reg2 = call_saved_regs[j];
574 saved_reg_conflicts[saved_reg->num * saved_regs_num
575 + saved_reg2->num]
576 = saved_reg_conflicts[saved_reg2->num * saved_regs_num
577 + saved_reg->num]
578 = TRUE;
579 }
580 }
581 }
582 /* Sort saved hard regs. */
583 qsort (all_saved_regs, saved_regs_num, sizeof (struct saved_hard_reg *),
584 saved_hard_reg_compare_func);
585 /* Initiate slots available from the previous reload
586 iteration. */
587 prev_save_slots_num = save_slots_num;
588 memcpy (prev_save_slots, save_slots, save_slots_num * sizeof (rtx));
589 save_slots_num = 0;
590 /* Allocate stack slots for the saved hard registers. */
591 for (i = 0; i < saved_regs_num; i++)
592 {
593 saved_reg = all_saved_regs[i];
594 regno = saved_reg->hard_regno;
595 for (j = 0; j < i; j++)
596 {
597 saved_reg2 = all_saved_regs[j];
598 if (! saved_reg2->first_p)
599 continue;
600 slot = saved_reg2->slot;
601 for (k = j; k >= 0; k = next_k)
602 {
603 saved_reg3 = all_saved_regs[k];
604 next_k = saved_reg3->next;
605 if (saved_reg_conflicts[saved_reg->num * saved_regs_num
606 + saved_reg3->num])
607 break;
608 }
609 if (k < 0
610 && (GET_MODE_SIZE (regno_save_mode[regno][1])
611 <= GET_MODE_SIZE (regno_save_mode
612 [saved_reg2->hard_regno][1])))
613 {
614 saved_reg->slot
615 = adjust_address_nv
616 (slot, regno_save_mode[saved_reg->hard_regno][1], 0);
617 regno_save_mem[regno][1] = saved_reg->slot;
618 saved_reg->next = saved_reg2->next;
619 saved_reg2->next = i;
620 if (dump_file != NULL)
621 fprintf (dump_file, "%d uses slot of %d\n",
622 regno, saved_reg2->hard_regno);
623 break;
624 }
625 }
626 if (j == i)
627 {
628 saved_reg->first_p = TRUE;
629 for (best_slot_num = -1, j = 0; j < prev_save_slots_num; j++)
630 {
631 slot = prev_save_slots[j];
632 if (slot == NULL_RTX)
633 continue;
634 if (GET_MODE_SIZE (regno_save_mode[regno][1])
635 <= GET_MODE_SIZE (GET_MODE (slot))
636 && best_slot_num < 0)
637 best_slot_num = j;
638 if (GET_MODE (slot) == regno_save_mode[regno][1])
639 break;
640 }
641 if (best_slot_num >= 0)
642 {
643 saved_reg->slot = prev_save_slots[best_slot_num];
644 saved_reg->slot
645 = adjust_address_nv
646 (saved_reg->slot,
647 regno_save_mode[saved_reg->hard_regno][1], 0);
648 if (dump_file != NULL)
649 fprintf (dump_file,
650 "%d uses a slot from prev iteration\n", regno);
651 prev_save_slots[best_slot_num] = NULL_RTX;
652 if (best_slot_num + 1 == prev_save_slots_num)
653 prev_save_slots_num--;
654 }
655 else
656 {
657 saved_reg->slot
658 = assign_stack_local_1
659 (regno_save_mode[regno][1],
80a832cd
JJ
660 GET_MODE_SIZE (regno_save_mode[regno][1]), 0,
661 ASLK_REDUCE_ALIGN);
058e97ec
VM
662 if (dump_file != NULL)
663 fprintf (dump_file, "%d uses a new slot\n", regno);
664 }
665 regno_save_mem[regno][1] = saved_reg->slot;
666 save_slots[save_slots_num++] = saved_reg->slot;
667 }
668 }
669 free (saved_reg_conflicts);
670 finish_saved_hard_regs ();
671 }
672 else
673 {
e90b336f
JL
674 /* We are not sharing slots.
675
676 Run through all the call-used hard-registers and allocate
677 space for each in the caller-save area. Try to allocate space
058e97ec 678 in a manner which allows multi-register saves/restores to be done. */
b8698a0f 679
058e97ec
VM
680 for (i = 0; i < FIRST_PSEUDO_REGISTER; i++)
681 for (j = MOVE_MAX_WORDS; j > 0; j--)
682 {
683 int do_save = 1;
b8698a0f 684
058e97ec
VM
685 /* If no mode exists for this size, try another. Also break out
686 if we have already saved this hard register. */
687 if (regno_save_mode[i][j] == VOIDmode || regno_save_mem[i][1] != 0)
688 continue;
b8698a0f 689
058e97ec
VM
690 /* See if any register in this group has been saved. */
691 for (k = 0; k < j; k++)
692 if (regno_save_mem[i + k][1])
693 {
694 do_save = 0;
695 break;
696 }
697 if (! do_save)
698 continue;
b8698a0f 699
058e97ec
VM
700 for (k = 0; k < j; k++)
701 if (! TEST_HARD_REG_BIT (hard_regs_used, i + k))
702 {
703 do_save = 0;
704 break;
705 }
706 if (! do_save)
707 continue;
b8698a0f 708
058e97ec
VM
709 /* We have found an acceptable mode to store in. Since
710 hard register is always saved in the widest mode
711 available, the mode may be wider than necessary, it is
712 OK to reduce the alignment of spill space. We will
713 verify that it is equal to or greater than required
714 when we restore and save the hard register in
715 insert_restore and insert_save. */
716 regno_save_mem[i][j]
717 = assign_stack_local_1 (regno_save_mode[i][j],
718 GET_MODE_SIZE (regno_save_mode[i][j]),
80a832cd 719 0, ASLK_REDUCE_ALIGN);
b8698a0f 720
058e97ec
VM
721 /* Setup single word save area just in case... */
722 for (k = 0; k < j; k++)
723 /* This should not depend on WORDS_BIG_ENDIAN.
724 The order of words in regs is the same as in memory. */
725 regno_save_mem[i + k][1]
726 = adjust_address_nv (regno_save_mem[i][j],
727 regno_save_mode[i + k][1],
728 k * UNITS_PER_WORD);
729 }
730 }
3bdf5ad1
RK
731
732 /* Now loop again and set the alias set of any save areas we made to
733 the alias set used to represent frame objects. */
734 for (i = 0; i < FIRST_PSEUDO_REGISTER; i++)
735 for (j = MOVE_MAX_WORDS; j > 0; j--)
736 if (regno_save_mem[i][j] != 0)
ba4828e0 737 set_mem_alias_set (regno_save_mem[i][j], get_frame_alias_set ());
c5986054 738}
058e97ec 739
c5986054 740\f
058e97ec 741
437a710d 742/* Find the places where hard regs are live across calls and save them. */
3bdf5ad1 743
c5986054 744void
d329e058 745save_call_clobbered_regs (void)
c5986054 746{
169e464e 747 struct insn_chain *chain, *next, *last = NULL;
787dc842 748 enum machine_mode save_mode [FIRST_PSEUDO_REGISTER];
7609e720 749
e30edd6c
R
750 /* Computed in mark_set_regs, holds all registers set by the current
751 instruction. */
752 HARD_REG_SET this_insn_sets;
753
7609e720
BS
754 CLEAR_HARD_REG_SET (hard_regs_saved);
755 n_regs_saved = 0;
c5986054 756
7609e720 757 for (chain = reload_insn_chain; chain != 0; chain = next)
c5986054 758 {
7609e720
BS
759 rtx insn = chain->insn;
760 enum rtx_code code = GET_CODE (insn);
761
762 next = chain->next;
763
366de0ce 764 gcc_assert (!chain->is_caller_save_insn);
7609e720 765
b5b8b0ac 766 if (NONDEBUG_INSN_P (insn))
c5986054 767 {
7609e720
BS
768 /* If some registers have been saved, see if INSN references
769 any of them. We must restore them before the insn if so. */
c5986054 770
7609e720 771 if (n_regs_saved)
c5986054 772 {
7609e720 773 int regno;
14292418 774 HARD_REG_SET this_insn_sets;
7609e720
BS
775
776 if (code == JUMP_INSN)
777 /* Restore all registers if this is a JUMP_INSN. */
778 COPY_HARD_REG_SET (referenced_regs, hard_regs_saved);
0e6362d9 779 else
0887bd4b 780 {
7609e720 781 CLEAR_HARD_REG_SET (referenced_regs);
b5b8b0ac
AO
782 mark_referenced_regs (&PATTERN (insn),
783 mark_reg_as_referenced, NULL);
7609e720 784 AND_HARD_REG_SET (referenced_regs, hard_regs_saved);
0887bd4b 785 }
c5986054 786
7609e720
BS
787 for (regno = 0; regno < FIRST_PSEUDO_REGISTER; regno++)
788 if (TEST_HARD_REG_BIT (referenced_regs, regno))
14292418
BS
789 regno += insert_restore (chain, 1, regno, MOVE_MAX_WORDS,
790 save_mode);
791 /* If a saved register is set after the call, this means we no
792 longer should restore it. This can happen when parts of a
793 multi-word pseudo do not conflict with other pseudos, so
794 IRA may allocate the same hard register for both. One may
795 be live across the call, while the other is set
796 afterwards. */
797 CLEAR_HARD_REG_SET (this_insn_sets);
798 note_stores (PATTERN (insn), mark_set_regs, &this_insn_sets);
799 AND_COMPL_HARD_REG_SET (hard_regs_saved, this_insn_sets);
c5986054
RS
800 }
801
ed765125
PB
802 if (code == CALL_INSN
803 && ! SIBLING_CALL_P (insn)
804 && ! find_reg_note (insn, REG_NORETURN, NULL))
7609e720 805 {
3cd8c58a 806 unsigned regno;
7609e720 807 HARD_REG_SET hard_regs_to_save;
a2041967 808 reg_set_iterator rsi;
e384e6b5
BS
809 rtx cheap;
810
811 cheap = find_reg_note (insn, REG_RETURNED, NULL);
812 if (cheap)
813 cheap = XEXP (cheap, 0);
7609e720
BS
814
815 /* Use the register life information in CHAIN to compute which
285f3cf0
R
816 regs are live during the call. */
817 REG_SET_TO_HARD_REG_SET (hard_regs_to_save,
239a0f5b 818 &chain->live_throughout);
f63d1bf7 819 /* Save hard registers always in the widest mode available. */
787dc842
JH
820 for (regno = 0; regno < FIRST_PSEUDO_REGISTER; regno++)
821 if (TEST_HARD_REG_BIT (hard_regs_to_save, regno))
822 save_mode [regno] = regno_save_mode [regno][1];
823 else
824 save_mode [regno] = VOIDmode;
825
f5143c46 826 /* Look through all live pseudos, mark their hard registers
787dc842
JH
827 and choose proper mode for saving. */
828 EXECUTE_IF_SET_IN_REG_SET
a2041967
KH
829 (&chain->live_throughout, FIRST_PSEUDO_REGISTER, regno, rsi)
830 {
831 int r = reg_renumber[regno];
832 int nregs;
833 enum machine_mode mode;
834
e384e6b5 835 if (r < 0 || regno_reg_rtx[regno] == cheap)
058e97ec 836 continue;
a2041967
KH
837 nregs = hard_regno_nregs[r][PSEUDO_REGNO_MODE (regno)];
838 mode = HARD_REGNO_CALLER_SAVE_MODE
839 (r, nregs, PSEUDO_REGNO_MODE (regno));
840 if (GET_MODE_BITSIZE (mode)
841 > GET_MODE_BITSIZE (save_mode[r]))
842 save_mode[r] = mode;
843 while (nregs-- > 0)
844 SET_HARD_REG_BIT (hard_regs_to_save, r + nregs);
845 }
7609e720
BS
846
847 /* Record all registers set in this call insn. These don't need
285f3cf0
R
848 to be saved. N.B. the call insn might set a subreg of a
849 multi-hard-reg pseudo; then the pseudo is considered live
850 during the call, but the subreg that is set isn't. */
7609e720 851 CLEAR_HARD_REG_SET (this_insn_sets);
e30edd6c 852 note_stores (PATTERN (insn), mark_set_regs, &this_insn_sets);
7609e720
BS
853
854 /* Compute which hard regs must be saved before this call. */
855 AND_COMPL_HARD_REG_SET (hard_regs_to_save, call_fixed_reg_set);
856 AND_COMPL_HARD_REG_SET (hard_regs_to_save, this_insn_sets);
857 AND_COMPL_HARD_REG_SET (hard_regs_to_save, hard_regs_saved);
858 AND_HARD_REG_SET (hard_regs_to_save, call_used_reg_set);
859
7609e720
BS
860 for (regno = 0; regno < FIRST_PSEUDO_REGISTER; regno++)
861 if (TEST_HARD_REG_BIT (hard_regs_to_save, regno))
787dc842 862 regno += insert_save (chain, 1, regno, &hard_regs_to_save, save_mode);
7609e720
BS
863
864 /* Must recompute n_regs_saved. */
865 n_regs_saved = 0;
866 for (regno = 0; regno < FIRST_PSEUDO_REGISTER; regno++)
867 if (TEST_HARD_REG_BIT (hard_regs_saved, regno))
868 n_regs_saved++;
e384e6b5
BS
869
870 if (cheap
871 && HARD_REGISTER_P (cheap)
872 && TEST_HARD_REG_BIT (call_used_reg_set, REGNO (cheap)))
873 {
1d2944a3
BS
874 rtx dest, newpat;
875 rtx pat = PATTERN (insn);
876 if (GET_CODE (pat) == PARALLEL)
877 pat = XVECEXP (pat, 0, 0);
878 dest = SET_DEST (pat);
879 newpat = gen_rtx_SET (VOIDmode, cheap, copy_rtx (dest));
880 chain = insert_one_insn (chain, 0, -1, newpat);
e384e6b5 881 }
7609e720 882 }
169e464e 883 last = chain;
7609e720 884 }
b5b8b0ac
AO
885 else if (DEBUG_INSN_P (insn) && n_regs_saved)
886 mark_referenced_regs (&PATTERN (insn),
887 replace_reg_with_saved_mem,
888 save_mode);
c5986054 889
058e97ec 890 if (chain->next == 0 || chain->next->block != chain->block)
7609e720
BS
891 {
892 int regno;
893 /* At the end of the basic block, we must restore any registers that
894 remain saved. If the last insn in the block is a JUMP_INSN, put
895 the restore before the insn, otherwise, put it after the insn. */
896
49b48605
JJ
897 if (n_regs_saved
898 && DEBUG_INSN_P (insn)
899 && last
900 && last->block == chain->block)
169e464e
JJ
901 {
902 rtx ins, prev;
903 basic_block bb = BLOCK_FOR_INSN (insn);
904
905 /* When adding hard reg restores after a DEBUG_INSN, move
906 all notes between last real insn and this DEBUG_INSN after
907 the DEBUG_INSN, otherwise we could get code
908 -g/-g0 differences. */
909 for (ins = PREV_INSN (insn); ins != last->insn; ins = prev)
910 {
911 prev = PREV_INSN (ins);
912 if (NOTE_P (ins))
913 {
914 NEXT_INSN (prev) = NEXT_INSN (ins);
915 PREV_INSN (NEXT_INSN (ins)) = prev;
916 PREV_INSN (ins) = insn;
917 NEXT_INSN (ins) = NEXT_INSN (insn);
918 NEXT_INSN (insn) = ins;
919 if (NEXT_INSN (ins))
920 PREV_INSN (NEXT_INSN (ins)) = ins;
921 if (BB_END (bb) == insn)
922 BB_END (bb) = ins;
923 }
924 else
925 gcc_assert (DEBUG_INSN_P (ins));
926 }
927 }
928 last = NULL;
929
7609e720
BS
930 if (n_regs_saved)
931 for (regno = 0; regno < FIRST_PSEUDO_REGISTER; regno++)
932 if (TEST_HARD_REG_BIT (hard_regs_saved, regno))
4b4bf941 933 regno += insert_restore (chain, JUMP_P (insn),
787dc842 934 regno, MOVE_MAX_WORDS, save_mode);
7609e720 935 }
d329e058 936 }
c5986054
RS
937}
938
e30edd6c
R
939/* Here from note_stores, or directly from save_call_clobbered_regs, when
940 an insn stores a value in a register.
7609e720 941 Set the proper bit or bits in this_insn_sets. All pseudos that have
c5986054
RS
942 been assigned hard regs have had their register number changed already,
943 so we can ignore pseudos. */
c5986054 944static void
7bc980e1 945mark_set_regs (rtx reg, const_rtx setter ATTRIBUTE_UNUSED, void *data)
c5986054 946{
b3694847 947 int regno, endregno, i;
f883e0a7 948 HARD_REG_SET *this_insn_sets = (HARD_REG_SET *) data;
c5986054
RS
949
950 if (GET_CODE (reg) == SUBREG)
951 {
ddef6bc7 952 rtx inner = SUBREG_REG (reg);
f8cfc6aa 953 if (!REG_P (inner) || REGNO (inner) >= FIRST_PSEUDO_REGISTER)
ddef6bc7 954 return;
6f9e3578 955 regno = subreg_regno (reg);
f1f4e530 956 endregno = regno + subreg_nregs (reg);
ddef6bc7 957 }
f8cfc6aa 958 else if (REG_P (reg)
ddef6bc7 959 && REGNO (reg) < FIRST_PSEUDO_REGISTER)
f1f4e530
JM
960 {
961 regno = REGNO (reg);
09e18274 962 endregno = END_HARD_REGNO (reg);
f1f4e530 963 }
ddef6bc7 964 else
c5986054
RS
965 return;
966
c5986054 967 for (i = regno; i < endregno; i++)
e30edd6c 968 SET_HARD_REG_BIT (*this_insn_sets, i);
c5986054
RS
969}
970
285f3cf0
R
971/* Here from note_stores when an insn stores a value in a register.
972 Set the proper bit or bits in the passed regset. All pseudos that have
973 been assigned hard regs have had their register number changed already,
974 so we can ignore pseudos. */
975static void
7bc980e1 976add_stored_regs (rtx reg, const_rtx setter, void *data)
285f3cf0 977{
b3694847 978 int regno, endregno, i;
285f3cf0 979 enum machine_mode mode = GET_MODE (reg);
ddef6bc7 980 int offset = 0;
285f3cf0
R
981
982 if (GET_CODE (setter) == CLOBBER)
983 return;
984
a446b4e8
JM
985 if (GET_CODE (reg) == SUBREG
986 && REG_P (SUBREG_REG (reg))
987 && REGNO (SUBREG_REG (reg)) < FIRST_PSEUDO_REGISTER)
285f3cf0 988 {
ddef6bc7
JJ
989 offset = subreg_regno_offset (REGNO (SUBREG_REG (reg)),
990 GET_MODE (SUBREG_REG (reg)),
991 SUBREG_BYTE (reg),
992 GET_MODE (reg));
a446b4e8 993 regno = REGNO (SUBREG_REG (reg)) + offset;
f1f4e530 994 endregno = regno + subreg_nregs (reg);
285f3cf0 995 }
f1f4e530
JM
996 else
997 {
998 if (!REG_P (reg) || REGNO (reg) >= FIRST_PSEUDO_REGISTER)
999 return;
285f3cf0 1000
f1f4e530 1001 regno = REGNO (reg) + offset;
09e18274 1002 endregno = end_hard_regno (mode, regno);
f1f4e530 1003 }
285f3cf0
R
1004
1005 for (i = regno; i < endregno; i++)
1006 SET_REGNO_REG_SET ((regset) data, i);
1007}
1008
7609e720 1009/* Walk X and record all referenced registers in REFERENCED_REGS. */
c5986054 1010static void
b5b8b0ac 1011mark_referenced_regs (rtx *loc, refmarker_fn *mark, void *arg)
c5986054 1012{
b5b8b0ac 1013 enum rtx_code code = GET_CODE (*loc);
6f7d635c 1014 const char *fmt;
c5986054
RS
1015 int i, j;
1016
7609e720 1017 if (code == SET)
b5b8b0ac 1018 mark_referenced_regs (&SET_SRC (*loc), mark, arg);
7609e720
BS
1019 if (code == SET || code == CLOBBER)
1020 {
b5b8b0ac
AO
1021 loc = &SET_DEST (*loc);
1022 code = GET_CODE (*loc);
1023 if ((code == REG && REGNO (*loc) < FIRST_PSEUDO_REGISTER)
4d450e9a 1024 || code == PC || code == CC0
b5b8b0ac
AO
1025 || (code == SUBREG && REG_P (SUBREG_REG (*loc))
1026 && REGNO (SUBREG_REG (*loc)) < FIRST_PSEUDO_REGISTER
2696f6a4
AO
1027 /* If we're setting only part of a multi-word register,
1028 we shall mark it as referenced, because the words
1029 that are not being set should be restored. */
b5b8b0ac
AO
1030 && ((GET_MODE_SIZE (GET_MODE (*loc))
1031 >= GET_MODE_SIZE (GET_MODE (SUBREG_REG (*loc))))
1032 || (GET_MODE_SIZE (GET_MODE (SUBREG_REG (*loc)))
2696f6a4 1033 <= UNITS_PER_WORD))))
7609e720
BS
1034 return;
1035 }
1036 if (code == MEM || code == SUBREG)
1037 {
b5b8b0ac
AO
1038 loc = &XEXP (*loc, 0);
1039 code = GET_CODE (*loc);
7609e720 1040 }
f95361c8 1041
c5986054
RS
1042 if (code == REG)
1043 {
b5b8b0ac 1044 int regno = REGNO (*loc);
7609e720
BS
1045 int hardregno = (regno < FIRST_PSEUDO_REGISTER ? regno
1046 : reg_renumber[regno]);
c5986054 1047
7609e720 1048 if (hardregno >= 0)
b5b8b0ac
AO
1049 mark (loc, GET_MODE (*loc), hardregno, arg);
1050 else if (arg)
1051 /* ??? Will we ever end up with an equiv expression in a debug
1052 insn, that would have required restoring a reg, or will
1053 reload take care of it for us? */
1054 return;
7609e720
BS
1055 /* If this is a pseudo that did not get a hard register, scan its
1056 memory location, since it might involve the use of another
1057 register, which might be saved. */
f2034d06
JL
1058 else if (reg_equiv_mem (regno) != 0)
1059 mark_referenced_regs (&XEXP (reg_equiv_mem (regno), 0), mark, arg);
1060 else if (reg_equiv_address (regno) != 0)
1061 mark_referenced_regs (&reg_equiv_address (regno), mark, arg);
c5986054
RS
1062 return;
1063 }
7609e720 1064
c5986054
RS
1065 fmt = GET_RTX_FORMAT (code);
1066 for (i = GET_RTX_LENGTH (code) - 1; i >= 0; i--)
1067 {
1068 if (fmt[i] == 'e')
b5b8b0ac 1069 mark_referenced_regs (&XEXP (*loc, i), mark, arg);
c5986054 1070 else if (fmt[i] == 'E')
b5b8b0ac
AO
1071 for (j = XVECLEN (*loc, i) - 1; j >= 0; j--)
1072 mark_referenced_regs (&XVECEXP (*loc, i, j), mark, arg);
1073 }
1074}
1075
1076/* Parameter function for mark_referenced_regs() that adds registers
1077 present in the insn and in equivalent mems and addresses to
1078 referenced_regs. */
1079
1080static void
1081mark_reg_as_referenced (rtx *loc ATTRIBUTE_UNUSED,
1082 enum machine_mode mode,
1083 int hardregno,
1084 void *arg ATTRIBUTE_UNUSED)
1085{
1086 add_to_hard_reg_set (&referenced_regs, mode, hardregno);
1087}
1088
1089/* Parameter function for mark_referenced_regs() that replaces
1090 registers referenced in a debug_insn that would have been restored,
1091 should it be a non-debug_insn, with their save locations. */
1092
1093static void
1094replace_reg_with_saved_mem (rtx *loc,
1095 enum machine_mode mode,
1096 int regno,
1097 void *arg)
1098{
1099 unsigned int i, nregs = hard_regno_nregs [regno][mode];
1100 rtx mem;
1101 enum machine_mode *save_mode = (enum machine_mode *)arg;
1102
1103 for (i = 0; i < nregs; i++)
1104 if (TEST_HARD_REG_BIT (hard_regs_saved, regno + i))
1105 break;
1106
1107 /* If none of the registers in the range would need restoring, we're
1108 all set. */
1109 if (i == nregs)
1110 return;
1111
1112 while (++i < nregs)
1113 if (!TEST_HARD_REG_BIT (hard_regs_saved, regno + i))
1114 break;
1115
1116 if (i == nregs
1117 && regno_save_mem[regno][nregs])
1118 {
1119 mem = copy_rtx (regno_save_mem[regno][nregs]);
1120
1121 if (nregs == (unsigned int) hard_regno_nregs[regno][save_mode[regno]])
1122 mem = adjust_address_nv (mem, save_mode[regno], 0);
1123
1124 if (GET_MODE (mem) != mode)
1125 {
1126 /* This is gen_lowpart_if_possible(), but without validating
1127 the newly-formed address. */
1128 int offset = 0;
1129
1130 if (WORDS_BIG_ENDIAN)
1131 offset = (MAX (GET_MODE_SIZE (GET_MODE (mem)), UNITS_PER_WORD)
1132 - MAX (GET_MODE_SIZE (mode), UNITS_PER_WORD));
1133 if (BYTES_BIG_ENDIAN)
1134 /* Adjust the address so that the address-after-the-data is
1135 unchanged. */
1136 offset -= (MIN (UNITS_PER_WORD, GET_MODE_SIZE (mode))
1137 - MIN (UNITS_PER_WORD, GET_MODE_SIZE (GET_MODE (mem))));
1138
1139 mem = adjust_address_nv (mem, mode, offset);
1140 }
c5986054 1141 }
b5b8b0ac
AO
1142 else
1143 {
1144 mem = gen_rtx_CONCATN (mode, rtvec_alloc (nregs));
1145 for (i = 0; i < nregs; i++)
1146 if (TEST_HARD_REG_BIT (hard_regs_saved, regno + i))
1147 {
1148 gcc_assert (regno_save_mem[regno + i][1]);
1149 XVECEXP (mem, 0, i) = copy_rtx (regno_save_mem[regno + i][1]);
1150 }
1151 else
1152 {
1153 gcc_assert (save_mode[regno] != VOIDmode);
1154 XVECEXP (mem, 0, i) = gen_rtx_REG (save_mode [regno],
1155 regno + i);
1156 }
1157 }
1158
1159 gcc_assert (GET_MODE (mem) == mode);
1160 *loc = mem;
c5986054 1161}
b5b8b0ac 1162
c5986054 1163\f
7609e720
BS
1164/* Insert a sequence of insns to restore. Place these insns in front of
1165 CHAIN if BEFORE_P is nonzero, behind the insn otherwise. MAXRESTORE is
1166 the maximum number of registers which should be restored during this call.
1167 It should never be less than 1 since we only work with entire registers.
c5986054
RS
1168
1169 Note that we have verified in init_caller_save that we can do this
1170 with a simple SET, so use it. Set INSN_CODE to what we save there
1171 since the address might not be valid so the insn might not be recognized.
1172 These insns will be reloaded and have register elimination done by
f95361c8 1173 find_reload, so we need not worry about that here.
c5986054 1174
f95361c8
JL
1175 Return the extra number of registers saved. */
1176
1177static int
d329e058
AJ
1178insert_restore (struct insn_chain *chain, int before_p, int regno,
1179 int maxrestore, enum machine_mode *save_mode)
c5986054 1180{
285f3cf0 1181 int i, k;
aefdd5ab 1182 rtx pat = NULL_RTX;
830a47ec 1183 int code;
787dc842 1184 unsigned int numregs = 0;
32e9fa48 1185 struct insn_chain *new_chain;
787dc842 1186 rtx mem;
c5986054 1187
41806d92
NS
1188 /* A common failure mode if register status is not correct in the
1189 RTL is for this routine to be called with a REGNO we didn't
1190 expect to save. That will cause us to write an insn with a (nil)
1191 SET_DEST or SET_SRC. Instead of doing so and causing a crash
1192 later, check for this common case here instead. This will remove
1193 one step in debugging such problems. */
366de0ce 1194 gcc_assert (regno_save_mem[regno][1]);
09835ed2 1195
7609e720 1196 /* Get the pattern to emit and update our status.
4554e20d 1197
7609e720 1198 See if we can restore `maxrestore' registers at once. Work
4554e20d
JL
1199 backwards to the single register case. */
1200 for (i = maxrestore; i > 0; i--)
c5986054 1201 {
285f3cf0 1202 int j;
7609e720
BS
1203 int ok = 1;
1204
1205 if (regno_save_mem[regno][i] == 0)
4554e20d
JL
1206 continue;
1207
7609e720
BS
1208 for (j = 0; j < i; j++)
1209 if (! TEST_HARD_REG_BIT (hard_regs_saved, regno + j))
1210 {
1211 ok = 0;
1212 break;
1213 }
f9da5064 1214 /* Must do this one restore at a time. */
4554e20d
JL
1215 if (! ok)
1216 continue;
7609e720 1217
4554e20d
JL
1218 numregs = i;
1219 break;
1220 }
f95361c8 1221
787dc842
JH
1222 mem = regno_save_mem [regno][numregs];
1223 if (save_mode [regno] != VOIDmode
1224 && save_mode [regno] != GET_MODE (mem)
c3c96e68
L
1225 && numregs == (unsigned int) hard_regno_nregs[regno][save_mode [regno]]
1226 /* Check that insn to restore REGNO in save_mode[regno] is
1227 correct. */
1228 && reg_save_code (regno, save_mode[regno]) >= 0)
8d2547f2 1229 mem = adjust_address_nv (mem, save_mode[regno], 0);
ce7b36a4
JW
1230 else
1231 mem = copy_rtx (mem);
76fe54f0
L
1232
1233 /* Verify that the alignment of spill space is equal to or greater
1234 than required. */
058e97ec
VM
1235 gcc_assert (MIN (MAX_SUPPORTED_STACK_ALIGNMENT,
1236 GET_MODE_ALIGNMENT (GET_MODE (mem))) <= MEM_ALIGN (mem));
76fe54f0 1237
285f3cf0 1238 pat = gen_rtx_SET (VOIDmode,
d329e058 1239 gen_rtx_REG (GET_MODE (mem),
787dc842 1240 regno), mem);
78187f5a 1241 code = reg_restore_code (regno, GET_MODE (mem));
32e9fa48 1242 new_chain = insert_one_insn (chain, before_p, code, pat);
285f3cf0
R
1243
1244 /* Clear status for all registers we restored. */
1245 for (k = 0; k < i; k++)
1246 {
1247 CLEAR_HARD_REG_BIT (hard_regs_saved, regno + k);
32e9fa48 1248 SET_REGNO_REG_SET (&new_chain->dead_or_set, regno + k);
285f3cf0
R
1249 n_regs_saved--;
1250 }
1251
6614fd40 1252 /* Tell our callers how many extra registers we saved/restored. */
4554e20d
JL
1253 return numregs - 1;
1254}
f95361c8 1255
7609e720 1256/* Like insert_restore above, but save registers instead. */
2cc2d4bb 1257
4554e20d 1258static int
d329e058
AJ
1259insert_save (struct insn_chain *chain, int before_p, int regno,
1260 HARD_REG_SET (*to_save), enum machine_mode *save_mode)
4554e20d 1261{
787dc842
JH
1262 int i;
1263 unsigned int k;
4554e20d 1264 rtx pat = NULL_RTX;
830a47ec 1265 int code;
787dc842 1266 unsigned int numregs = 0;
32e9fa48 1267 struct insn_chain *new_chain;
787dc842 1268 rtx mem;
f95361c8 1269
41806d92
NS
1270 /* A common failure mode if register status is not correct in the
1271 RTL is for this routine to be called with a REGNO we didn't
1272 expect to save. That will cause us to write an insn with a (nil)
1273 SET_DEST or SET_SRC. Instead of doing so and causing a crash
1274 later, check for this common case here. This will remove one
4554e20d 1275 step in debugging such problems. */
366de0ce 1276 gcc_assert (regno_save_mem[regno][1]);
c5986054 1277
7609e720 1278 /* Get the pattern to emit and update our status.
f95361c8 1279
d329e058 1280 See if we can save several registers with a single instruction.
4554e20d 1281 Work backwards to the single register case. */
7609e720 1282 for (i = MOVE_MAX_WORDS; i > 0; i--)
4554e20d 1283 {
285f3cf0 1284 int j;
7609e720
BS
1285 int ok = 1;
1286 if (regno_save_mem[regno][i] == 0)
4554e20d 1287 continue;
f95361c8 1288
7609e720
BS
1289 for (j = 0; j < i; j++)
1290 if (! TEST_HARD_REG_BIT (*to_save, regno + j))
1291 {
1292 ok = 0;
1293 break;
1294 }
f9da5064 1295 /* Must do this one save at a time. */
4554e20d
JL
1296 if (! ok)
1297 continue;
1298
4554e20d
JL
1299 numregs = i;
1300 break;
f95361c8 1301 }
c5986054 1302
787dc842
JH
1303 mem = regno_save_mem [regno][numregs];
1304 if (save_mode [regno] != VOIDmode
1305 && save_mode [regno] != GET_MODE (mem)
c3c96e68
L
1306 && numregs == (unsigned int) hard_regno_nregs[regno][save_mode [regno]]
1307 /* Check that insn to save REGNO in save_mode[regno] is
1308 correct. */
1309 && reg_save_code (regno, save_mode[regno]) >= 0)
8d2547f2 1310 mem = adjust_address_nv (mem, save_mode[regno], 0);
ce7b36a4
JW
1311 else
1312 mem = copy_rtx (mem);
76fe54f0
L
1313
1314 /* Verify that the alignment of spill space is equal to or greater
1315 than required. */
058e97ec
VM
1316 gcc_assert (MIN (MAX_SUPPORTED_STACK_ALIGNMENT,
1317 GET_MODE_ALIGNMENT (GET_MODE (mem))) <= MEM_ALIGN (mem));
76fe54f0 1318
787dc842
JH
1319 pat = gen_rtx_SET (VOIDmode, mem,
1320 gen_rtx_REG (GET_MODE (mem),
285f3cf0 1321 regno));
78187f5a 1322 code = reg_save_code (regno, GET_MODE (mem));
32e9fa48 1323 new_chain = insert_one_insn (chain, before_p, code, pat);
285f3cf0
R
1324
1325 /* Set hard_regs_saved and dead_or_set for all the registers we saved. */
1326 for (k = 0; k < numregs; k++)
1327 {
1328 SET_HARD_REG_BIT (hard_regs_saved, regno + k);
32e9fa48 1329 SET_REGNO_REG_SET (&new_chain->dead_or_set, regno + k);
285f3cf0
R
1330 n_regs_saved++;
1331 }
f95361c8 1332
6614fd40 1333 /* Tell our callers how many extra registers we saved/restored. */
f95361c8 1334 return numregs - 1;
c5986054 1335}
4554e20d 1336
574af2d6
RS
1337/* A for_each_rtx callback used by add_used_regs. Add the hard-register
1338 equivalent of each REG to regset DATA. */
1339
1340static int
1341add_used_regs_1 (rtx *loc, void *data)
1342{
f773c2bd 1343 unsigned int regno;
574af2d6
RS
1344 regset live;
1345 rtx x;
1346
1347 x = *loc;
1348 live = (regset) data;
1349 if (REG_P (x))
1350 {
1351 regno = REGNO (x);
f773c2bd
AS
1352 if (HARD_REGISTER_NUM_P (regno))
1353 bitmap_set_range (live, regno, hard_regno_nregs[regno][GET_MODE (x)]);
1354 else
574af2d6 1355 regno = reg_renumber[regno];
574af2d6
RS
1356 }
1357 return 0;
1358}
1359
1360/* A note_uses callback used by insert_one_insn. Add the hard-register
1361 equivalent of each REG to regset DATA. */
1362
1363static void
1364add_used_regs (rtx *loc, void *data)
1365{
1366 for_each_rtx (loc, add_used_regs_1, data);
1367}
1368
7609e720 1369/* Emit a new caller-save insn and set the code. */
285f3cf0 1370static struct insn_chain *
d329e058 1371insert_one_insn (struct insn_chain *chain, int before_p, int code, rtx pat)
4554e20d 1372{
7609e720 1373 rtx insn = chain->insn;
32e9fa48 1374 struct insn_chain *new_chain;
d329e058 1375
4554e20d
JL
1376#ifdef HAVE_cc0
1377 /* If INSN references CC0, put our insns in front of the insn that sets
1378 CC0. This is always safe, since the only way we could be passed an
1379 insn that references CC0 is for a restore, and doing a restore earlier
1380 isn't a problem. We do, however, assume here that CALL_INSNs don't
1381 reference CC0. Guard against non-INSN's like CODE_LABEL. */
1382
4b4bf941 1383 if ((NONJUMP_INSN_P (insn) || JUMP_P (insn))
4554e20d
JL
1384 && before_p
1385 && reg_referenced_p (cc0_rtx, PATTERN (insn)))
7609e720 1386 chain = chain->prev, insn = chain->insn;
4554e20d
JL
1387#endif
1388
32e9fa48 1389 new_chain = new_insn_chain ();
4554e20d
JL
1390 if (before_p)
1391 {
285f3cf0
R
1392 rtx link;
1393
32e9fa48
KG
1394 new_chain->prev = chain->prev;
1395 if (new_chain->prev != 0)
1396 new_chain->prev->next = new_chain;
7609e720 1397 else
32e9fa48 1398 reload_insn_chain = new_chain;
7609e720 1399
32e9fa48
KG
1400 chain->prev = new_chain;
1401 new_chain->next = chain;
1402 new_chain->insn = emit_insn_before (pat, insn);
92691d7d
JL
1403 /* ??? It would be nice if we could exclude the already / still saved
1404 registers from the live sets. */
32e9fa48 1405 COPY_REG_SET (&new_chain->live_throughout, &chain->live_throughout);
574af2d6
RS
1406 note_uses (&PATTERN (chain->insn), add_used_regs,
1407 &new_chain->live_throughout);
ac8bc93e
PB
1408 /* If CHAIN->INSN is a call, then the registers which contain
1409 the arguments to the function are live in the new insn. */
1410 if (CALL_P (chain->insn))
574af2d6
RS
1411 for (link = CALL_INSN_FUNCTION_USAGE (chain->insn);
1412 link != NULL_RTX;
1413 link = XEXP (link, 1))
1414 note_uses (&XEXP (link, 0), add_used_regs,
1415 &new_chain->live_throughout);
ac8bc93e 1416
32e9fa48 1417 CLEAR_REG_SET (&new_chain->dead_or_set);
a813c111 1418 if (chain->insn == BB_HEAD (BASIC_BLOCK (chain->block)))
32e9fa48 1419 BB_HEAD (BASIC_BLOCK (chain->block)) = new_chain->insn;
4554e20d
JL
1420 }
1421 else
1422 {
32e9fa48
KG
1423 new_chain->next = chain->next;
1424 if (new_chain->next != 0)
1425 new_chain->next->prev = new_chain;
1426 chain->next = new_chain;
1427 new_chain->prev = chain;
1428 new_chain->insn = emit_insn_after (pat, insn);
92691d7d
JL
1429 /* ??? It would be nice if we could exclude the already / still saved
1430 registers from the live sets, and observe REG_UNUSED notes. */
32e9fa48 1431 COPY_REG_SET (&new_chain->live_throughout, &chain->live_throughout);
285f3cf0 1432 /* Registers that are set in CHAIN->INSN live in the new insn.
c22cacf3 1433 (Unless there is a REG_UNUSED note for them, but we don't
285f3cf0
R
1434 look for them here.) */
1435 note_stores (PATTERN (chain->insn), add_stored_regs,
32e9fa48
KG
1436 &new_chain->live_throughout);
1437 CLEAR_REG_SET (&new_chain->dead_or_set);
a813c111 1438 if (chain->insn == BB_END (BASIC_BLOCK (chain->block)))
32e9fa48 1439 BB_END (BASIC_BLOCK (chain->block)) = new_chain->insn;
4554e20d 1440 }
32e9fa48
KG
1441 new_chain->block = chain->block;
1442 new_chain->is_caller_save_insn = 1;
437a710d 1443
32e9fa48
KG
1444 INSN_CODE (new_chain->insn) = code;
1445 return new_chain;
4554e20d 1446}
78187f5a 1447#include "gt-caller-save.h"