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