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