1 /* Subroutines used to generate function prologues and epilogues
3 Copyright (C) 1991-2019 Free Software Foundation, Inc.
5 This file is part of GCC.
7 GCC is free software; you can redistribute it and/or modify it
8 under the terms of the GNU General Public License as published
9 by the Free Software Foundation; either version 3, or (at your
10 option) any later version.
12 GCC is distributed in the hope that it will be useful, but WITHOUT
13 ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
14 or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public
15 License for more details.
17 You should have received a copy of the GNU General Public License
18 along with GCC; see the file COPYING3. If not see
19 <http://www.gnu.org/licenses/>. */
21 #define IN_TARGET_CODE 1
25 #include "coretypes.h"
33 #include "print-tree.h"
38 #include "tree-pass.h"
39 #include "rtx-vector-builder.h"
42 #include "stringpool.h"
45 #include "langhooks.h"
47 #include "diagnostic-core.h"
50 #include "rs6000-internal.h"
52 static int rs6000_ra_ever_killed (void);
53 static void is_altivec_return_reg (rtx, void *);
54 static bool rs6000_save_toc_in_prologue_p (void);
56 static rs6000_stack_t stack_info;
59 /* Label number of label created for -mrelocatable, to call to so we can
60 get the address of the GOT section */
61 int rs6000_pic_labelno = 0;
64 /* Function to init struct machine_function.
65 This will be called, via a pointer variable,
66 from push_function_context. */
68 struct machine_function *
69 rs6000_init_machine_status (void)
71 stack_info.reload_completed = 0;
72 return ggc_cleared_alloc<machine_function> ();
75 /* This page contains routines that are used to determine what the
76 function prologue and epilogue code will do and write them out. */
78 /* Determine whether the REG is really used. */
83 if (reg == RS6000_PIC_OFFSET_TABLE_REGNUM && !TARGET_SINGLE_PIC_BASE)
85 /* When calling eh_return, we must return true for all the cases
86 where conditional_register_usage marks the PIC offset reg
87 call used or fixed. */
88 if (crtl->calls_eh_return
89 && ((DEFAULT_ABI == ABI_V4 && flag_pic)
90 || (DEFAULT_ABI == ABI_DARWIN && flag_pic)
91 || (TARGET_TOC && TARGET_MINIMAL_TOC)))
94 /* We need to mark the PIC offset register live for the same
95 conditions as it is set up in rs6000_emit_prologue, or
96 otherwise it won't be saved before we clobber it. */
97 if (TARGET_TOC && TARGET_MINIMAL_TOC
98 && !constant_pool_empty_p ())
101 if (DEFAULT_ABI == ABI_V4
102 && (flag_pic == 1 || (flag_pic && TARGET_SECURE_PLT))
103 && df_regs_ever_live_p (RS6000_PIC_OFFSET_TABLE_REGNUM))
106 if (DEFAULT_ABI == ABI_DARWIN
107 && flag_pic && crtl->uses_pic_offset_table)
111 return !call_used_regs[reg] && df_regs_ever_live_p (reg);
114 /* Return the first fixed-point register that is required to be
115 saved. 32 if none. */
118 first_reg_to_save (void)
122 /* Find lowest numbered live register. */
123 for (first_reg = 13; first_reg <= 31; first_reg++)
124 if (save_reg_p (first_reg))
130 /* Similar, for FP regs. */
133 first_fp_reg_to_save (void)
137 /* Find lowest numbered live register. */
138 for (first_reg = 14 + 32; first_reg <= 63; first_reg++)
139 if (save_reg_p (first_reg))
145 /* Similar, for AltiVec regs. */
148 first_altivec_reg_to_save (void)
152 /* Stack frame remains as is unless we are in AltiVec ABI. */
153 if (! TARGET_ALTIVEC_ABI)
154 return LAST_ALTIVEC_REGNO + 1;
156 /* On Darwin, the unwind routines are compiled without
157 TARGET_ALTIVEC, and use save_world to save/restore the
158 altivec registers when necessary. */
159 if (DEFAULT_ABI == ABI_DARWIN && crtl->calls_eh_return
161 return FIRST_ALTIVEC_REGNO + 20;
163 /* Find lowest numbered live register. */
164 for (i = FIRST_ALTIVEC_REGNO + 20; i <= LAST_ALTIVEC_REGNO; ++i)
171 /* Return a 32-bit mask of the AltiVec registers we need to set in
172 VRSAVE. Bit n of the return value is 1 if Vn is live. The MSB in
173 the 32-bit word is 0. */
176 compute_vrsave_mask (void)
178 unsigned int i, mask = 0;
180 /* On Darwin, the unwind routines are compiled without
181 TARGET_ALTIVEC, and use save_world to save/restore the
182 call-saved altivec registers when necessary. */
183 if (DEFAULT_ABI == ABI_DARWIN && crtl->calls_eh_return
187 /* First, find out if we use _any_ altivec registers. */
188 for (i = FIRST_ALTIVEC_REGNO; i <= LAST_ALTIVEC_REGNO; ++i)
189 if (df_regs_ever_live_p (i))
190 mask |= ALTIVEC_REG_BIT (i);
195 /* Next, remove the argument registers from the set. These must
196 be in the VRSAVE mask set by the caller, so we don't need to add
197 them in again. More importantly, the mask we compute here is
198 used to generate CLOBBERs in the set_vrsave insn, and we do not
199 wish the argument registers to die. */
200 for (i = ALTIVEC_ARG_MIN_REG; i < (unsigned) crtl->args.info.vregno; i++)
201 mask &= ~ALTIVEC_REG_BIT (i);
203 /* Similarly, remove the return value from the set. */
206 diddle_return_value (is_altivec_return_reg, &yes);
208 mask &= ~ALTIVEC_REG_BIT (ALTIVEC_ARG_RETURN);
214 /* For a very restricted set of circumstances, we can cut down the
215 size of prologues/epilogues by calling our own save/restore-the-world
219 compute_save_world_info (rs6000_stack_t *info)
221 info->world_save_p = 1;
223 = (WORLD_SAVE_P (info)
224 && DEFAULT_ABI == ABI_DARWIN
225 && !cfun->has_nonlocal_label
226 && info->first_fp_reg_save == FIRST_SAVED_FP_REGNO
227 && info->first_gp_reg_save == FIRST_SAVED_GP_REGNO
228 && info->first_altivec_reg_save == FIRST_SAVED_ALTIVEC_REGNO
231 /* This will not work in conjunction with sibcalls. Make sure there
232 are none. (This check is expensive, but seldom executed.) */
233 if (WORLD_SAVE_P (info))
236 for (insn = get_last_insn_anywhere (); insn; insn = PREV_INSN (insn))
237 if (CALL_P (insn) && SIBLING_CALL_P (insn))
239 info->world_save_p = 0;
244 if (WORLD_SAVE_P (info))
246 /* Even if we're not touching VRsave, make sure there's room on the
247 stack for it, if it looks like we're calling SAVE_WORLD, which
248 will attempt to save it. */
249 info->vrsave_size = 4;
251 /* If we are going to save the world, we need to save the link register too. */
254 /* "Save" the VRsave register too if we're saving the world. */
255 if (info->vrsave_mask == 0)
256 info->vrsave_mask = compute_vrsave_mask ();
258 /* Because the Darwin register save/restore routines only handle
259 F14 .. F31 and V20 .. V31 as per the ABI, perform a consistency
261 gcc_assert (info->first_fp_reg_save >= FIRST_SAVED_FP_REGNO
262 && (info->first_altivec_reg_save
263 >= FIRST_SAVED_ALTIVEC_REGNO));
271 is_altivec_return_reg (rtx reg, void *xyes)
273 bool *yes = (bool *) xyes;
274 if (REGNO (reg) == ALTIVEC_ARG_RETURN)
279 /* Return whether REG is a global user reg or has been specifed by
280 -ffixed-REG. We should not restore these, and so cannot use
281 lmw or out-of-line restore functions if there are any. We also
282 can't save them (well, emit frame notes for them), because frame
283 unwinding during exception handling will restore saved registers. */
286 fixed_reg_p (int reg)
288 /* Ignore fixed_regs[RS6000_PIC_OFFSET_TABLE_REGNUM] when the
289 backend sets it, overriding anything the user might have given. */
290 if (reg == RS6000_PIC_OFFSET_TABLE_REGNUM
291 && ((DEFAULT_ABI == ABI_V4 && flag_pic)
292 || (DEFAULT_ABI == ABI_DARWIN && flag_pic)
293 || (TARGET_TOC && TARGET_MINIMAL_TOC)))
296 return fixed_regs[reg];
299 /* Determine the strategy for savings/restoring registers. */
303 SAVE_INLINE_GPRS = 0x2,
304 SAVE_INLINE_FPRS = 0x4,
305 SAVE_NOINLINE_GPRS_SAVES_LR = 0x8,
306 SAVE_NOINLINE_FPRS_SAVES_LR = 0x10,
307 SAVE_INLINE_VRS = 0x20,
308 REST_MULTIPLE = 0x100,
309 REST_INLINE_GPRS = 0x200,
310 REST_INLINE_FPRS = 0x400,
311 REST_NOINLINE_FPRS_DOESNT_RESTORE_LR = 0x800,
312 REST_INLINE_VRS = 0x1000
316 rs6000_savres_strategy (rs6000_stack_t *info,
317 bool using_static_chain_p)
321 /* Select between in-line and out-of-line save and restore of regs.
322 First, all the obvious cases where we don't use out-of-line. */
323 if (crtl->calls_eh_return
324 || cfun->machine->ra_need_lr)
325 strategy |= (SAVE_INLINE_FPRS | REST_INLINE_FPRS
326 | SAVE_INLINE_GPRS | REST_INLINE_GPRS
327 | SAVE_INLINE_VRS | REST_INLINE_VRS);
329 if (info->first_gp_reg_save == 32)
330 strategy |= SAVE_INLINE_GPRS | REST_INLINE_GPRS;
332 if (info->first_fp_reg_save == 64)
333 strategy |= SAVE_INLINE_FPRS | REST_INLINE_FPRS;
335 if (info->first_altivec_reg_save == LAST_ALTIVEC_REGNO + 1)
336 strategy |= SAVE_INLINE_VRS | REST_INLINE_VRS;
338 /* Define cutoff for using out-of-line functions to save registers. */
339 if (DEFAULT_ABI == ABI_V4 || TARGET_ELF)
343 strategy |= SAVE_INLINE_FPRS | REST_INLINE_FPRS;
344 strategy |= SAVE_INLINE_GPRS | REST_INLINE_GPRS;
345 strategy |= SAVE_INLINE_VRS | REST_INLINE_VRS;
349 /* Prefer out-of-line restore if it will exit. */
350 if (info->first_fp_reg_save > 61)
351 strategy |= SAVE_INLINE_FPRS;
352 if (info->first_gp_reg_save > 29)
354 if (info->first_fp_reg_save == 64)
355 strategy |= SAVE_INLINE_GPRS;
357 strategy |= SAVE_INLINE_GPRS | REST_INLINE_GPRS;
359 if (info->first_altivec_reg_save == LAST_ALTIVEC_REGNO)
360 strategy |= SAVE_INLINE_VRS | REST_INLINE_VRS;
363 else if (DEFAULT_ABI == ABI_DARWIN)
365 if (info->first_fp_reg_save > 60)
366 strategy |= SAVE_INLINE_FPRS | REST_INLINE_FPRS;
367 if (info->first_gp_reg_save > 29)
368 strategy |= SAVE_INLINE_GPRS | REST_INLINE_GPRS;
369 strategy |= SAVE_INLINE_VRS | REST_INLINE_VRS;
373 gcc_checking_assert (DEFAULT_ABI == ABI_AIX || DEFAULT_ABI == ABI_ELFv2);
374 if ((flag_shrink_wrap_separate && optimize_function_for_speed_p (cfun))
375 || info->first_fp_reg_save > 61)
376 strategy |= SAVE_INLINE_FPRS | REST_INLINE_FPRS;
377 strategy |= SAVE_INLINE_GPRS | REST_INLINE_GPRS;
378 strategy |= SAVE_INLINE_VRS | REST_INLINE_VRS;
381 /* Don't bother to try to save things out-of-line if r11 is occupied
382 by the static chain. It would require too much fiddling and the
383 static chain is rarely used anyway. FPRs are saved w.r.t the stack
384 pointer on Darwin, and AIX uses r1 or r12. */
385 if (using_static_chain_p
386 && (DEFAULT_ABI == ABI_V4 || DEFAULT_ABI == ABI_DARWIN))
387 strategy |= ((DEFAULT_ABI == ABI_DARWIN ? 0 : SAVE_INLINE_FPRS)
391 /* Don't ever restore fixed regs. That means we can't use the
392 out-of-line register restore functions if a fixed reg is in the
393 range of regs restored. */
394 if (!(strategy & REST_INLINE_FPRS))
395 for (int i = info->first_fp_reg_save; i < 64; i++)
398 strategy |= REST_INLINE_FPRS;
402 /* We can only use the out-of-line routines to restore fprs if we've
403 saved all the registers from first_fp_reg_save in the prologue.
404 Otherwise, we risk loading garbage. Of course, if we have saved
405 out-of-line then we know we haven't skipped any fprs. */
406 if ((strategy & SAVE_INLINE_FPRS)
407 && !(strategy & REST_INLINE_FPRS))
408 for (int i = info->first_fp_reg_save; i < 64; i++)
411 strategy |= REST_INLINE_FPRS;
415 /* Similarly, for altivec regs. */
416 if (!(strategy & REST_INLINE_VRS))
417 for (int i = info->first_altivec_reg_save; i < LAST_ALTIVEC_REGNO + 1; i++)
420 strategy |= REST_INLINE_VRS;
424 if ((strategy & SAVE_INLINE_VRS)
425 && !(strategy & REST_INLINE_VRS))
426 for (int i = info->first_altivec_reg_save; i < LAST_ALTIVEC_REGNO + 1; i++)
429 strategy |= REST_INLINE_VRS;
433 /* info->lr_save_p isn't yet set if the only reason lr needs to be
434 saved is an out-of-line save or restore. Set up the value for
435 the next test (excluding out-of-line gprs). */
436 bool lr_save_p = (info->lr_save_p
437 || !(strategy & SAVE_INLINE_FPRS)
438 || !(strategy & SAVE_INLINE_VRS)
439 || !(strategy & REST_INLINE_FPRS)
440 || !(strategy & REST_INLINE_VRS));
444 && info->first_gp_reg_save < 31
445 && !(flag_shrink_wrap
446 && flag_shrink_wrap_separate
447 && optimize_function_for_speed_p (cfun)))
450 for (int i = info->first_gp_reg_save; i < 32; i++)
455 /* Don't use store multiple if only one reg needs to be
456 saved. This can occur for example when the ABI_V4 pic reg
457 (r30) needs to be saved to make calls, but r31 is not
459 strategy |= SAVE_INLINE_GPRS | REST_INLINE_GPRS;
462 /* Prefer store multiple for saves over out-of-line
463 routines, since the store-multiple instruction will
464 always be smaller. */
465 strategy |= SAVE_INLINE_GPRS | SAVE_MULTIPLE;
467 /* The situation is more complicated with load multiple.
468 We'd prefer to use the out-of-line routines for restores,
469 since the "exit" out-of-line routines can handle the
470 restore of LR and the frame teardown. However if doesn't
471 make sense to use the out-of-line routine if that is the
472 only reason we'd need to save LR, and we can't use the
473 "exit" out-of-line gpr restore if we have saved some
474 fprs; In those cases it is advantageous to use load
475 multiple when available. */
476 if (info->first_fp_reg_save != 64 || !lr_save_p)
477 strategy |= REST_INLINE_GPRS | REST_MULTIPLE;
481 /* Using the "exit" out-of-line routine does not improve code size
482 if using it would require lr to be saved and if only saving one
484 else if (!lr_save_p && info->first_gp_reg_save > 29)
485 strategy |= SAVE_INLINE_GPRS | REST_INLINE_GPRS;
487 /* Don't ever restore fixed regs. */
488 if ((strategy & (REST_INLINE_GPRS | REST_MULTIPLE)) != REST_INLINE_GPRS)
489 for (int i = info->first_gp_reg_save; i < 32; i++)
492 strategy |= REST_INLINE_GPRS;
493 strategy &= ~REST_MULTIPLE;
497 /* We can only use load multiple or the out-of-line routines to
498 restore gprs if we've saved all the registers from
499 first_gp_reg_save. Otherwise, we risk loading garbage.
500 Of course, if we have saved out-of-line or used stmw then we know
501 we haven't skipped any gprs. */
502 if ((strategy & (SAVE_INLINE_GPRS | SAVE_MULTIPLE)) == SAVE_INLINE_GPRS
503 && (strategy & (REST_INLINE_GPRS | REST_MULTIPLE)) != REST_INLINE_GPRS)
504 for (int i = info->first_gp_reg_save; i < 32; i++)
507 strategy |= REST_INLINE_GPRS;
508 strategy &= ~REST_MULTIPLE;
512 if (TARGET_ELF && TARGET_64BIT)
514 if (!(strategy & SAVE_INLINE_FPRS))
515 strategy |= SAVE_NOINLINE_FPRS_SAVES_LR;
516 else if (!(strategy & SAVE_INLINE_GPRS)
517 && info->first_fp_reg_save == 64)
518 strategy |= SAVE_NOINLINE_GPRS_SAVES_LR;
520 else if (TARGET_AIX && !(strategy & REST_INLINE_FPRS))
521 strategy |= REST_NOINLINE_FPRS_DOESNT_RESTORE_LR;
523 if (TARGET_MACHO && !(strategy & SAVE_INLINE_FPRS))
524 strategy |= SAVE_NOINLINE_FPRS_SAVES_LR;
529 /* Calculate the stack information for the current function. This is
530 complicated by having two separate calling sequences, the AIX calling
531 sequence and the V.4 calling sequence.
533 AIX (and Darwin/Mac OS X) stack frames look like:
535 SP----> +---------------------------------------+
536 | back chain to caller | 0 0
537 +---------------------------------------+
538 | saved CR | 4 8 (8-11)
539 +---------------------------------------+
541 +---------------------------------------+
542 | reserved for compilers | 12 24
543 +---------------------------------------+
544 | reserved for binders | 16 32
545 +---------------------------------------+
546 | saved TOC pointer | 20 40
547 +---------------------------------------+
548 | Parameter save area (+padding*) (P) | 24 48
549 +---------------------------------------+
550 | Alloca space (A) | 24+P etc.
551 +---------------------------------------+
552 | Local variable space (L) | 24+P+A
553 +---------------------------------------+
554 | Float/int conversion temporary (X) | 24+P+A+L
555 +---------------------------------------+
556 | Save area for AltiVec registers (W) | 24+P+A+L+X
557 +---------------------------------------+
558 | AltiVec alignment padding (Y) | 24+P+A+L+X+W
559 +---------------------------------------+
560 | Save area for VRSAVE register (Z) | 24+P+A+L+X+W+Y
561 +---------------------------------------+
562 | Save area for GP registers (G) | 24+P+A+X+L+X+W+Y+Z
563 +---------------------------------------+
564 | Save area for FP registers (F) | 24+P+A+X+L+X+W+Y+Z+G
565 +---------------------------------------+
566 old SP->| back chain to caller's caller |
567 +---------------------------------------+
569 * If the alloca area is present, the parameter save area is
570 padded so that the former starts 16-byte aligned.
572 The required alignment for AIX configurations is two words (i.e., 8
575 The ELFv2 ABI is a variant of the AIX ABI. Stack frames look like:
577 SP----> +---------------------------------------+
578 | Back chain to caller | 0
579 +---------------------------------------+
580 | Save area for CR | 8
581 +---------------------------------------+
583 +---------------------------------------+
584 | Saved TOC pointer | 24
585 +---------------------------------------+
586 | Parameter save area (+padding*) (P) | 32
587 +---------------------------------------+
588 | Alloca space (A) | 32+P
589 +---------------------------------------+
590 | Local variable space (L) | 32+P+A
591 +---------------------------------------+
592 | Save area for AltiVec registers (W) | 32+P+A+L
593 +---------------------------------------+
594 | AltiVec alignment padding (Y) | 32+P+A+L+W
595 +---------------------------------------+
596 | Save area for GP registers (G) | 32+P+A+L+W+Y
597 +---------------------------------------+
598 | Save area for FP registers (F) | 32+P+A+L+W+Y+G
599 +---------------------------------------+
600 old SP->| back chain to caller's caller | 32+P+A+L+W+Y+G+F
601 +---------------------------------------+
603 * If the alloca area is present, the parameter save area is
604 padded so that the former starts 16-byte aligned.
606 V.4 stack frames look like:
608 SP----> +---------------------------------------+
609 | back chain to caller | 0
610 +---------------------------------------+
611 | caller's saved LR | 4
612 +---------------------------------------+
613 | Parameter save area (+padding*) (P) | 8
614 +---------------------------------------+
615 | Alloca space (A) | 8+P
616 +---------------------------------------+
617 | Varargs save area (V) | 8+P+A
618 +---------------------------------------+
619 | Local variable space (L) | 8+P+A+V
620 +---------------------------------------+
621 | Float/int conversion temporary (X) | 8+P+A+V+L
622 +---------------------------------------+
623 | Save area for AltiVec registers (W) | 8+P+A+V+L+X
624 +---------------------------------------+
625 | AltiVec alignment padding (Y) | 8+P+A+V+L+X+W
626 +---------------------------------------+
627 | Save area for VRSAVE register (Z) | 8+P+A+V+L+X+W+Y
628 +---------------------------------------+
629 | saved CR (C) | 8+P+A+V+L+X+W+Y+Z
630 +---------------------------------------+
631 | Save area for GP registers (G) | 8+P+A+V+L+X+W+Y+Z+C
632 +---------------------------------------+
633 | Save area for FP registers (F) | 8+P+A+V+L+X+W+Y+Z+C+G
634 +---------------------------------------+
635 old SP->| back chain to caller's caller |
636 +---------------------------------------+
638 * If the alloca area is present and the required alignment is
639 16 bytes, the parameter save area is padded so that the
640 alloca area starts 16-byte aligned.
642 The required alignment for V.4 is 16 bytes, or 8 bytes if -meabi is
643 given. (But note below and in sysv4.h that we require only 8 and
644 may round up the size of our stack frame anyways. The historical
645 reason is early versions of powerpc-linux which didn't properly
646 align the stack at program startup. A happy side-effect is that
647 -mno-eabi libraries can be used with -meabi programs.)
649 The EABI configuration defaults to the V.4 layout. However,
650 the stack alignment requirements may differ. If -mno-eabi is not
651 given, the required stack alignment is 8 bytes; if -mno-eabi is
652 given, the required alignment is 16 bytes. (But see V.4 comment
655 #ifndef ABI_STACK_BOUNDARY
656 #define ABI_STACK_BOUNDARY STACK_BOUNDARY
660 rs6000_stack_info (void)
662 /* We should never be called for thunks, we are not set up for that. */
663 gcc_assert (!cfun->is_thunk);
665 rs6000_stack_t *info = &stack_info;
666 int reg_size = TARGET_32BIT ? 4 : 8;
671 HOST_WIDE_INT non_fixed_size;
672 bool using_static_chain_p;
674 if (reload_completed && info->reload_completed)
677 memset (info, 0, sizeof (*info));
678 info->reload_completed = reload_completed;
680 /* Select which calling sequence. */
681 info->abi = DEFAULT_ABI;
683 /* Calculate which registers need to be saved & save area size. */
684 info->first_gp_reg_save = first_reg_to_save ();
685 /* Assume that we will have to save RS6000_PIC_OFFSET_TABLE_REGNUM,
686 even if it currently looks like we won't. Reload may need it to
687 get at a constant; if so, it will have already created a constant
688 pool entry for it. */
689 if (((TARGET_TOC && TARGET_MINIMAL_TOC)
690 || (flag_pic == 1 && DEFAULT_ABI == ABI_V4)
691 || (flag_pic && DEFAULT_ABI == ABI_DARWIN))
692 && crtl->uses_const_pool
693 && info->first_gp_reg_save > RS6000_PIC_OFFSET_TABLE_REGNUM)
694 first_gp = RS6000_PIC_OFFSET_TABLE_REGNUM;
696 first_gp = info->first_gp_reg_save;
698 info->gp_size = reg_size * (32 - first_gp);
700 info->first_fp_reg_save = first_fp_reg_to_save ();
701 info->fp_size = 8 * (64 - info->first_fp_reg_save);
703 info->first_altivec_reg_save = first_altivec_reg_to_save ();
704 info->altivec_size = 16 * (LAST_ALTIVEC_REGNO + 1
705 - info->first_altivec_reg_save);
707 /* Does this function call anything? */
708 info->calls_p = (!crtl->is_leaf || cfun->machine->ra_needs_full_frame);
710 /* Determine if we need to save the condition code registers. */
711 if (save_reg_p (CR2_REGNO)
712 || save_reg_p (CR3_REGNO)
713 || save_reg_p (CR4_REGNO))
716 if (DEFAULT_ABI == ABI_V4)
717 info->cr_size = reg_size;
720 /* If the current function calls __builtin_eh_return, then we need
721 to allocate stack space for registers that will hold data for
722 the exception handler. */
723 if (crtl->calls_eh_return)
726 for (i = 0; EH_RETURN_DATA_REGNO (i) != INVALID_REGNUM; ++i)
729 ehrd_size = i * UNITS_PER_WORD;
734 /* In the ELFv2 ABI, we also need to allocate space for separate
735 CR field save areas if the function calls __builtin_eh_return. */
736 if (DEFAULT_ABI == ABI_ELFv2 && crtl->calls_eh_return)
738 /* This hard-codes that we have three call-saved CR fields. */
739 ehcr_size = 3 * reg_size;
740 /* We do *not* use the regular CR save mechanism. */
746 /* Determine various sizes. */
747 info->reg_size = reg_size;
748 info->fixed_size = RS6000_SAVE_AREA;
749 info->vars_size = RS6000_ALIGN (get_frame_size (), 8);
750 if (cfun->calls_alloca)
752 RS6000_ALIGN (crtl->outgoing_args_size + info->fixed_size,
753 STACK_BOUNDARY / BITS_PER_UNIT) - info->fixed_size;
755 info->parm_size = RS6000_ALIGN (crtl->outgoing_args_size,
756 TARGET_ALTIVEC ? 16 : 8);
757 if (FRAME_GROWS_DOWNWARD)
759 += RS6000_ALIGN (info->fixed_size + info->vars_size + info->parm_size,
760 ABI_STACK_BOUNDARY / BITS_PER_UNIT)
761 - (info->fixed_size + info->vars_size + info->parm_size);
763 if (TARGET_ALTIVEC_ABI)
764 info->vrsave_mask = compute_vrsave_mask ();
766 if (TARGET_ALTIVEC_VRSAVE && info->vrsave_mask)
767 info->vrsave_size = 4;
769 compute_save_world_info (info);
771 /* Calculate the offsets. */
781 info->fp_save_offset = -info->fp_size;
782 info->gp_save_offset = info->fp_save_offset - info->gp_size;
784 if (TARGET_ALTIVEC_ABI)
786 info->vrsave_save_offset = info->gp_save_offset - info->vrsave_size;
788 /* Align stack so vector save area is on a quadword boundary.
789 The padding goes above the vectors. */
790 if (info->altivec_size != 0)
791 info->altivec_padding_size = info->vrsave_save_offset & 0xF;
793 info->altivec_save_offset = info->vrsave_save_offset
794 - info->altivec_padding_size
795 - info->altivec_size;
796 gcc_assert (info->altivec_size == 0
797 || info->altivec_save_offset % 16 == 0);
799 /* Adjust for AltiVec case. */
800 info->ehrd_offset = info->altivec_save_offset - ehrd_size;
803 info->ehrd_offset = info->gp_save_offset - ehrd_size;
805 info->ehcr_offset = info->ehrd_offset - ehcr_size;
806 info->cr_save_offset = reg_size; /* first word when 64-bit. */
807 info->lr_save_offset = 2*reg_size;
811 info->fp_save_offset = -info->fp_size;
812 info->gp_save_offset = info->fp_save_offset - info->gp_size;
813 info->cr_save_offset = info->gp_save_offset - info->cr_size;
815 if (TARGET_ALTIVEC_ABI)
817 info->vrsave_save_offset = info->cr_save_offset - info->vrsave_size;
819 /* Align stack so vector save area is on a quadword boundary. */
820 if (info->altivec_size != 0)
821 info->altivec_padding_size = 16 - (-info->vrsave_save_offset % 16);
823 info->altivec_save_offset = info->vrsave_save_offset
824 - info->altivec_padding_size
825 - info->altivec_size;
827 /* Adjust for AltiVec case. */
828 info->ehrd_offset = info->altivec_save_offset;
831 info->ehrd_offset = info->cr_save_offset;
833 info->ehrd_offset -= ehrd_size;
834 info->lr_save_offset = reg_size;
837 save_align = (TARGET_ALTIVEC_ABI || DEFAULT_ABI == ABI_DARWIN) ? 16 : 8;
838 info->save_size = RS6000_ALIGN (info->fp_size
841 + info->altivec_padding_size
848 non_fixed_size = info->vars_size + info->parm_size + info->save_size;
850 info->total_size = RS6000_ALIGN (non_fixed_size + info->fixed_size,
851 ABI_STACK_BOUNDARY / BITS_PER_UNIT);
853 /* Determine if we need to save the link register. */
855 || ((DEFAULT_ABI == ABI_AIX || DEFAULT_ABI == ABI_ELFv2)
857 && !TARGET_PROFILE_KERNEL)
858 || (DEFAULT_ABI == ABI_V4 && cfun->calls_alloca)
859 #ifdef TARGET_RELOCATABLE
860 || (DEFAULT_ABI == ABI_V4
861 && (TARGET_RELOCATABLE || flag_pic > 1)
862 && !constant_pool_empty_p ())
864 || rs6000_ra_ever_killed ())
867 using_static_chain_p = (cfun->static_chain_decl != NULL_TREE
868 && df_regs_ever_live_p (STATIC_CHAIN_REGNUM)
869 && call_used_regs[STATIC_CHAIN_REGNUM]);
870 info->savres_strategy = rs6000_savres_strategy (info, using_static_chain_p);
872 if (!(info->savres_strategy & SAVE_INLINE_GPRS)
873 || !(info->savres_strategy & SAVE_INLINE_FPRS)
874 || !(info->savres_strategy & SAVE_INLINE_VRS)
875 || !(info->savres_strategy & REST_INLINE_GPRS)
876 || !(info->savres_strategy & REST_INLINE_FPRS)
877 || !(info->savres_strategy & REST_INLINE_VRS))
881 df_set_regs_ever_live (LR_REGNO, true);
883 /* Determine if we need to allocate any stack frame:
885 For AIX we need to push the stack if a frame pointer is needed
886 (because the stack might be dynamically adjusted), if we are
887 debugging, if we make calls, or if the sum of fp_save, gp_save,
888 and local variables are more than the space needed to save all
889 non-volatile registers: 32-bit: 18*8 + 19*4 = 220 or 64-bit: 18*8
890 + 18*8 = 288 (GPR13 reserved).
892 For V.4 we don't have the stack cushion that AIX uses, but assume
893 that the debugger can handle stackless frames. */
898 else if (DEFAULT_ABI == ABI_V4)
899 info->push_p = non_fixed_size != 0;
901 else if (frame_pointer_needed)
904 else if (TARGET_XCOFF && write_symbols != NO_DEBUG)
908 info->push_p = non_fixed_size > (TARGET_32BIT ? 220 : 288);
914 debug_stack_info (rs6000_stack_t *info)
916 const char *abi_string;
919 info = rs6000_stack_info ();
921 fprintf (stderr, "\nStack information for function %s:\n",
922 ((current_function_decl && DECL_NAME (current_function_decl))
923 ? IDENTIFIER_POINTER (DECL_NAME (current_function_decl))
928 default: abi_string = "Unknown"; break;
929 case ABI_NONE: abi_string = "NONE"; break;
930 case ABI_AIX: abi_string = "AIX"; break;
931 case ABI_ELFv2: abi_string = "ELFv2"; break;
932 case ABI_DARWIN: abi_string = "Darwin"; break;
933 case ABI_V4: abi_string = "V.4"; break;
936 fprintf (stderr, "\tABI = %5s\n", abi_string);
938 if (TARGET_ALTIVEC_ABI)
939 fprintf (stderr, "\tALTIVEC ABI extensions enabled.\n");
941 if (info->first_gp_reg_save != 32)
942 fprintf (stderr, "\tfirst_gp_reg_save = %5d\n", info->first_gp_reg_save);
944 if (info->first_fp_reg_save != 64)
945 fprintf (stderr, "\tfirst_fp_reg_save = %5d\n", info->first_fp_reg_save);
947 if (info->first_altivec_reg_save <= LAST_ALTIVEC_REGNO)
948 fprintf (stderr, "\tfirst_altivec_reg_save = %5d\n",
949 info->first_altivec_reg_save);
952 fprintf (stderr, "\tlr_save_p = %5d\n", info->lr_save_p);
955 fprintf (stderr, "\tcr_save_p = %5d\n", info->cr_save_p);
957 if (info->vrsave_mask)
958 fprintf (stderr, "\tvrsave_mask = 0x%x\n", info->vrsave_mask);
961 fprintf (stderr, "\tpush_p = %5d\n", info->push_p);
964 fprintf (stderr, "\tcalls_p = %5d\n", info->calls_p);
967 fprintf (stderr, "\tgp_save_offset = %5d\n", info->gp_save_offset);
970 fprintf (stderr, "\tfp_save_offset = %5d\n", info->fp_save_offset);
972 if (info->altivec_size)
973 fprintf (stderr, "\taltivec_save_offset = %5d\n",
974 info->altivec_save_offset);
976 if (info->vrsave_size)
977 fprintf (stderr, "\tvrsave_save_offset = %5d\n",
978 info->vrsave_save_offset);
981 fprintf (stderr, "\tlr_save_offset = %5d\n", info->lr_save_offset);
984 fprintf (stderr, "\tcr_save_offset = %5d\n", info->cr_save_offset);
986 if (info->varargs_save_offset)
987 fprintf (stderr, "\tvarargs_save_offset = %5d\n", info->varargs_save_offset);
989 if (info->total_size)
990 fprintf (stderr, "\ttotal_size = " HOST_WIDE_INT_PRINT_DEC"\n",
994 fprintf (stderr, "\tvars_size = " HOST_WIDE_INT_PRINT_DEC"\n",
998 fprintf (stderr, "\tparm_size = %5d\n", info->parm_size);
1000 if (info->fixed_size)
1001 fprintf (stderr, "\tfixed_size = %5d\n", info->fixed_size);
1004 fprintf (stderr, "\tgp_size = %5d\n", info->gp_size);
1007 fprintf (stderr, "\tfp_size = %5d\n", info->fp_size);
1009 if (info->altivec_size)
1010 fprintf (stderr, "\taltivec_size = %5d\n", info->altivec_size);
1012 if (info->vrsave_size)
1013 fprintf (stderr, "\tvrsave_size = %5d\n", info->vrsave_size);
1015 if (info->altivec_padding_size)
1016 fprintf (stderr, "\taltivec_padding_size= %5d\n",
1017 info->altivec_padding_size);
1020 fprintf (stderr, "\tcr_size = %5d\n", info->cr_size);
1022 if (info->save_size)
1023 fprintf (stderr, "\tsave_size = %5d\n", info->save_size);
1025 if (info->reg_size != 4)
1026 fprintf (stderr, "\treg_size = %5d\n", info->reg_size);
1028 fprintf (stderr, "\tsave-strategy = %04x\n", info->savres_strategy);
1030 if (info->abi == ABI_DARWIN)
1031 fprintf (stderr, "\tWORLD_SAVE_P = %5d\n", WORLD_SAVE_P(info));
1033 fprintf (stderr, "\n");
1037 rs6000_return_addr (int count, rtx frame)
1039 /* We can't use get_hard_reg_initial_val for LR when count == 0 if LR
1040 is trashed by the prologue, as it is for PIC on ABI_V4 and Darwin. */
1042 || ((DEFAULT_ABI == ABI_V4 || DEFAULT_ABI == ABI_DARWIN) && flag_pic))
1044 cfun->machine->ra_needs_full_frame = 1;
1047 /* FRAME is set to frame_pointer_rtx by the generic code, but that
1048 is good for loading 0(r1) only when !FRAME_GROWS_DOWNWARD. */
1049 frame = stack_pointer_rtx;
1050 rtx prev_frame_addr = memory_address (Pmode, frame);
1051 rtx prev_frame = copy_to_reg (gen_rtx_MEM (Pmode, prev_frame_addr));
1052 rtx lr_save_off = plus_constant (Pmode,
1053 prev_frame, RETURN_ADDRESS_OFFSET);
1054 rtx lr_save_addr = memory_address (Pmode, lr_save_off);
1055 return gen_rtx_MEM (Pmode, lr_save_addr);
1058 cfun->machine->ra_need_lr = 1;
1059 return get_hard_reg_initial_val (Pmode, LR_REGNO);
1062 /* Helper function for rs6000_function_ok_for_sibcall. */
1065 rs6000_decl_ok_for_sibcall (tree decl)
1067 /* Sibcalls are always fine for the Darwin ABI. */
1068 if (DEFAULT_ABI == ABI_DARWIN)
1071 if (DEFAULT_ABI == ABI_AIX || DEFAULT_ABI == ABI_ELFv2)
1073 /* Under the AIX or ELFv2 ABIs we can't allow calls to non-local
1074 functions, because the callee may have a different TOC pointer to
1075 the caller and there's no way to ensure we restore the TOC when
1077 if (!decl || DECL_EXTERNAL (decl) || DECL_WEAK (decl)
1078 || !(*targetm.binds_local_p) (decl))
1081 /* Similarly, if the caller preserves the TOC pointer and the callee
1082 doesn't (or vice versa), proper TOC setup or restoration will be
1083 missed. For example, suppose A, B, and C are in the same binary
1084 and A -> B -> C. A and B preserve the TOC pointer but C does not,
1085 and B -> C is eligible as a sibcall. A will call B through its
1086 local entry point, so A will not restore its TOC itself. B calls
1087 C with a sibcall, so it will not restore the TOC. C does not
1088 preserve the TOC, so it may clobber r2 with impunity. Returning
1089 from C will result in a corrupted TOC for A. */
1090 else if (rs6000_fndecl_pcrel_p (decl) != rs6000_pcrel_p (cfun))
1097 /* With the secure-plt SYSV ABI we can't make non-local calls when
1098 -fpic/PIC because the plt call stubs use r30. */
1099 if (DEFAULT_ABI != ABI_V4
1100 || (TARGET_SECURE_PLT
1102 && (!decl || !((*targetm.binds_local_p) (decl)))))
1108 /* Say whether a function is a candidate for sibcall handling or not. */
1111 rs6000_function_ok_for_sibcall (tree decl, tree exp)
1115 /* The sibcall epilogue may clobber the static chain register.
1116 ??? We could work harder and avoid that, but it's probably
1117 not worth the hassle in practice. */
1118 if (CALL_EXPR_STATIC_CHAIN (exp))
1122 fntype = TREE_TYPE (decl);
1124 fntype = TREE_TYPE (TREE_TYPE (CALL_EXPR_FN (exp)));
1126 /* We can't do it if the called function has more vector parameters
1127 than the current function; there's nowhere to put the VRsave code. */
1128 if (TARGET_ALTIVEC_ABI
1129 && TARGET_ALTIVEC_VRSAVE
1130 && !(decl && decl == current_function_decl))
1132 function_args_iterator args_iter;
1136 /* Functions with vector parameters are required to have a
1137 prototype, so the argument type info must be available
1139 FOREACH_FUNCTION_ARGS(fntype, type, args_iter)
1140 if (TREE_CODE (type) == VECTOR_TYPE
1141 && ALTIVEC_OR_VSX_VECTOR_MODE (TYPE_MODE (type)))
1144 FOREACH_FUNCTION_ARGS(TREE_TYPE (current_function_decl), type, args_iter)
1145 if (TREE_CODE (type) == VECTOR_TYPE
1146 && ALTIVEC_OR_VSX_VECTOR_MODE (TYPE_MODE (type)))
1153 if (rs6000_decl_ok_for_sibcall (decl))
1155 tree attr_list = TYPE_ATTRIBUTES (fntype);
1157 if (!lookup_attribute ("longcall", attr_list)
1158 || lookup_attribute ("shortcall", attr_list))
1166 rs6000_ra_ever_killed (void)
1175 if (cfun->machine->lr_save_state)
1176 return cfun->machine->lr_save_state - 1;
1178 /* regs_ever_live has LR marked as used if any sibcalls are present,
1179 but this should not force saving and restoring in the
1180 pro/epilogue. Likewise, reg_set_between_p thinks a sibcall
1181 clobbers LR, so that is inappropriate. */
1183 /* Also, the prologue can generate a store into LR that
1184 doesn't really count, like this:
1187 bcl to set PIC register
1191 When we're called from the epilogue, we need to avoid counting
1194 push_topmost_sequence ();
1196 pop_topmost_sequence ();
1197 reg = gen_rtx_REG (Pmode, LR_REGNO);
1199 for (insn = NEXT_INSN (top); insn != NULL_RTX; insn = NEXT_INSN (insn))
1205 if (!SIBLING_CALL_P (insn))
1208 else if (find_regno_note (insn, REG_INC, LR_REGNO))
1210 else if (set_of (reg, insn) != NULL_RTX
1211 && !prologue_epilogue_contains (insn))
1218 /* Emit instructions needed to load the TOC register.
1219 This is only needed when TARGET_TOC, TARGET_MINIMAL_TOC, and there is
1220 a constant pool; or for SVR4 -fpic. */
1223 rs6000_emit_load_toc_table (int fromprolog)
1226 dest = gen_rtx_REG (Pmode, RS6000_PIC_OFFSET_TABLE_REGNUM);
1228 if (TARGET_ELF && TARGET_SECURE_PLT && DEFAULT_ABI == ABI_V4 && flag_pic)
1231 rtx lab, tmp1, tmp2, got;
1233 lab = gen_label_rtx ();
1234 ASM_GENERATE_INTERNAL_LABEL (buf, "L", CODE_LABEL_NUMBER (lab));
1235 lab = gen_rtx_SYMBOL_REF (Pmode, ggc_strdup (buf));
1238 got = gen_rtx_SYMBOL_REF (Pmode, ggc_strdup (toc_label_name));
1242 got = rs6000_got_sym ();
1246 tmp1 = gen_reg_rtx (Pmode);
1247 tmp2 = gen_reg_rtx (Pmode);
1249 emit_insn (gen_load_toc_v4_PIC_1 (lab));
1250 emit_move_insn (tmp1, gen_rtx_REG (Pmode, LR_REGNO));
1251 emit_insn (gen_load_toc_v4_PIC_3b (tmp2, tmp1, got, lab));
1252 emit_insn (gen_load_toc_v4_PIC_3c (dest, tmp2, got, lab));
1254 else if (TARGET_ELF && DEFAULT_ABI == ABI_V4 && flag_pic == 1)
1256 emit_insn (gen_load_toc_v4_pic_si ());
1257 emit_move_insn (dest, gen_rtx_REG (Pmode, LR_REGNO));
1259 else if (TARGET_ELF && DEFAULT_ABI == ABI_V4 && flag_pic == 2)
1262 rtx temp0 = (fromprolog
1263 ? gen_rtx_REG (Pmode, 0)
1264 : gen_reg_rtx (Pmode));
1270 ASM_GENERATE_INTERNAL_LABEL (buf, "LCF", rs6000_pic_labelno);
1271 symF = gen_rtx_SYMBOL_REF (Pmode, ggc_strdup (buf));
1273 ASM_GENERATE_INTERNAL_LABEL (buf, "LCL", rs6000_pic_labelno);
1274 symL = gen_rtx_SYMBOL_REF (Pmode, ggc_strdup (buf));
1276 emit_insn (gen_load_toc_v4_PIC_1 (symF));
1277 emit_move_insn (dest, gen_rtx_REG (Pmode, LR_REGNO));
1278 emit_insn (gen_load_toc_v4_PIC_2 (temp0, dest, symL, symF));
1284 tocsym = gen_rtx_SYMBOL_REF (Pmode, ggc_strdup (toc_label_name));
1286 lab = gen_label_rtx ();
1287 emit_insn (gen_load_toc_v4_PIC_1b (tocsym, lab));
1288 emit_move_insn (dest, gen_rtx_REG (Pmode, LR_REGNO));
1289 if (TARGET_LINK_STACK)
1290 emit_insn (gen_addsi3 (dest, dest, GEN_INT (4)));
1291 emit_move_insn (temp0, gen_rtx_MEM (Pmode, dest));
1293 emit_insn (gen_addsi3 (dest, temp0, dest));
1295 else if (TARGET_ELF && !TARGET_AIX && flag_pic == 0 && TARGET_MINIMAL_TOC)
1297 /* This is for AIX code running in non-PIC ELF32. */
1298 rtx realsym = gen_rtx_SYMBOL_REF (Pmode, ggc_strdup (toc_label_name));
1301 emit_insn (gen_elf_high (dest, realsym));
1302 emit_insn (gen_elf_low (dest, dest, realsym));
1306 gcc_assert (DEFAULT_ABI == ABI_AIX || DEFAULT_ABI == ABI_ELFv2);
1309 emit_insn (gen_load_toc_aix_si (dest));
1311 emit_insn (gen_load_toc_aix_di (dest));
1315 /* Emit instructions to restore the link register after determining where
1316 its value has been stored. */
1319 rs6000_emit_eh_reg_restore (rtx source, rtx scratch)
1321 rs6000_stack_t *info = rs6000_stack_info ();
1324 operands[0] = source;
1325 operands[1] = scratch;
1327 if (info->lr_save_p)
1329 rtx frame_rtx = stack_pointer_rtx;
1330 HOST_WIDE_INT sp_offset = 0;
1333 if (frame_pointer_needed
1334 || cfun->calls_alloca
1335 || info->total_size > 32767)
1337 tmp = gen_frame_mem (Pmode, frame_rtx);
1338 emit_move_insn (operands[1], tmp);
1339 frame_rtx = operands[1];
1341 else if (info->push_p)
1342 sp_offset = info->total_size;
1344 tmp = plus_constant (Pmode, frame_rtx,
1345 info->lr_save_offset + sp_offset);
1346 tmp = gen_frame_mem (Pmode, tmp);
1347 emit_move_insn (tmp, operands[0]);
1350 emit_move_insn (gen_rtx_REG (Pmode, LR_REGNO), operands[0]);
1352 /* Freeze lr_save_p. We've just emitted rtl that depends on the
1353 state of lr_save_p so any change from here on would be a bug. In
1354 particular, stop rs6000_ra_ever_killed from considering the SET
1355 of lr we may have added just above. */
1356 cfun->machine->lr_save_state = info->lr_save_p + 1;
1359 /* This returns nonzero if the current function uses the TOC. This is
1360 determined by the presence of (use (unspec ... UNSPEC_TOC)), which
1361 is generated by the ABI_V4 load_toc_* patterns.
1362 Return 2 instead of 1 if the load_toc_* pattern is in the function
1363 partition that doesn't start the function. */
1371 for (insn = get_insns (); insn; insn = NEXT_INSN (insn))
1375 rtx pat = PATTERN (insn);
1378 if (GET_CODE (pat) == PARALLEL)
1379 for (i = 0; i < XVECLEN (pat, 0); i++)
1381 rtx sub = XVECEXP (pat, 0, i);
1382 if (GET_CODE (sub) == USE)
1384 sub = XEXP (sub, 0);
1385 if (GET_CODE (sub) == UNSPEC
1386 && XINT (sub, 1) == UNSPEC_TOC)
1391 else if (crtl->has_bb_partition
1393 && NOTE_KIND (insn) == NOTE_INSN_SWITCH_TEXT_SECTIONS)
1401 create_TOC_reference (rtx symbol, rtx largetoc_reg)
1403 rtx tocrel, tocreg, hi;
1405 if (TARGET_DEBUG_ADDR)
1407 if (SYMBOL_REF_P (symbol))
1408 fprintf (stderr, "\ncreate_TOC_reference, (symbol_ref %s)\n",
1412 fprintf (stderr, "\ncreate_TOC_reference, code %s:\n",
1413 GET_RTX_NAME (GET_CODE (symbol)));
1418 if (!can_create_pseudo_p ())
1419 df_set_regs_ever_live (TOC_REGISTER, true);
1421 tocreg = gen_rtx_REG (Pmode, TOC_REGISTER);
1422 tocrel = gen_rtx_UNSPEC (Pmode, gen_rtvec (2, symbol, tocreg), UNSPEC_TOCREL);
1423 if (TARGET_CMODEL == CMODEL_SMALL || can_create_pseudo_p ())
1426 hi = gen_rtx_HIGH (Pmode, copy_rtx (tocrel));
1427 if (largetoc_reg != NULL)
1429 emit_move_insn (largetoc_reg, hi);
1432 return gen_rtx_LO_SUM (Pmode, hi, tocrel);
1435 /* Issue assembly directives that create a reference to the given DWARF
1436 FRAME_TABLE_LABEL from the current function section. */
1438 rs6000_aix_asm_output_dwarf_table_ref (char * frame_table_label)
1440 fprintf (asm_out_file, "\t.ref %s\n",
1441 (* targetm.strip_name_encoding) (frame_table_label));
1444 /* This ties together stack memory (MEM with an alias set of frame_alias_set)
1445 and the change to the stack pointer. */
1448 rs6000_emit_stack_tie (rtx fp, bool hard_frame_needed)
1455 regs[i++] = gen_rtx_REG (Pmode, STACK_POINTER_REGNUM);
1456 if (hard_frame_needed)
1457 regs[i++] = gen_rtx_REG (Pmode, HARD_FRAME_POINTER_REGNUM);
1458 if (!(REGNO (fp) == STACK_POINTER_REGNUM
1459 || (hard_frame_needed
1460 && REGNO (fp) == HARD_FRAME_POINTER_REGNUM)))
1463 p = rtvec_alloc (i);
1466 rtx mem = gen_frame_mem (BLKmode, regs[i]);
1467 RTVEC_ELT (p, i) = gen_rtx_SET (mem, const0_rtx);
1470 emit_insn (gen_stack_tie (gen_rtx_PARALLEL (VOIDmode, p)));
1473 /* Allocate SIZE_INT bytes on the stack using a store with update style insn
1474 and set the appropriate attributes for the generated insn. Return the
1475 first insn which adjusts the stack pointer or the last insn before
1476 the stack adjustment loop.
1478 SIZE_INT is used to create the CFI note for the allocation.
1480 SIZE_RTX is an rtx containing the size of the adjustment. Note that
1481 since stacks grow to lower addresses its runtime value is -SIZE_INT.
1483 ORIG_SP contains the backchain value that must be stored at *sp. */
1486 rs6000_emit_allocate_stack_1 (HOST_WIDE_INT size_int, rtx orig_sp)
1490 rtx size_rtx = GEN_INT (-size_int);
1491 if (size_int > 32767)
1493 rtx tmp_reg = gen_rtx_REG (Pmode, 0);
1494 /* Need a note here so that try_split doesn't get confused. */
1495 if (get_last_insn () == NULL_RTX)
1496 emit_note (NOTE_INSN_DELETED);
1497 insn = emit_move_insn (tmp_reg, size_rtx);
1498 try_split (PATTERN (insn), insn, 0);
1503 insn = emit_insn (gen_movsi_update_stack (stack_pointer_rtx,
1508 insn = emit_insn (gen_movdi_update_stack (stack_pointer_rtx,
1512 rtx par = PATTERN (insn);
1513 gcc_assert (GET_CODE (par) == PARALLEL);
1514 rtx set = XVECEXP (par, 0, 0);
1515 gcc_assert (GET_CODE (set) == SET);
1516 rtx mem = SET_DEST (set);
1517 gcc_assert (MEM_P (mem));
1518 MEM_NOTRAP_P (mem) = 1;
1519 set_mem_alias_set (mem, get_frame_alias_set ());
1521 RTX_FRAME_RELATED_P (insn) = 1;
1522 add_reg_note (insn, REG_FRAME_RELATED_EXPR,
1523 gen_rtx_SET (stack_pointer_rtx,
1524 gen_rtx_PLUS (Pmode,
1526 GEN_INT (-size_int))));
1528 /* Emit a blockage to ensure the allocation/probing insns are
1529 not optimized, combined, removed, etc. Add REG_STACK_CHECK
1530 note for similar reasons. */
1531 if (flag_stack_clash_protection)
1533 add_reg_note (insn, REG_STACK_CHECK, const0_rtx);
1534 emit_insn (gen_blockage ());
1540 static HOST_WIDE_INT
1541 get_stack_clash_protection_probe_interval (void)
1543 return (HOST_WIDE_INT_1U
1544 << PARAM_VALUE (PARAM_STACK_CLASH_PROTECTION_PROBE_INTERVAL));
1547 static HOST_WIDE_INT
1548 get_stack_clash_protection_guard_size (void)
1550 return (HOST_WIDE_INT_1U
1551 << PARAM_VALUE (PARAM_STACK_CLASH_PROTECTION_GUARD_SIZE));
1554 /* Allocate ORIG_SIZE bytes on the stack and probe the newly
1555 allocated space every STACK_CLASH_PROTECTION_PROBE_INTERVAL bytes.
1557 COPY_REG, if non-null, should contain a copy of the original
1558 stack pointer at exit from this function.
1560 This is subtly different than the Ada probing in that it tries hard to
1561 prevent attacks that jump the stack guard. Thus it is never allowed to
1562 allocate more than STACK_CLASH_PROTECTION_PROBE_INTERVAL bytes of stack
1563 space without a suitable probe. */
1565 rs6000_emit_probe_stack_range_stack_clash (HOST_WIDE_INT orig_size,
1568 rtx orig_sp = copy_reg;
1570 HOST_WIDE_INT probe_interval = get_stack_clash_protection_probe_interval ();
1572 /* Round the size down to a multiple of PROBE_INTERVAL. */
1573 HOST_WIDE_INT rounded_size = ROUND_DOWN (orig_size, probe_interval);
1575 /* If explicitly requested,
1576 or the rounded size is not the same as the original size
1577 or the the rounded size is greater than a page,
1578 then we will need a copy of the original stack pointer. */
1579 if (rounded_size != orig_size
1580 || rounded_size > probe_interval
1583 /* If the caller did not request a copy of the incoming stack
1584 pointer, then we use r0 to hold the copy. */
1586 orig_sp = gen_rtx_REG (Pmode, 0);
1587 emit_move_insn (orig_sp, stack_pointer_rtx);
1590 /* There's three cases here.
1592 One is a single probe which is the most common and most efficiently
1593 implemented as it does not have to have a copy of the original
1594 stack pointer if there are no residuals.
1596 Second is unrolled allocation/probes which we use if there's just
1597 a few of them. It needs to save the original stack pointer into a
1598 temporary for use as a source register in the allocation/probe.
1600 Last is a loop. This is the most uncommon case and least efficient. */
1601 rtx_insn *retval = NULL;
1602 if (rounded_size == probe_interval)
1604 retval = rs6000_emit_allocate_stack_1 (probe_interval, stack_pointer_rtx);
1606 dump_stack_clash_frame_info (PROBE_INLINE, rounded_size != orig_size);
1608 else if (rounded_size <= 8 * probe_interval)
1610 /* The ABI requires using the store with update insns to allocate
1611 space and store the backchain into the stack
1613 So we save the current stack pointer into a temporary, then
1614 emit the store-with-update insns to store the saved stack pointer
1615 into the right location in each new page. */
1616 for (int i = 0; i < rounded_size; i += probe_interval)
1619 = rs6000_emit_allocate_stack_1 (probe_interval, orig_sp);
1621 /* Save the first stack adjustment in RETVAL. */
1626 dump_stack_clash_frame_info (PROBE_INLINE, rounded_size != orig_size);
1630 /* Compute the ending address. */
1632 = copy_reg ? gen_rtx_REG (Pmode, 0) : gen_rtx_REG (Pmode, 12);
1633 rtx rs = GEN_INT (-rounded_size);
1635 if (add_operand (rs, Pmode))
1636 insn = emit_insn (gen_add3_insn (end_addr, stack_pointer_rtx, rs));
1639 emit_move_insn (end_addr, GEN_INT (-rounded_size));
1640 insn = emit_insn (gen_add3_insn (end_addr, end_addr,
1641 stack_pointer_rtx));
1642 /* Describe the effect of INSN to the CFI engine. */
1643 add_reg_note (insn, REG_FRAME_RELATED_EXPR,
1644 gen_rtx_SET (end_addr,
1645 gen_rtx_PLUS (Pmode, stack_pointer_rtx,
1648 RTX_FRAME_RELATED_P (insn) = 1;
1650 /* Emit the loop. */
1652 retval = emit_insn (gen_probe_stack_rangedi (stack_pointer_rtx,
1653 stack_pointer_rtx, orig_sp,
1656 retval = emit_insn (gen_probe_stack_rangesi (stack_pointer_rtx,
1657 stack_pointer_rtx, orig_sp,
1659 RTX_FRAME_RELATED_P (retval) = 1;
1660 /* Describe the effect of INSN to the CFI engine. */
1661 add_reg_note (retval, REG_FRAME_RELATED_EXPR,
1662 gen_rtx_SET (stack_pointer_rtx, end_addr));
1664 /* Emit a blockage to ensure the allocation/probing insns are
1665 not optimized, combined, removed, etc. Other cases handle this
1666 within their call to rs6000_emit_allocate_stack_1. */
1667 emit_insn (gen_blockage ());
1669 dump_stack_clash_frame_info (PROBE_LOOP, rounded_size != orig_size);
1672 if (orig_size != rounded_size)
1674 /* Allocate (and implicitly probe) any residual space. */
1675 HOST_WIDE_INT residual = orig_size - rounded_size;
1677 rtx_insn *insn = rs6000_emit_allocate_stack_1 (residual, orig_sp);
1679 /* If the residual was the only allocation, then we can return the
1688 /* Emit the correct code for allocating stack space, as insns.
1689 If COPY_REG, make sure a copy of the old frame is left there.
1690 The generated code may use hard register 0 as a temporary. */
1693 rs6000_emit_allocate_stack (HOST_WIDE_INT size, rtx copy_reg, int copy_off)
1696 rtx stack_reg = gen_rtx_REG (Pmode, STACK_POINTER_REGNUM);
1697 rtx tmp_reg = gen_rtx_REG (Pmode, 0);
1698 rtx todec = gen_int_mode (-size, Pmode);
1700 if (INTVAL (todec) != -size)
1702 warning (0, "stack frame too large");
1703 emit_insn (gen_trap ());
1707 if (crtl->limit_stack)
1709 if (REG_P (stack_limit_rtx)
1710 && REGNO (stack_limit_rtx) > 1
1711 && REGNO (stack_limit_rtx) <= 31)
1714 = gen_add3_insn (tmp_reg, stack_limit_rtx, GEN_INT (size));
1717 emit_insn (gen_cond_trap (LTU, stack_reg, tmp_reg, const0_rtx));
1719 else if (SYMBOL_REF_P (stack_limit_rtx)
1721 && DEFAULT_ABI == ABI_V4
1724 rtx toload = gen_rtx_CONST (VOIDmode,
1725 gen_rtx_PLUS (Pmode,
1729 emit_insn (gen_elf_high (tmp_reg, toload));
1730 emit_insn (gen_elf_low (tmp_reg, tmp_reg, toload));
1731 emit_insn (gen_cond_trap (LTU, stack_reg, tmp_reg,
1735 warning (0, "stack limit expression is not supported");
1738 if (flag_stack_clash_protection)
1740 if (size < get_stack_clash_protection_guard_size ())
1741 dump_stack_clash_frame_info (NO_PROBE_SMALL_FRAME, true);
1744 rtx_insn *insn = rs6000_emit_probe_stack_range_stack_clash (size,
1747 /* If we asked for a copy with an offset, then we still need add in
1749 if (copy_reg && copy_off)
1750 emit_insn (gen_add3_insn (copy_reg, copy_reg, GEN_INT (copy_off)));
1758 emit_insn (gen_add3_insn (copy_reg, stack_reg, GEN_INT (copy_off)));
1760 emit_move_insn (copy_reg, stack_reg);
1763 /* Since we didn't use gen_frame_mem to generate the MEM, grab
1764 it now and set the alias set/attributes. The above gen_*_update
1765 calls will generate a PARALLEL with the MEM set being the first
1767 insn = rs6000_emit_allocate_stack_1 (size, stack_reg);
1771 #define PROBE_INTERVAL (1 << STACK_CHECK_PROBE_INTERVAL_EXP)
1773 #if PROBE_INTERVAL > 32768
1774 #error Cannot use indexed addressing mode for stack probing
1777 /* Emit code to probe a range of stack addresses from FIRST to FIRST+SIZE,
1778 inclusive. These are offsets from the current stack pointer. */
1781 rs6000_emit_probe_stack_range (HOST_WIDE_INT first, HOST_WIDE_INT size)
1783 /* See if we have a constant small number of probes to generate. If so,
1784 that's the easy case. */
1785 if (first + size <= 32768)
1789 /* Probe at FIRST + N * PROBE_INTERVAL for values of N from 1 until
1790 it exceeds SIZE. If only one probe is needed, this will not
1791 generate any code. Then probe at FIRST + SIZE. */
1792 for (i = PROBE_INTERVAL; i < size; i += PROBE_INTERVAL)
1793 emit_stack_probe (plus_constant (Pmode, stack_pointer_rtx,
1796 emit_stack_probe (plus_constant (Pmode, stack_pointer_rtx,
1800 /* Otherwise, do the same as above, but in a loop. Note that we must be
1801 extra careful with variables wrapping around because we might be at
1802 the very top (or the very bottom) of the address space and we have
1803 to be able to handle this case properly; in particular, we use an
1804 equality test for the loop condition. */
1807 HOST_WIDE_INT rounded_size;
1808 rtx r12 = gen_rtx_REG (Pmode, 12);
1809 rtx r0 = gen_rtx_REG (Pmode, 0);
1811 /* Sanity check for the addressing mode we're going to use. */
1812 gcc_assert (first <= 32768);
1814 /* Step 1: round SIZE to the previous multiple of the interval. */
1816 rounded_size = ROUND_DOWN (size, PROBE_INTERVAL);
1819 /* Step 2: compute initial and final value of the loop counter. */
1821 /* TEST_ADDR = SP + FIRST. */
1822 emit_insn (gen_rtx_SET (r12, plus_constant (Pmode, stack_pointer_rtx,
1825 /* LAST_ADDR = SP + FIRST + ROUNDED_SIZE. */
1826 if (rounded_size > 32768)
1828 emit_move_insn (r0, GEN_INT (-rounded_size));
1829 emit_insn (gen_rtx_SET (r0, gen_rtx_PLUS (Pmode, r12, r0)));
1832 emit_insn (gen_rtx_SET (r0, plus_constant (Pmode, r12,
1840 TEST_ADDR = TEST_ADDR + PROBE_INTERVAL
1843 while (TEST_ADDR != LAST_ADDR)
1845 probes at FIRST + N * PROBE_INTERVAL for values of N from 1
1846 until it is equal to ROUNDED_SIZE. */
1849 emit_insn (gen_probe_stack_rangedi (r12, r12, stack_pointer_rtx, r0));
1851 emit_insn (gen_probe_stack_rangesi (r12, r12, stack_pointer_rtx, r0));
1854 /* Step 4: probe at FIRST + SIZE if we cannot assert at compile-time
1855 that SIZE is equal to ROUNDED_SIZE. */
1857 if (size != rounded_size)
1858 emit_stack_probe (plus_constant (Pmode, r12, rounded_size - size));
1862 /* Probe a range of stack addresses from REG1 to REG2 inclusive. These are
1863 addresses, not offsets. */
1866 output_probe_stack_range_1 (rtx reg1, rtx reg2)
1868 static int labelno = 0;
1872 ASM_GENERATE_INTERNAL_LABEL (loop_lab, "LPSRL", labelno++);
1875 ASM_OUTPUT_INTERNAL_LABEL (asm_out_file, loop_lab);
1877 /* TEST_ADDR = TEST_ADDR + PROBE_INTERVAL. */
1879 xops[1] = GEN_INT (-PROBE_INTERVAL);
1880 output_asm_insn ("addi %0,%0,%1", xops);
1882 /* Probe at TEST_ADDR. */
1883 xops[1] = gen_rtx_REG (Pmode, 0);
1884 output_asm_insn ("stw %1,0(%0)", xops);
1886 /* Test if TEST_ADDR == LAST_ADDR. */
1889 output_asm_insn ("cmpd 0,%0,%1", xops);
1891 output_asm_insn ("cmpw 0,%0,%1", xops);
1894 fputs ("\tbne 0,", asm_out_file);
1895 assemble_name_raw (asm_out_file, loop_lab);
1896 fputc ('\n', asm_out_file);
1901 /* This function is called when rs6000_frame_related is processing
1902 SETs within a PARALLEL, and returns whether the REGNO save ought to
1903 be marked RTX_FRAME_RELATED_P. The PARALLELs involved are those
1904 for out-of-line register save functions, store multiple, and the
1905 Darwin world_save. They may contain registers that don't really
1909 interesting_frame_related_regno (unsigned int regno)
1911 /* Saves apparently of r0 are actually saving LR. It doesn't make
1912 sense to substitute the regno here to test save_reg_p (LR_REGNO).
1913 We *know* LR needs saving, and dwarf2cfi.c is able to deduce that
1914 (set (mem) (r0)) is saving LR from a prior (set (r0) (lr)) marked
1915 as frame related. */
1918 /* If we see CR2 then we are here on a Darwin world save. Saves of
1919 CR2 signify the whole CR is being saved. This is a long-standing
1920 ABI wart fixed by ELFv2. As for r0/lr there is no need to check
1921 that CR needs to be saved. */
1922 if (regno == CR2_REGNO)
1924 /* Omit frame info for any user-defined global regs. If frame info
1925 is supplied for them, frame unwinding will restore a user reg.
1926 Also omit frame info for any reg we don't need to save, as that
1927 bloats frame info and can cause problems with shrink wrapping.
1928 Since global regs won't be seen as needing to be saved, both of
1929 these conditions are covered by save_reg_p. */
1930 return save_reg_p (regno);
1933 /* Probe a range of stack addresses from REG1 to REG3 inclusive. These are
1934 addresses, not offsets.
1936 REG2 contains the backchain that must be stored into *sp at each allocation.
1938 This is subtly different than the Ada probing above in that it tries hard
1939 to prevent attacks that jump the stack guard. Thus, it is never allowed
1940 to allocate more than PROBE_INTERVAL bytes of stack space without a
1944 output_probe_stack_range_stack_clash (rtx reg1, rtx reg2, rtx reg3)
1946 static int labelno = 0;
1950 HOST_WIDE_INT probe_interval = get_stack_clash_protection_probe_interval ();
1952 ASM_GENERATE_INTERNAL_LABEL (loop_lab, "LPSRL", labelno++);
1954 ASM_OUTPUT_INTERNAL_LABEL (asm_out_file, loop_lab);
1956 /* This allocates and probes. */
1959 xops[2] = GEN_INT (-probe_interval);
1961 output_asm_insn ("stdu %1,%2(%0)", xops);
1963 output_asm_insn ("stwu %1,%2(%0)", xops);
1965 /* Jump to LOOP_LAB if TEST_ADDR != LAST_ADDR. */
1969 output_asm_insn ("cmpd 0,%0,%1", xops);
1971 output_asm_insn ("cmpw 0,%0,%1", xops);
1973 fputs ("\tbne 0,", asm_out_file);
1974 assemble_name_raw (asm_out_file, loop_lab);
1975 fputc ('\n', asm_out_file);
1980 /* Wrapper around the output_probe_stack_range routines. */
1982 output_probe_stack_range (rtx reg1, rtx reg2, rtx reg3)
1984 if (flag_stack_clash_protection)
1985 return output_probe_stack_range_stack_clash (reg1, reg2, reg3);
1987 return output_probe_stack_range_1 (reg1, reg3);
1990 /* Add to 'insn' a note which is PATTERN (INSN) but with REG replaced
1991 with (plus:P (reg 1) VAL), and with REG2 replaced with REPL2 if REG2
1992 is not NULL. It would be nice if dwarf2out_frame_debug_expr could
1993 deduce these equivalences by itself so it wasn't necessary to hold
1994 its hand so much. Don't be tempted to always supply d2_f_d_e with
1995 the actual cfa register, ie. r31 when we are using a hard frame
1996 pointer. That fails when saving regs off r1, and sched moves the
1997 r31 setup past the reg saves. */
2000 rs6000_frame_related (rtx_insn *insn, rtx reg, HOST_WIDE_INT val,
2001 rtx reg2, rtx repl2)
2005 if (REGNO (reg) == STACK_POINTER_REGNUM)
2007 gcc_checking_assert (val == 0);
2011 repl = gen_rtx_PLUS (Pmode, gen_rtx_REG (Pmode, STACK_POINTER_REGNUM),
2014 rtx pat = PATTERN (insn);
2017 /* No need for any replacement. Just set RTX_FRAME_RELATED_P. */
2018 if (GET_CODE (pat) == PARALLEL)
2019 for (int i = 0; i < XVECLEN (pat, 0); i++)
2020 if (GET_CODE (XVECEXP (pat, 0, i)) == SET)
2022 rtx set = XVECEXP (pat, 0, i);
2024 if (!REG_P (SET_SRC (set))
2025 || interesting_frame_related_regno (REGNO (SET_SRC (set))))
2026 RTX_FRAME_RELATED_P (set) = 1;
2028 RTX_FRAME_RELATED_P (insn) = 1;
2032 /* We expect that 'pat' is either a SET or a PARALLEL containing
2033 SETs (and possibly other stuff). In a PARALLEL, all the SETs
2034 are important so they all have to be marked RTX_FRAME_RELATED_P.
2035 Call simplify_replace_rtx on the SETs rather than the whole insn
2036 so as to leave the other stuff alone (for example USE of r12). */
2038 set_used_flags (pat);
2039 if (GET_CODE (pat) == SET)
2042 pat = simplify_replace_rtx (pat, reg, repl);
2044 pat = simplify_replace_rtx (pat, reg2, repl2);
2046 else if (GET_CODE (pat) == PARALLEL)
2048 pat = shallow_copy_rtx (pat);
2049 XVEC (pat, 0) = shallow_copy_rtvec (XVEC (pat, 0));
2051 for (int i = 0; i < XVECLEN (pat, 0); i++)
2052 if (GET_CODE (XVECEXP (pat, 0, i)) == SET)
2054 rtx set = XVECEXP (pat, 0, i);
2057 set = simplify_replace_rtx (set, reg, repl);
2059 set = simplify_replace_rtx (set, reg2, repl2);
2060 XVECEXP (pat, 0, i) = set;
2062 if (!REG_P (SET_SRC (set))
2063 || interesting_frame_related_regno (REGNO (SET_SRC (set))))
2064 RTX_FRAME_RELATED_P (set) = 1;
2070 RTX_FRAME_RELATED_P (insn) = 1;
2071 add_reg_note (insn, REG_FRAME_RELATED_EXPR, copy_rtx_if_shared (pat));
2076 /* Returns an insn that has a vrsave set operation with the
2077 appropriate CLOBBERs. */
2080 generate_set_vrsave (rtx reg, rs6000_stack_t *info, int epiloguep)
2083 rtx insn, clobs[TOTAL_ALTIVEC_REGS + 1];
2084 rtx vrsave = gen_rtx_REG (SImode, VRSAVE_REGNO);
2087 = gen_rtx_SET (vrsave,
2088 gen_rtx_UNSPEC_VOLATILE (SImode,
2089 gen_rtvec (2, reg, vrsave),
2090 UNSPECV_SET_VRSAVE));
2094 /* We need to clobber the registers in the mask so the scheduler
2095 does not move sets to VRSAVE before sets of AltiVec registers.
2097 However, if the function receives nonlocal gotos, reload will set
2098 all call saved registers live. We will end up with:
2100 (set (reg 999) (mem))
2101 (parallel [ (set (reg vrsave) (unspec blah))
2102 (clobber (reg 999))])
2104 The clobber will cause the store into reg 999 to be dead, and
2105 flow will attempt to delete an epilogue insn. In this case, we
2106 need an unspec use/set of the register. */
2108 for (i = FIRST_ALTIVEC_REGNO; i <= LAST_ALTIVEC_REGNO; ++i)
2109 if (info->vrsave_mask & ALTIVEC_REG_BIT (i))
2111 if (!epiloguep || call_used_regs [i])
2112 clobs[nclobs++] = gen_hard_reg_clobber (V4SImode, i);
2115 rtx reg = gen_rtx_REG (V4SImode, i);
2119 gen_rtx_UNSPEC (V4SImode,
2120 gen_rtvec (1, reg), 27));
2124 insn = gen_rtx_PARALLEL (VOIDmode, rtvec_alloc (nclobs));
2126 for (i = 0; i < nclobs; ++i)
2127 XVECEXP (insn, 0, i) = clobs[i];
2133 gen_frame_set (rtx reg, rtx frame_reg, int offset, bool store)
2137 addr = gen_rtx_PLUS (Pmode, frame_reg, GEN_INT (offset));
2138 mem = gen_frame_mem (GET_MODE (reg), addr);
2139 return gen_rtx_SET (store ? mem : reg, store ? reg : mem);
2143 gen_frame_load (rtx reg, rtx frame_reg, int offset)
2145 return gen_frame_set (reg, frame_reg, offset, false);
2149 gen_frame_store (rtx reg, rtx frame_reg, int offset)
2151 return gen_frame_set (reg, frame_reg, offset, true);
2154 /* Save a register into the frame, and emit RTX_FRAME_RELATED_P notes.
2155 Save REGNO into [FRAME_REG + OFFSET] in mode MODE. */
2158 emit_frame_save (rtx frame_reg, machine_mode mode,
2159 unsigned int regno, int offset, HOST_WIDE_INT frame_reg_to_sp)
2163 /* Some cases that need register indexed addressing. */
2164 gcc_checking_assert (!(TARGET_ALTIVEC_ABI && ALTIVEC_VECTOR_MODE (mode))
2165 || (TARGET_VSX && ALTIVEC_OR_VSX_VECTOR_MODE (mode)));
2167 reg = gen_rtx_REG (mode, regno);
2168 rtx_insn *insn = emit_insn (gen_frame_store (reg, frame_reg, offset));
2169 return rs6000_frame_related (insn, frame_reg, frame_reg_to_sp,
2170 NULL_RTX, NULL_RTX);
2173 /* Emit an offset memory reference suitable for a frame store, while
2174 converting to a valid addressing mode. */
2177 gen_frame_mem_offset (machine_mode mode, rtx reg, int offset)
2179 return gen_frame_mem (mode, gen_rtx_PLUS (Pmode, reg, GEN_INT (offset)));
2182 #ifndef TARGET_FIX_AND_CONTINUE
2183 #define TARGET_FIX_AND_CONTINUE 0
2186 /* It's really GPR 13 or 14, FPR 14 and VR 20. We need the smallest. */
2187 #define FIRST_SAVRES_REGISTER FIRST_SAVED_GP_REGNO
2188 #define LAST_SAVRES_REGISTER 31
2189 #define N_SAVRES_REGISTERS (LAST_SAVRES_REGISTER - FIRST_SAVRES_REGISTER + 1)
2200 static GTY(()) rtx savres_routine_syms[N_SAVRES_REGISTERS][12];
2202 /* Temporary holding space for an out-of-line register save/restore
2204 static char savres_routine_name[30];
2206 /* Return the name for an out-of-line register save/restore routine.
2207 We are saving/restoring GPRs if GPR is true. */
2210 rs6000_savres_routine_name (int regno, int sel)
2212 const char *prefix = "";
2213 const char *suffix = "";
2215 /* Different targets are supposed to define
2216 {SAVE,RESTORE}_FP_{PREFIX,SUFFIX} with the idea that the needed
2217 routine name could be defined with:
2219 sprintf (name, "%s%d%s", SAVE_FP_PREFIX, regno, SAVE_FP_SUFFIX)
2221 This is a nice idea in practice, but in reality, things are
2222 complicated in several ways:
2224 - ELF targets have save/restore routines for GPRs.
2226 - PPC64 ELF targets have routines for save/restore of GPRs that
2227 differ in what they do with the link register, so having a set
2228 prefix doesn't work. (We only use one of the save routines at
2229 the moment, though.)
2231 - PPC32 elf targets have "exit" versions of the restore routines
2232 that restore the link register and can save some extra space.
2233 These require an extra suffix. (There are also "tail" versions
2234 of the restore routines and "GOT" versions of the save routines,
2235 but we don't generate those at present. Same problems apply,
2238 We deal with all this by synthesizing our own prefix/suffix and
2239 using that for the simple sprintf call shown above. */
2240 if (DEFAULT_ABI == ABI_V4)
2245 if ((sel & SAVRES_REG) == SAVRES_GPR)
2246 prefix = (sel & SAVRES_SAVE) ? "_savegpr_" : "_restgpr_";
2247 else if ((sel & SAVRES_REG) == SAVRES_FPR)
2248 prefix = (sel & SAVRES_SAVE) ? "_savefpr_" : "_restfpr_";
2249 else if ((sel & SAVRES_REG) == SAVRES_VR)
2250 prefix = (sel & SAVRES_SAVE) ? "_savevr_" : "_restvr_";
2254 if ((sel & SAVRES_LR))
2257 else if (DEFAULT_ABI == ABI_AIX || DEFAULT_ABI == ABI_ELFv2)
2259 #if !defined (POWERPC_LINUX) && !defined (POWERPC_FREEBSD)
2260 /* No out-of-line save/restore routines for GPRs on AIX. */
2261 gcc_assert (!TARGET_AIX || (sel & SAVRES_REG) != SAVRES_GPR);
2265 if ((sel & SAVRES_REG) == SAVRES_GPR)
2266 prefix = ((sel & SAVRES_SAVE)
2267 ? ((sel & SAVRES_LR) ? "_savegpr0_" : "_savegpr1_")
2268 : ((sel & SAVRES_LR) ? "_restgpr0_" : "_restgpr1_"));
2269 else if ((sel & SAVRES_REG) == SAVRES_FPR)
2271 #if defined (POWERPC_LINUX) || defined (POWERPC_FREEBSD)
2272 if ((sel & SAVRES_LR))
2273 prefix = ((sel & SAVRES_SAVE) ? "_savefpr_" : "_restfpr_");
2277 prefix = (sel & SAVRES_SAVE) ? SAVE_FP_PREFIX : RESTORE_FP_PREFIX;
2278 suffix = (sel & SAVRES_SAVE) ? SAVE_FP_SUFFIX : RESTORE_FP_SUFFIX;
2281 else if ((sel & SAVRES_REG) == SAVRES_VR)
2282 prefix = (sel & SAVRES_SAVE) ? "_savevr_" : "_restvr_";
2287 if (DEFAULT_ABI == ABI_DARWIN)
2289 /* The Darwin approach is (slightly) different, in order to be
2290 compatible with code generated by the system toolchain. There is a
2291 single symbol for the start of save sequence, and the code here
2292 embeds an offset into that code on the basis of the first register
2294 prefix = (sel & SAVRES_SAVE) ? "save" : "rest" ;
2295 if ((sel & SAVRES_REG) == SAVRES_GPR)
2296 sprintf (savres_routine_name, "*%sGPR%s%s%.0d ; %s r%d-r31", prefix,
2297 ((sel & SAVRES_LR) ? "x" : ""), (regno == 13 ? "" : "+"),
2298 (regno - 13) * 4, prefix, regno);
2299 else if ((sel & SAVRES_REG) == SAVRES_FPR)
2300 sprintf (savres_routine_name, "*%sFP%s%.0d ; %s f%d-f31", prefix,
2301 (regno == 14 ? "" : "+"), (regno - 14) * 4, prefix, regno);
2302 else if ((sel & SAVRES_REG) == SAVRES_VR)
2303 sprintf (savres_routine_name, "*%sVEC%s%.0d ; %s v%d-v31", prefix,
2304 (regno == 20 ? "" : "+"), (regno - 20) * 8, prefix, regno);
2309 sprintf (savres_routine_name, "%s%d%s", prefix, regno, suffix);
2311 return savres_routine_name;
2314 /* Return an RTL SYMBOL_REF for an out-of-line register save/restore routine.
2315 We are saving/restoring GPRs if GPR is true. */
2318 rs6000_savres_routine_sym (rs6000_stack_t *info, int sel)
2320 int regno = ((sel & SAVRES_REG) == SAVRES_GPR
2321 ? info->first_gp_reg_save
2322 : (sel & SAVRES_REG) == SAVRES_FPR
2323 ? info->first_fp_reg_save - 32
2324 : (sel & SAVRES_REG) == SAVRES_VR
2325 ? info->first_altivec_reg_save - FIRST_ALTIVEC_REGNO
2330 /* Don't generate bogus routine names. */
2331 gcc_assert (FIRST_SAVRES_REGISTER <= regno
2332 && regno <= LAST_SAVRES_REGISTER
2333 && select >= 0 && select <= 12);
2335 sym = savres_routine_syms[regno-FIRST_SAVRES_REGISTER][select];
2341 name = rs6000_savres_routine_name (regno, sel);
2343 sym = savres_routine_syms[regno-FIRST_SAVRES_REGISTER][select]
2344 = gen_rtx_SYMBOL_REF (Pmode, ggc_strdup (name));
2345 SYMBOL_REF_FLAGS (sym) |= SYMBOL_FLAG_FUNCTION;
2351 /* Emit a sequence of insns, including a stack tie if needed, for
2352 resetting the stack pointer. If UPDT_REGNO is not 1, then don't
2353 reset the stack pointer, but move the base of the frame into
2354 reg UPDT_REGNO for use by out-of-line register restore routines. */
2357 rs6000_emit_stack_reset (rtx frame_reg_rtx, HOST_WIDE_INT frame_off,
2358 unsigned updt_regno)
2360 /* If there is nothing to do, don't do anything. */
2361 if (frame_off == 0 && REGNO (frame_reg_rtx) == updt_regno)
2364 rtx updt_reg_rtx = gen_rtx_REG (Pmode, updt_regno);
2366 /* This blockage is needed so that sched doesn't decide to move
2367 the sp change before the register restores. */
2368 if (DEFAULT_ABI == ABI_V4)
2369 return emit_insn (gen_stack_restore_tie (updt_reg_rtx, frame_reg_rtx,
2370 GEN_INT (frame_off)));
2372 /* If we are restoring registers out-of-line, we will be using the
2373 "exit" variants of the restore routines, which will reset the
2374 stack for us. But we do need to point updt_reg into the
2375 right place for those routines. */
2377 return emit_insn (gen_add3_insn (updt_reg_rtx,
2378 frame_reg_rtx, GEN_INT (frame_off)));
2380 return emit_move_insn (updt_reg_rtx, frame_reg_rtx);
2385 /* Return the register number used as a pointer by out-of-line
2386 save/restore functions. */
2388 static inline unsigned
2389 ptr_regno_for_savres (int sel)
2391 if (DEFAULT_ABI == ABI_AIX || DEFAULT_ABI == ABI_ELFv2)
2392 return (sel & SAVRES_REG) == SAVRES_FPR || (sel & SAVRES_LR) ? 1 : 12;
2393 return DEFAULT_ABI == ABI_DARWIN && (sel & SAVRES_REG) == SAVRES_FPR ? 1 : 11;
2396 /* Construct a parallel rtx describing the effect of a call to an
2397 out-of-line register save/restore routine, and emit the insn
2398 or jump_insn as appropriate. */
2401 rs6000_emit_savres_rtx (rs6000_stack_t *info,
2402 rtx frame_reg_rtx, int save_area_offset, int lr_offset,
2403 machine_mode reg_mode, int sel)
2406 int offset, start_reg, end_reg, n_regs, use_reg;
2407 int reg_size = GET_MODE_SIZE (reg_mode);
2414 start_reg = ((sel & SAVRES_REG) == SAVRES_GPR
2415 ? info->first_gp_reg_save
2416 : (sel & SAVRES_REG) == SAVRES_FPR
2417 ? info->first_fp_reg_save
2418 : (sel & SAVRES_REG) == SAVRES_VR
2419 ? info->first_altivec_reg_save
2421 end_reg = ((sel & SAVRES_REG) == SAVRES_GPR
2423 : (sel & SAVRES_REG) == SAVRES_FPR
2425 : (sel & SAVRES_REG) == SAVRES_VR
2426 ? LAST_ALTIVEC_REGNO + 1
2428 n_regs = end_reg - start_reg;
2429 p = rtvec_alloc (3 + ((sel & SAVRES_LR) ? 1 : 0)
2430 + ((sel & SAVRES_REG) == SAVRES_VR ? 1 : 0)
2433 if (!(sel & SAVRES_SAVE) && (sel & SAVRES_LR))
2434 RTVEC_ELT (p, offset++) = ret_rtx;
2436 RTVEC_ELT (p, offset++) = gen_hard_reg_clobber (Pmode, LR_REGNO);
2438 sym = rs6000_savres_routine_sym (info, sel);
2439 RTVEC_ELT (p, offset++) = gen_rtx_USE (VOIDmode, sym);
2441 use_reg = ptr_regno_for_savres (sel);
2442 if ((sel & SAVRES_REG) == SAVRES_VR)
2444 /* Vector regs are saved/restored using [reg+reg] addressing. */
2445 RTVEC_ELT (p, offset++) = gen_hard_reg_clobber (Pmode, use_reg);
2446 RTVEC_ELT (p, offset++)
2447 = gen_rtx_USE (VOIDmode, gen_rtx_REG (Pmode, 0));
2450 RTVEC_ELT (p, offset++)
2451 = gen_rtx_USE (VOIDmode, gen_rtx_REG (Pmode, use_reg));
2453 for (i = 0; i < end_reg - start_reg; i++)
2454 RTVEC_ELT (p, i + offset)
2455 = gen_frame_set (gen_rtx_REG (reg_mode, start_reg + i),
2456 frame_reg_rtx, save_area_offset + reg_size * i,
2457 (sel & SAVRES_SAVE) != 0);
2459 if ((sel & SAVRES_SAVE) && (sel & SAVRES_LR))
2460 RTVEC_ELT (p, i + offset)
2461 = gen_frame_store (gen_rtx_REG (Pmode, 0), frame_reg_rtx, lr_offset);
2463 par = gen_rtx_PARALLEL (VOIDmode, p);
2465 if (!(sel & SAVRES_SAVE) && (sel & SAVRES_LR))
2467 insn = emit_jump_insn (par);
2468 JUMP_LABEL (insn) = ret_rtx;
2471 insn = emit_insn (par);
2475 /* Emit prologue code to store CR fields that need to be saved into REG. This
2476 function should only be called when moving the non-volatile CRs to REG, it
2477 is not a general purpose routine to move the entire set of CRs to REG.
2478 Specifically, gen_prologue_movesi_from_cr() does not contain uses of the
2482 rs6000_emit_prologue_move_from_cr (rtx reg)
2484 /* Only the ELFv2 ABI allows storing only selected fields. */
2485 if (DEFAULT_ABI == ABI_ELFv2 && TARGET_MFCRF)
2487 int i, cr_reg[8], count = 0;
2489 /* Collect CR fields that must be saved. */
2490 for (i = 0; i < 8; i++)
2491 if (save_reg_p (CR0_REGNO + i))
2492 cr_reg[count++] = i;
2494 /* If it's just a single one, use mfcrf. */
2497 rtvec p = rtvec_alloc (1);
2498 rtvec r = rtvec_alloc (2);
2499 RTVEC_ELT (r, 0) = gen_rtx_REG (CCmode, CR0_REGNO + cr_reg[0]);
2500 RTVEC_ELT (r, 1) = GEN_INT (1 << (7 - cr_reg[0]));
2503 gen_rtx_UNSPEC (SImode, r, UNSPEC_MOVESI_FROM_CR));
2505 emit_insn (gen_rtx_PARALLEL (VOIDmode, p));
2509 /* ??? It might be better to handle count == 2 / 3 cases here
2510 as well, using logical operations to combine the values. */
2513 emit_insn (gen_prologue_movesi_from_cr (reg));
2516 /* Return whether the split-stack arg pointer (r12) is used. */
2519 split_stack_arg_pointer_used_p (void)
2521 /* If the pseudo holding the arg pointer is no longer a pseudo,
2522 then the arg pointer is used. */
2523 if (cfun->machine->split_stack_arg_pointer != NULL_RTX
2524 && (!REG_P (cfun->machine->split_stack_arg_pointer)
2525 || HARD_REGISTER_P (cfun->machine->split_stack_arg_pointer)))
2528 /* Unfortunately we also need to do some code scanning, since
2529 r12 may have been substituted for the pseudo. */
2531 basic_block bb = ENTRY_BLOCK_PTR_FOR_FN (cfun)->next_bb;
2532 FOR_BB_INSNS (bb, insn)
2533 if (NONDEBUG_INSN_P (insn))
2535 /* A call destroys r12. */
2540 FOR_EACH_INSN_USE (use, insn)
2542 rtx x = DF_REF_REG (use);
2543 if (REG_P (x) && REGNO (x) == 12)
2547 FOR_EACH_INSN_DEF (def, insn)
2549 rtx x = DF_REF_REG (def);
2550 if (REG_P (x) && REGNO (x) == 12)
2554 return bitmap_bit_p (DF_LR_OUT (bb), 12);
2557 /* Return whether we need to emit an ELFv2 global entry point prologue. */
2560 rs6000_global_entry_point_prologue_needed_p (void)
2562 /* Only needed for the ELFv2 ABI. */
2563 if (DEFAULT_ABI != ABI_ELFv2)
2566 /* With -msingle-pic-base, we assume the whole program shares the same
2567 TOC, so no global entry point prologues are needed anywhere. */
2568 if (TARGET_SINGLE_PIC_BASE)
2571 /* PC-relative functions never generate a global entry point prologue. */
2572 if (rs6000_pcrel_p (cfun))
2575 /* Ensure we have a global entry point for thunks. ??? We could
2576 avoid that if the target routine doesn't need a global entry point,
2577 but we do not know whether this is the case at this point. */
2581 /* For regular functions, rs6000_emit_prologue sets this flag if the
2582 routine ever uses the TOC pointer. */
2583 return cfun->machine->r2_setup_needed;
2586 /* Implement TARGET_SHRINK_WRAP_GET_SEPARATE_COMPONENTS. */
2588 rs6000_get_separate_components (void)
2590 rs6000_stack_t *info = rs6000_stack_info ();
2592 if (WORLD_SAVE_P (info))
2595 gcc_assert (!(info->savres_strategy & SAVE_MULTIPLE)
2596 && !(info->savres_strategy & REST_MULTIPLE));
2598 /* Component 0 is the save/restore of LR (done via GPR0).
2599 Component 2 is the save of the TOC (GPR2).
2600 Components 13..31 are the save/restore of GPR13..GPR31.
2601 Components 46..63 are the save/restore of FPR14..FPR31. */
2603 cfun->machine->n_components = 64;
2605 sbitmap components = sbitmap_alloc (cfun->machine->n_components);
2606 bitmap_clear (components);
2608 int reg_size = TARGET_32BIT ? 4 : 8;
2609 int fp_reg_size = 8;
2611 /* The GPRs we need saved to the frame. */
2612 if ((info->savres_strategy & SAVE_INLINE_GPRS)
2613 && (info->savres_strategy & REST_INLINE_GPRS))
2615 int offset = info->gp_save_offset;
2617 offset += info->total_size;
2619 for (unsigned regno = info->first_gp_reg_save; regno < 32; regno++)
2621 if (IN_RANGE (offset, -0x8000, 0x7fff)
2622 && save_reg_p (regno))
2623 bitmap_set_bit (components, regno);
2629 /* Don't mess with the hard frame pointer. */
2630 if (frame_pointer_needed)
2631 bitmap_clear_bit (components, HARD_FRAME_POINTER_REGNUM);
2633 /* Don't mess with the fixed TOC register. */
2634 if ((TARGET_TOC && TARGET_MINIMAL_TOC)
2635 || (flag_pic == 1 && DEFAULT_ABI == ABI_V4)
2636 || (flag_pic && DEFAULT_ABI == ABI_DARWIN))
2637 bitmap_clear_bit (components, RS6000_PIC_OFFSET_TABLE_REGNUM);
2639 /* The FPRs we need saved to the frame. */
2640 if ((info->savres_strategy & SAVE_INLINE_FPRS)
2641 && (info->savres_strategy & REST_INLINE_FPRS))
2643 int offset = info->fp_save_offset;
2645 offset += info->total_size;
2647 for (unsigned regno = info->first_fp_reg_save; regno < 64; regno++)
2649 if (IN_RANGE (offset, -0x8000, 0x7fff) && save_reg_p (regno))
2650 bitmap_set_bit (components, regno);
2652 offset += fp_reg_size;
2656 /* Optimize LR save and restore if we can. This is component 0. Any
2657 out-of-line register save/restore routines need LR. */
2659 && !(flag_pic && (DEFAULT_ABI == ABI_V4 || DEFAULT_ABI == ABI_DARWIN))
2660 && (info->savres_strategy & SAVE_INLINE_GPRS)
2661 && (info->savres_strategy & REST_INLINE_GPRS)
2662 && (info->savres_strategy & SAVE_INLINE_FPRS)
2663 && (info->savres_strategy & REST_INLINE_FPRS)
2664 && (info->savres_strategy & SAVE_INLINE_VRS)
2665 && (info->savres_strategy & REST_INLINE_VRS))
2667 int offset = info->lr_save_offset;
2669 offset += info->total_size;
2670 if (IN_RANGE (offset, -0x8000, 0x7fff))
2671 bitmap_set_bit (components, 0);
2674 /* Optimize saving the TOC. This is component 2. */
2675 if (cfun->machine->save_toc_in_prologue)
2676 bitmap_set_bit (components, 2);
2681 /* Implement TARGET_SHRINK_WRAP_COMPONENTS_FOR_BB. */
2683 rs6000_components_for_bb (basic_block bb)
2685 rs6000_stack_t *info = rs6000_stack_info ();
2687 bitmap in = DF_LIVE_IN (bb);
2688 bitmap gen = &DF_LIVE_BB_INFO (bb)->gen;
2689 bitmap kill = &DF_LIVE_BB_INFO (bb)->kill;
2691 sbitmap components = sbitmap_alloc (cfun->machine->n_components);
2692 bitmap_clear (components);
2694 /* A register is used in a bb if it is in the IN, GEN, or KILL sets. */
2697 for (unsigned regno = info->first_gp_reg_save; regno < 32; regno++)
2698 if (bitmap_bit_p (in, regno)
2699 || bitmap_bit_p (gen, regno)
2700 || bitmap_bit_p (kill, regno))
2701 bitmap_set_bit (components, regno);
2704 for (unsigned regno = info->first_fp_reg_save; regno < 64; regno++)
2705 if (bitmap_bit_p (in, regno)
2706 || bitmap_bit_p (gen, regno)
2707 || bitmap_bit_p (kill, regno))
2708 bitmap_set_bit (components, regno);
2710 /* The link register. */
2711 if (bitmap_bit_p (in, LR_REGNO)
2712 || bitmap_bit_p (gen, LR_REGNO)
2713 || bitmap_bit_p (kill, LR_REGNO))
2714 bitmap_set_bit (components, 0);
2717 if (bitmap_bit_p (in, TOC_REGNUM)
2718 || bitmap_bit_p (gen, TOC_REGNUM)
2719 || bitmap_bit_p (kill, TOC_REGNUM))
2720 bitmap_set_bit (components, 2);
2725 /* Implement TARGET_SHRINK_WRAP_DISQUALIFY_COMPONENTS. */
2727 rs6000_disqualify_components (sbitmap components, edge e,
2728 sbitmap edge_components, bool /*is_prologue*/)
2730 /* Our LR pro/epilogue code moves LR via R0, so R0 had better not be
2731 live where we want to place that code. */
2732 if (bitmap_bit_p (edge_components, 0)
2733 && bitmap_bit_p (DF_LIVE_IN (e->dest), 0))
2736 fprintf (dump_file, "Disqualifying LR because GPR0 is live "
2737 "on entry to bb %d\n", e->dest->index);
2738 bitmap_clear_bit (components, 0);
2742 /* Implement TARGET_SHRINK_WRAP_EMIT_PROLOGUE_COMPONENTS. */
2744 rs6000_emit_prologue_components (sbitmap components)
2746 rs6000_stack_t *info = rs6000_stack_info ();
2747 rtx ptr_reg = gen_rtx_REG (Pmode, frame_pointer_needed
2748 ? HARD_FRAME_POINTER_REGNUM
2749 : STACK_POINTER_REGNUM);
2751 machine_mode reg_mode = Pmode;
2752 int reg_size = TARGET_32BIT ? 4 : 8;
2753 machine_mode fp_reg_mode = TARGET_HARD_FLOAT ? DFmode : SFmode;
2754 int fp_reg_size = 8;
2756 /* Prologue for LR. */
2757 if (bitmap_bit_p (components, 0))
2759 rtx lr = gen_rtx_REG (reg_mode, LR_REGNO);
2760 rtx reg = gen_rtx_REG (reg_mode, 0);
2761 rtx_insn *insn = emit_move_insn (reg, lr);
2762 RTX_FRAME_RELATED_P (insn) = 1;
2763 add_reg_note (insn, REG_CFA_REGISTER, gen_rtx_SET (reg, lr));
2765 int offset = info->lr_save_offset;
2767 offset += info->total_size;
2769 insn = emit_insn (gen_frame_store (reg, ptr_reg, offset));
2770 RTX_FRAME_RELATED_P (insn) = 1;
2771 rtx mem = copy_rtx (SET_DEST (single_set (insn)));
2772 add_reg_note (insn, REG_CFA_OFFSET, gen_rtx_SET (mem, lr));
2775 /* Prologue for TOC. */
2776 if (bitmap_bit_p (components, 2))
2778 rtx reg = gen_rtx_REG (reg_mode, TOC_REGNUM);
2779 rtx sp_reg = gen_rtx_REG (Pmode, STACK_POINTER_REGNUM);
2780 emit_insn (gen_frame_store (reg, sp_reg, RS6000_TOC_SAVE_SLOT));
2783 /* Prologue for the GPRs. */
2784 int offset = info->gp_save_offset;
2786 offset += info->total_size;
2788 for (int i = info->first_gp_reg_save; i < 32; i++)
2790 if (bitmap_bit_p (components, i))
2792 rtx reg = gen_rtx_REG (reg_mode, i);
2793 rtx_insn *insn = emit_insn (gen_frame_store (reg, ptr_reg, offset));
2794 RTX_FRAME_RELATED_P (insn) = 1;
2795 rtx set = copy_rtx (single_set (insn));
2796 add_reg_note (insn, REG_CFA_OFFSET, set);
2802 /* Prologue for the FPRs. */
2803 offset = info->fp_save_offset;
2805 offset += info->total_size;
2807 for (int i = info->first_fp_reg_save; i < 64; i++)
2809 if (bitmap_bit_p (components, i))
2811 rtx reg = gen_rtx_REG (fp_reg_mode, i);
2812 rtx_insn *insn = emit_insn (gen_frame_store (reg, ptr_reg, offset));
2813 RTX_FRAME_RELATED_P (insn) = 1;
2814 rtx set = copy_rtx (single_set (insn));
2815 add_reg_note (insn, REG_CFA_OFFSET, set);
2818 offset += fp_reg_size;
2822 /* Implement TARGET_SHRINK_WRAP_EMIT_EPILOGUE_COMPONENTS. */
2824 rs6000_emit_epilogue_components (sbitmap components)
2826 rs6000_stack_t *info = rs6000_stack_info ();
2827 rtx ptr_reg = gen_rtx_REG (Pmode, frame_pointer_needed
2828 ? HARD_FRAME_POINTER_REGNUM
2829 : STACK_POINTER_REGNUM);
2831 machine_mode reg_mode = Pmode;
2832 int reg_size = TARGET_32BIT ? 4 : 8;
2834 machine_mode fp_reg_mode = TARGET_HARD_FLOAT ? DFmode : SFmode;
2835 int fp_reg_size = 8;
2837 /* Epilogue for the FPRs. */
2838 int offset = info->fp_save_offset;
2840 offset += info->total_size;
2842 for (int i = info->first_fp_reg_save; i < 64; i++)
2844 if (bitmap_bit_p (components, i))
2846 rtx reg = gen_rtx_REG (fp_reg_mode, i);
2847 rtx_insn *insn = emit_insn (gen_frame_load (reg, ptr_reg, offset));
2848 RTX_FRAME_RELATED_P (insn) = 1;
2849 add_reg_note (insn, REG_CFA_RESTORE, reg);
2852 offset += fp_reg_size;
2855 /* Epilogue for the GPRs. */
2856 offset = info->gp_save_offset;
2858 offset += info->total_size;
2860 for (int i = info->first_gp_reg_save; i < 32; i++)
2862 if (bitmap_bit_p (components, i))
2864 rtx reg = gen_rtx_REG (reg_mode, i);
2865 rtx_insn *insn = emit_insn (gen_frame_load (reg, ptr_reg, offset));
2866 RTX_FRAME_RELATED_P (insn) = 1;
2867 add_reg_note (insn, REG_CFA_RESTORE, reg);
2873 /* Epilogue for LR. */
2874 if (bitmap_bit_p (components, 0))
2876 int offset = info->lr_save_offset;
2878 offset += info->total_size;
2880 rtx reg = gen_rtx_REG (reg_mode, 0);
2881 rtx_insn *insn = emit_insn (gen_frame_load (reg, ptr_reg, offset));
2883 rtx lr = gen_rtx_REG (Pmode, LR_REGNO);
2884 insn = emit_move_insn (lr, reg);
2885 RTX_FRAME_RELATED_P (insn) = 1;
2886 add_reg_note (insn, REG_CFA_RESTORE, lr);
2890 /* Implement TARGET_SHRINK_WRAP_SET_HANDLED_COMPONENTS. */
2892 rs6000_set_handled_components (sbitmap components)
2894 rs6000_stack_t *info = rs6000_stack_info ();
2896 for (int i = info->first_gp_reg_save; i < 32; i++)
2897 if (bitmap_bit_p (components, i))
2898 cfun->machine->gpr_is_wrapped_separately[i] = true;
2900 for (int i = info->first_fp_reg_save; i < 64; i++)
2901 if (bitmap_bit_p (components, i))
2902 cfun->machine->fpr_is_wrapped_separately[i - 32] = true;
2904 if (bitmap_bit_p (components, 0))
2905 cfun->machine->lr_is_wrapped_separately = true;
2907 if (bitmap_bit_p (components, 2))
2908 cfun->machine->toc_is_wrapped_separately = true;
2911 /* VRSAVE is a bit vector representing which AltiVec registers
2912 are used. The OS uses this to determine which vector
2913 registers to save on a context switch. We need to save
2914 VRSAVE on the stack frame, add whatever AltiVec registers we
2915 used in this function, and do the corresponding magic in the
2918 emit_vrsave_prologue (rs6000_stack_t *info, int save_regno,
2919 HOST_WIDE_INT frame_off, rtx frame_reg_rtx)
2921 /* Get VRSAVE into a GPR. */
2922 rtx reg = gen_rtx_REG (SImode, save_regno);
2923 rtx vrsave = gen_rtx_REG (SImode, VRSAVE_REGNO);
2925 emit_insn (gen_get_vrsave_internal (reg));
2927 emit_insn (gen_rtx_SET (reg, vrsave));
2930 int offset = info->vrsave_save_offset + frame_off;
2931 emit_insn (gen_frame_store (reg, frame_reg_rtx, offset));
2933 /* Include the registers in the mask. */
2934 emit_insn (gen_iorsi3 (reg, reg, GEN_INT (info->vrsave_mask)));
2936 emit_insn (generate_set_vrsave (reg, info, 0));
2939 /* Set up the arg pointer (r12) for -fsplit-stack code. If __morestack was
2940 called, it left the arg pointer to the old stack in r29. Otherwise, the
2941 arg pointer is the top of the current frame. */
2943 emit_split_stack_prologue (rs6000_stack_t *info, rtx_insn *sp_adjust,
2944 HOST_WIDE_INT frame_off, rtx frame_reg_rtx)
2946 cfun->machine->split_stack_argp_used = true;
2950 rtx r12 = gen_rtx_REG (Pmode, 12);
2951 rtx sp_reg_rtx = gen_rtx_REG (Pmode, STACK_POINTER_REGNUM);
2952 rtx set_r12 = gen_rtx_SET (r12, sp_reg_rtx);
2953 emit_insn_before (set_r12, sp_adjust);
2955 else if (frame_off != 0 || REGNO (frame_reg_rtx) != 12)
2957 rtx r12 = gen_rtx_REG (Pmode, 12);
2959 emit_move_insn (r12, frame_reg_rtx);
2961 emit_insn (gen_add3_insn (r12, frame_reg_rtx, GEN_INT (frame_off)));
2966 rtx r12 = gen_rtx_REG (Pmode, 12);
2967 rtx r29 = gen_rtx_REG (Pmode, 29);
2968 rtx cr7 = gen_rtx_REG (CCUNSmode, CR7_REGNO);
2969 rtx not_more = gen_label_rtx ();
2972 jump = gen_rtx_IF_THEN_ELSE (VOIDmode,
2973 gen_rtx_GEU (VOIDmode, cr7, const0_rtx),
2974 gen_rtx_LABEL_REF (VOIDmode, not_more),
2976 jump = emit_jump_insn (gen_rtx_SET (pc_rtx, jump));
2977 JUMP_LABEL (jump) = not_more;
2978 LABEL_NUSES (not_more) += 1;
2979 emit_move_insn (r12, r29);
2980 emit_label (not_more);
2984 /* Emit function prologue as insns. */
2987 rs6000_emit_prologue (void)
2989 rs6000_stack_t *info = rs6000_stack_info ();
2990 machine_mode reg_mode = Pmode;
2991 int reg_size = TARGET_32BIT ? 4 : 8;
2992 machine_mode fp_reg_mode = TARGET_HARD_FLOAT ? DFmode : SFmode;
2993 int fp_reg_size = 8;
2994 rtx sp_reg_rtx = gen_rtx_REG (Pmode, STACK_POINTER_REGNUM);
2995 rtx frame_reg_rtx = sp_reg_rtx;
2996 unsigned int cr_save_regno;
2997 rtx cr_save_rtx = NULL_RTX;
3000 int using_static_chain_p = (cfun->static_chain_decl != NULL_TREE
3001 && df_regs_ever_live_p (STATIC_CHAIN_REGNUM)
3002 && call_used_regs[STATIC_CHAIN_REGNUM]);
3003 int using_split_stack = (flag_split_stack
3004 && (lookup_attribute ("no_split_stack",
3005 DECL_ATTRIBUTES (cfun->decl))
3008 /* Offset to top of frame for frame_reg and sp respectively. */
3009 HOST_WIDE_INT frame_off = 0;
3010 HOST_WIDE_INT sp_off = 0;
3011 /* sp_adjust is the stack adjusting instruction, tracked so that the
3012 insn setting up the split-stack arg pointer can be emitted just
3013 prior to it, when r12 is not used here for other purposes. */
3014 rtx_insn *sp_adjust = 0;
3017 /* Track and check usage of r0, r11, r12. */
3018 int reg_inuse = using_static_chain_p ? 1 << 11 : 0;
3019 #define START_USE(R) do \
3021 gcc_assert ((reg_inuse & (1 << (R))) == 0); \
3022 reg_inuse |= 1 << (R); \
3024 #define END_USE(R) do \
3026 gcc_assert ((reg_inuse & (1 << (R))) != 0); \
3027 reg_inuse &= ~(1 << (R)); \
3029 #define NOT_INUSE(R) do \
3031 gcc_assert ((reg_inuse & (1 << (R))) == 0); \
3034 #define START_USE(R) do {} while (0)
3035 #define END_USE(R) do {} while (0)
3036 #define NOT_INUSE(R) do {} while (0)
3039 if (DEFAULT_ABI == ABI_ELFv2
3040 && !TARGET_SINGLE_PIC_BASE)
3042 cfun->machine->r2_setup_needed = df_regs_ever_live_p (TOC_REGNUM);
3044 /* With -mminimal-toc we may generate an extra use of r2 below. */
3045 if (TARGET_TOC && TARGET_MINIMAL_TOC
3046 && !constant_pool_empty_p ())
3047 cfun->machine->r2_setup_needed = true;
3051 if (flag_stack_usage_info)
3052 current_function_static_stack_size = info->total_size;
3054 if (flag_stack_check == STATIC_BUILTIN_STACK_CHECK)
3056 HOST_WIDE_INT size = info->total_size;
3058 if (crtl->is_leaf && !cfun->calls_alloca)
3060 if (size > PROBE_INTERVAL && size > get_stack_check_protect ())
3061 rs6000_emit_probe_stack_range (get_stack_check_protect (),
3062 size - get_stack_check_protect ());
3065 rs6000_emit_probe_stack_range (get_stack_check_protect (), size);
3068 if (TARGET_FIX_AND_CONTINUE)
3070 /* gdb on darwin arranges to forward a function from the old
3071 address by modifying the first 5 instructions of the function
3072 to branch to the overriding function. This is necessary to
3073 permit function pointers that point to the old function to
3074 actually forward to the new function. */
3075 emit_insn (gen_nop ());
3076 emit_insn (gen_nop ());
3077 emit_insn (gen_nop ());
3078 emit_insn (gen_nop ());
3079 emit_insn (gen_nop ());
3082 /* Handle world saves specially here. */
3083 if (WORLD_SAVE_P (info))
3090 /* save_world expects lr in r0. */
3091 reg0 = gen_rtx_REG (Pmode, 0);
3092 if (info->lr_save_p)
3094 insn = emit_move_insn (reg0,
3095 gen_rtx_REG (Pmode, LR_REGNO));
3096 RTX_FRAME_RELATED_P (insn) = 1;
3099 /* The SAVE_WORLD and RESTORE_WORLD routines make a number of
3100 assumptions about the offsets of various bits of the stack
3102 gcc_assert (info->gp_save_offset == -220
3103 && info->fp_save_offset == -144
3104 && info->lr_save_offset == 8
3105 && info->cr_save_offset == 4
3108 && (!crtl->calls_eh_return
3109 || info->ehrd_offset == -432)
3110 && info->vrsave_save_offset == -224
3111 && info->altivec_save_offset == -416);
3113 treg = gen_rtx_REG (SImode, 11);
3114 emit_move_insn (treg, GEN_INT (-info->total_size));
3116 /* SAVE_WORLD takes the caller's LR in R0 and the frame size
3117 in R11. It also clobbers R12, so beware! */
3119 /* Preserve CR2 for save_world prologues */
3121 sz += 32 - info->first_gp_reg_save;
3122 sz += 64 - info->first_fp_reg_save;
3123 sz += LAST_ALTIVEC_REGNO - info->first_altivec_reg_save + 1;
3124 p = rtvec_alloc (sz);
3126 RTVEC_ELT (p, j++) = gen_hard_reg_clobber (SImode, LR_REGNO);
3127 RTVEC_ELT (p, j++) = gen_rtx_USE (VOIDmode,
3128 gen_rtx_SYMBOL_REF (Pmode,
3130 /* We do floats first so that the instruction pattern matches
3132 for (i = 0; i < 64 - info->first_fp_reg_save; i++)
3134 = gen_frame_store (gen_rtx_REG (TARGET_HARD_FLOAT ? DFmode : SFmode,
3135 info->first_fp_reg_save + i),
3137 info->fp_save_offset + frame_off + 8 * i);
3138 for (i = 0; info->first_altivec_reg_save + i <= LAST_ALTIVEC_REGNO; i++)
3140 = gen_frame_store (gen_rtx_REG (V4SImode,
3141 info->first_altivec_reg_save + i),
3143 info->altivec_save_offset + frame_off + 16 * i);
3144 for (i = 0; i < 32 - info->first_gp_reg_save; i++)
3146 = gen_frame_store (gen_rtx_REG (reg_mode, info->first_gp_reg_save + i),
3148 info->gp_save_offset + frame_off + reg_size * i);
3150 /* CR register traditionally saved as CR2. */
3152 = gen_frame_store (gen_rtx_REG (SImode, CR2_REGNO),
3153 frame_reg_rtx, info->cr_save_offset + frame_off);
3154 /* Explain about use of R0. */
3155 if (info->lr_save_p)
3157 = gen_frame_store (reg0,
3158 frame_reg_rtx, info->lr_save_offset + frame_off);
3159 /* Explain what happens to the stack pointer. */
3161 rtx newval = gen_rtx_PLUS (Pmode, sp_reg_rtx, treg);
3162 RTVEC_ELT (p, j++) = gen_rtx_SET (sp_reg_rtx, newval);
3165 insn = emit_insn (gen_rtx_PARALLEL (VOIDmode, p));
3166 rs6000_frame_related (insn, frame_reg_rtx, sp_off - frame_off,
3167 treg, GEN_INT (-info->total_size));
3168 sp_off = frame_off = info->total_size;
3171 strategy = info->savres_strategy;
3173 /* For V.4, update stack before we do any saving and set back pointer. */
3174 if (! WORLD_SAVE_P (info)
3176 && (DEFAULT_ABI == ABI_V4
3177 || crtl->calls_eh_return))
3179 bool need_r11 = (!(strategy & SAVE_INLINE_FPRS)
3180 || !(strategy & SAVE_INLINE_GPRS)
3181 || !(strategy & SAVE_INLINE_VRS));
3183 rtx ptr_reg = NULL_RTX;
3186 if (info->total_size < 32767)
3187 frame_off = info->total_size;
3190 else if (info->cr_save_p
3192 || info->first_fp_reg_save < 64
3193 || info->first_gp_reg_save < 32
3194 || info->altivec_size != 0
3195 || info->vrsave_size != 0
3196 || crtl->calls_eh_return)
3200 /* The prologue won't be saving any regs so there is no need
3201 to set up a frame register to access any frame save area.
3202 We also won't be using frame_off anywhere below, but set
3203 the correct value anyway to protect against future
3204 changes to this function. */
3205 frame_off = info->total_size;
3207 if (ptr_regno != -1)
3209 /* Set up the frame offset to that needed by the first
3210 out-of-line save function. */
3211 START_USE (ptr_regno);
3212 ptr_reg = gen_rtx_REG (Pmode, ptr_regno);
3213 frame_reg_rtx = ptr_reg;
3214 if (!(strategy & SAVE_INLINE_FPRS) && info->fp_size != 0)
3215 gcc_checking_assert (info->fp_save_offset + info->fp_size == 0);
3216 else if (!(strategy & SAVE_INLINE_GPRS) && info->first_gp_reg_save < 32)
3217 ptr_off = info->gp_save_offset + info->gp_size;
3218 else if (!(strategy & SAVE_INLINE_VRS) && info->altivec_size != 0)
3219 ptr_off = info->altivec_save_offset + info->altivec_size;
3220 frame_off = -ptr_off;
3222 sp_adjust = rs6000_emit_allocate_stack (info->total_size,
3224 if (REGNO (frame_reg_rtx) == 12)
3226 sp_off = info->total_size;
3227 if (frame_reg_rtx != sp_reg_rtx)
3228 rs6000_emit_stack_tie (frame_reg_rtx, false);
3231 /* If we use the link register, get it into r0. */
3232 if (!WORLD_SAVE_P (info) && info->lr_save_p
3233 && !cfun->machine->lr_is_wrapped_separately)
3237 reg = gen_rtx_REG (Pmode, 0);
3239 insn = emit_move_insn (reg, gen_rtx_REG (Pmode, LR_REGNO));
3240 RTX_FRAME_RELATED_P (insn) = 1;
3242 if (!(strategy & (SAVE_NOINLINE_GPRS_SAVES_LR
3243 | SAVE_NOINLINE_FPRS_SAVES_LR)))
3245 addr = gen_rtx_PLUS (Pmode, frame_reg_rtx,
3246 GEN_INT (info->lr_save_offset + frame_off));
3247 mem = gen_rtx_MEM (Pmode, addr);
3248 /* This should not be of rs6000_sr_alias_set, because of
3249 __builtin_return_address. */
3251 insn = emit_move_insn (mem, reg);
3252 rs6000_frame_related (insn, frame_reg_rtx, sp_off - frame_off,
3253 NULL_RTX, NULL_RTX);
3258 /* If we need to save CR, put it into r12 or r11. Choose r12 except when
3259 r12 will be needed by out-of-line gpr save. */
3260 cr_save_regno = ((DEFAULT_ABI == ABI_AIX || DEFAULT_ABI == ABI_ELFv2)
3261 && !(strategy & (SAVE_INLINE_GPRS
3262 | SAVE_NOINLINE_GPRS_SAVES_LR))
3264 if (!WORLD_SAVE_P (info)
3266 && REGNO (frame_reg_rtx) != cr_save_regno
3267 && !(using_static_chain_p && cr_save_regno == 11)
3268 && !(using_split_stack && cr_save_regno == 12 && sp_adjust))
3270 cr_save_rtx = gen_rtx_REG (SImode, cr_save_regno);
3271 START_USE (cr_save_regno);
3272 rs6000_emit_prologue_move_from_cr (cr_save_rtx);
3275 /* Do any required saving of fpr's. If only one or two to save, do
3276 it ourselves. Otherwise, call function. */
3277 if (!WORLD_SAVE_P (info) && (strategy & SAVE_INLINE_FPRS))
3279 int offset = info->fp_save_offset + frame_off;
3280 for (int i = info->first_fp_reg_save; i < 64; i++)
3283 && !cfun->machine->fpr_is_wrapped_separately[i - 32])
3284 emit_frame_save (frame_reg_rtx, fp_reg_mode, i, offset,
3285 sp_off - frame_off);
3287 offset += fp_reg_size;
3290 else if (!WORLD_SAVE_P (info) && info->first_fp_reg_save != 64)
3292 bool lr = (strategy & SAVE_NOINLINE_FPRS_SAVES_LR) != 0;
3293 int sel = SAVRES_SAVE | SAVRES_FPR | (lr ? SAVRES_LR : 0);
3294 unsigned ptr_regno = ptr_regno_for_savres (sel);
3295 rtx ptr_reg = frame_reg_rtx;
3297 if (REGNO (frame_reg_rtx) == ptr_regno)
3298 gcc_checking_assert (frame_off == 0);
3301 ptr_reg = gen_rtx_REG (Pmode, ptr_regno);
3302 NOT_INUSE (ptr_regno);
3303 emit_insn (gen_add3_insn (ptr_reg,
3304 frame_reg_rtx, GEN_INT (frame_off)));
3306 insn = rs6000_emit_savres_rtx (info, ptr_reg,
3307 info->fp_save_offset,
3308 info->lr_save_offset,
3310 rs6000_frame_related (insn, ptr_reg, sp_off,
3311 NULL_RTX, NULL_RTX);
3316 /* Save GPRs. This is done as a PARALLEL if we are using
3317 the store-multiple instructions. */
3318 if (!WORLD_SAVE_P (info) && !(strategy & SAVE_INLINE_GPRS))
3320 bool lr = (strategy & SAVE_NOINLINE_GPRS_SAVES_LR) != 0;
3321 int sel = SAVRES_SAVE | SAVRES_GPR | (lr ? SAVRES_LR : 0);
3322 unsigned ptr_regno = ptr_regno_for_savres (sel);
3323 rtx ptr_reg = frame_reg_rtx;
3324 bool ptr_set_up = REGNO (ptr_reg) == ptr_regno;
3325 int end_save = info->gp_save_offset + info->gp_size;
3328 if (ptr_regno == 12)
3331 ptr_reg = gen_rtx_REG (Pmode, ptr_regno);
3333 /* Need to adjust r11 (r12) if we saved any FPRs. */
3334 if (end_save + frame_off != 0)
3336 rtx offset = GEN_INT (end_save + frame_off);
3339 frame_off = -end_save;
3341 NOT_INUSE (ptr_regno);
3342 emit_insn (gen_add3_insn (ptr_reg, frame_reg_rtx, offset));
3344 else if (!ptr_set_up)
3346 NOT_INUSE (ptr_regno);
3347 emit_move_insn (ptr_reg, frame_reg_rtx);
3349 ptr_off = -end_save;
3350 insn = rs6000_emit_savres_rtx (info, ptr_reg,
3351 info->gp_save_offset + ptr_off,
3352 info->lr_save_offset + ptr_off,
3354 rs6000_frame_related (insn, ptr_reg, sp_off - ptr_off,
3355 NULL_RTX, NULL_RTX);
3359 else if (!WORLD_SAVE_P (info) && (strategy & SAVE_MULTIPLE))
3363 p = rtvec_alloc (32 - info->first_gp_reg_save);
3364 for (i = 0; i < 32 - info->first_gp_reg_save; i++)
3366 = gen_frame_store (gen_rtx_REG (reg_mode, info->first_gp_reg_save + i),
3368 info->gp_save_offset + frame_off + reg_size * i);
3369 insn = emit_insn (gen_rtx_PARALLEL (VOIDmode, p));
3370 rs6000_frame_related (insn, frame_reg_rtx, sp_off - frame_off,
3371 NULL_RTX, NULL_RTX);
3373 else if (!WORLD_SAVE_P (info))
3375 int offset = info->gp_save_offset + frame_off;
3376 for (int i = info->first_gp_reg_save; i < 32; i++)
3379 && !cfun->machine->gpr_is_wrapped_separately[i])
3380 emit_frame_save (frame_reg_rtx, reg_mode, i, offset,
3381 sp_off - frame_off);
3387 if (crtl->calls_eh_return)
3394 unsigned int regno = EH_RETURN_DATA_REGNO (i);
3395 if (regno == INVALID_REGNUM)
3399 p = rtvec_alloc (i);
3403 unsigned int regno = EH_RETURN_DATA_REGNO (i);
3404 if (regno == INVALID_REGNUM)
3408 = gen_frame_store (gen_rtx_REG (reg_mode, regno),
3410 info->ehrd_offset + sp_off + reg_size * (int) i);
3411 RTVEC_ELT (p, i) = set;
3412 RTX_FRAME_RELATED_P (set) = 1;
3415 insn = emit_insn (gen_blockage ());
3416 RTX_FRAME_RELATED_P (insn) = 1;
3417 add_reg_note (insn, REG_FRAME_RELATED_EXPR, gen_rtx_PARALLEL (VOIDmode, p));
3420 /* In AIX ABI we need to make sure r2 is really saved. */
3421 if (TARGET_AIX && crtl->calls_eh_return)
3423 rtx tmp_reg, tmp_reg_si, hi, lo, compare_result, toc_save_done, jump;
3424 rtx join_insn, note;
3425 rtx_insn *save_insn;
3426 long toc_restore_insn;
3428 tmp_reg = gen_rtx_REG (Pmode, 11);
3429 tmp_reg_si = gen_rtx_REG (SImode, 11);
3430 if (using_static_chain_p)
3433 emit_move_insn (gen_rtx_REG (Pmode, 0), tmp_reg);
3437 emit_move_insn (tmp_reg, gen_rtx_REG (Pmode, LR_REGNO));
3438 /* Peek at instruction to which this function returns. If it's
3439 restoring r2, then we know we've already saved r2. We can't
3440 unconditionally save r2 because the value we have will already
3441 be updated if we arrived at this function via a plt call or
3442 toc adjusting stub. */
3443 emit_move_insn (tmp_reg_si, gen_rtx_MEM (SImode, tmp_reg));
3444 toc_restore_insn = ((TARGET_32BIT ? 0x80410000 : 0xE8410000)
3445 + RS6000_TOC_SAVE_SLOT);
3446 hi = gen_int_mode (toc_restore_insn & ~0xffff, SImode);
3447 emit_insn (gen_xorsi3 (tmp_reg_si, tmp_reg_si, hi));
3448 compare_result = gen_rtx_REG (CCUNSmode, CR0_REGNO);
3449 validate_condition_mode (EQ, CCUNSmode);
3450 lo = gen_int_mode (toc_restore_insn & 0xffff, SImode);
3451 emit_insn (gen_rtx_SET (compare_result,
3452 gen_rtx_COMPARE (CCUNSmode, tmp_reg_si, lo)));
3453 toc_save_done = gen_label_rtx ();
3454 jump = gen_rtx_IF_THEN_ELSE (VOIDmode,
3455 gen_rtx_EQ (VOIDmode, compare_result,
3457 gen_rtx_LABEL_REF (VOIDmode, toc_save_done),
3459 jump = emit_jump_insn (gen_rtx_SET (pc_rtx, jump));
3460 JUMP_LABEL (jump) = toc_save_done;
3461 LABEL_NUSES (toc_save_done) += 1;
3463 save_insn = emit_frame_save (frame_reg_rtx, reg_mode,
3464 TOC_REGNUM, frame_off + RS6000_TOC_SAVE_SLOT,
3465 sp_off - frame_off);
3467 emit_label (toc_save_done);
3469 /* ??? If we leave SAVE_INSN as marked as saving R2, then we'll
3470 have a CFG that has different saves along different paths.
3471 Move the note to a dummy blockage insn, which describes that
3472 R2 is unconditionally saved after the label. */
3473 /* ??? An alternate representation might be a special insn pattern
3474 containing both the branch and the store. That might let the
3475 code that minimizes the number of DW_CFA_advance opcodes better
3476 freedom in placing the annotations. */
3477 note = find_reg_note (save_insn, REG_FRAME_RELATED_EXPR, NULL);
3479 remove_note (save_insn, note);
3481 note = alloc_reg_note (REG_FRAME_RELATED_EXPR,
3482 copy_rtx (PATTERN (save_insn)), NULL_RTX);
3483 RTX_FRAME_RELATED_P (save_insn) = 0;
3485 join_insn = emit_insn (gen_blockage ());
3486 REG_NOTES (join_insn) = note;
3487 RTX_FRAME_RELATED_P (join_insn) = 1;
3489 if (using_static_chain_p)
3491 emit_move_insn (tmp_reg, gen_rtx_REG (Pmode, 0));
3498 /* Save CR if we use any that must be preserved. */
3499 if (!WORLD_SAVE_P (info) && info->cr_save_p)
3501 rtx addr = gen_rtx_PLUS (Pmode, frame_reg_rtx,
3502 GEN_INT (info->cr_save_offset + frame_off));
3503 rtx mem = gen_frame_mem (SImode, addr);
3505 /* If we didn't copy cr before, do so now using r0. */
3506 if (cr_save_rtx == NULL_RTX)
3509 cr_save_rtx = gen_rtx_REG (SImode, 0);
3510 rs6000_emit_prologue_move_from_cr (cr_save_rtx);
3513 /* Saving CR requires a two-instruction sequence: one instruction
3514 to move the CR to a general-purpose register, and a second
3515 instruction that stores the GPR to memory.
3517 We do not emit any DWARF CFI records for the first of these,
3518 because we cannot properly represent the fact that CR is saved in
3519 a register. One reason is that we cannot express that multiple
3520 CR fields are saved; another reason is that on 64-bit, the size
3521 of the CR register in DWARF (4 bytes) differs from the size of
3522 a general-purpose register.
3524 This means if any intervening instruction were to clobber one of
3525 the call-saved CR fields, we'd have incorrect CFI. To prevent
3526 this from happening, we mark the store to memory as a use of
3527 those CR fields, which prevents any such instruction from being
3528 scheduled in between the two instructions. */
3533 crsave_v[n_crsave++] = gen_rtx_SET (mem, cr_save_rtx);
3534 for (i = 0; i < 8; i++)
3535 if (save_reg_p (CR0_REGNO + i))
3536 crsave_v[n_crsave++]
3537 = gen_rtx_USE (VOIDmode, gen_rtx_REG (CCmode, CR0_REGNO + i));
3539 insn = emit_insn (gen_rtx_PARALLEL (VOIDmode,
3540 gen_rtvec_v (n_crsave, crsave_v)));
3541 END_USE (REGNO (cr_save_rtx));
3543 /* Now, there's no way that dwarf2out_frame_debug_expr is going to
3544 understand '(unspec:SI [(reg:CC 68) ...] UNSPEC_MOVESI_FROM_CR)',
3545 so we need to construct a frame expression manually. */
3546 RTX_FRAME_RELATED_P (insn) = 1;
3548 /* Update address to be stack-pointer relative, like
3549 rs6000_frame_related would do. */
3550 addr = gen_rtx_PLUS (Pmode, gen_rtx_REG (Pmode, STACK_POINTER_REGNUM),
3551 GEN_INT (info->cr_save_offset + sp_off));
3552 mem = gen_frame_mem (SImode, addr);
3554 if (DEFAULT_ABI == ABI_ELFv2)
3556 /* In the ELFv2 ABI we generate separate CFI records for each
3557 CR field that was actually saved. They all point to the
3558 same 32-bit stack slot. */
3562 for (i = 0; i < 8; i++)
3563 if (save_reg_p (CR0_REGNO + i))
3566 = gen_rtx_SET (mem, gen_rtx_REG (SImode, CR0_REGNO + i));
3568 RTX_FRAME_RELATED_P (crframe[n_crframe]) = 1;
3572 add_reg_note (insn, REG_FRAME_RELATED_EXPR,
3573 gen_rtx_PARALLEL (VOIDmode,
3574 gen_rtvec_v (n_crframe, crframe)));
3578 /* In other ABIs, by convention, we use a single CR regnum to
3579 represent the fact that all call-saved CR fields are saved.
3580 We use CR2_REGNO to be compatible with gcc-2.95 on Linux. */
3581 rtx set = gen_rtx_SET (mem, gen_rtx_REG (SImode, CR2_REGNO));
3582 add_reg_note (insn, REG_FRAME_RELATED_EXPR, set);
3586 /* In the ELFv2 ABI we need to save all call-saved CR fields into
3587 *separate* slots if the routine calls __builtin_eh_return, so
3588 that they can be independently restored by the unwinder. */
3589 if (DEFAULT_ABI == ABI_ELFv2 && crtl->calls_eh_return)
3591 int i, cr_off = info->ehcr_offset;
3594 /* ??? We might get better performance by using multiple mfocrf
3596 crsave = gen_rtx_REG (SImode, 0);
3597 emit_insn (gen_prologue_movesi_from_cr (crsave));
3599 for (i = 0; i < 8; i++)
3600 if (!call_used_regs[CR0_REGNO + i])
3602 rtvec p = rtvec_alloc (2);
3604 = gen_frame_store (crsave, frame_reg_rtx, cr_off + frame_off);
3606 = gen_rtx_USE (VOIDmode, gen_rtx_REG (CCmode, CR0_REGNO + i));
3608 insn = emit_insn (gen_rtx_PARALLEL (VOIDmode, p));
3610 RTX_FRAME_RELATED_P (insn) = 1;
3611 add_reg_note (insn, REG_FRAME_RELATED_EXPR,
3612 gen_frame_store (gen_rtx_REG (SImode, CR0_REGNO + i),
3613 sp_reg_rtx, cr_off + sp_off));
3619 /* If we are emitting stack probes, but allocate no stack, then
3620 just note that in the dump file. */
3621 if (flag_stack_clash_protection
3624 dump_stack_clash_frame_info (NO_PROBE_NO_FRAME, false);
3626 /* Update stack and set back pointer unless this is V.4,
3627 for which it was done previously. */
3628 if (!WORLD_SAVE_P (info) && info->push_p
3629 && !(DEFAULT_ABI == ABI_V4 || crtl->calls_eh_return))
3634 /* If saving altivec regs we need to be able to address all save
3635 locations using a 16-bit offset. */
3636 if ((strategy & SAVE_INLINE_VRS) == 0
3637 || (info->altivec_size != 0
3638 && (info->altivec_save_offset + info->altivec_size - 16
3639 + info->total_size - frame_off) > 32767)
3640 || (info->vrsave_size != 0
3641 && (info->vrsave_save_offset
3642 + info->total_size - frame_off) > 32767))
3644 int sel = SAVRES_SAVE | SAVRES_VR;
3645 unsigned ptr_regno = ptr_regno_for_savres (sel);
3647 if (using_static_chain_p
3648 && ptr_regno == STATIC_CHAIN_REGNUM)
3650 if (REGNO (frame_reg_rtx) != ptr_regno)
3651 START_USE (ptr_regno);
3652 ptr_reg = gen_rtx_REG (Pmode, ptr_regno);
3653 frame_reg_rtx = ptr_reg;
3654 ptr_off = info->altivec_save_offset + info->altivec_size;
3655 frame_off = -ptr_off;
3657 else if (REGNO (frame_reg_rtx) == 1)
3658 frame_off = info->total_size;
3659 sp_adjust = rs6000_emit_allocate_stack (info->total_size,
3661 if (REGNO (frame_reg_rtx) == 12)
3663 sp_off = info->total_size;
3664 if (frame_reg_rtx != sp_reg_rtx)
3665 rs6000_emit_stack_tie (frame_reg_rtx, false);
3668 /* Set frame pointer, if needed. */
3669 if (frame_pointer_needed)
3671 insn = emit_move_insn (gen_rtx_REG (Pmode, HARD_FRAME_POINTER_REGNUM),
3673 RTX_FRAME_RELATED_P (insn) = 1;
3676 /* Save AltiVec registers if needed. Save here because the red zone does
3677 not always include AltiVec registers. */
3678 if (!WORLD_SAVE_P (info)
3679 && info->altivec_size != 0 && (strategy & SAVE_INLINE_VRS) == 0)
3681 int end_save = info->altivec_save_offset + info->altivec_size;
3683 /* Oddly, the vector save/restore functions point r0 at the end
3684 of the save area, then use r11 or r12 to load offsets for
3685 [reg+reg] addressing. */
3686 rtx ptr_reg = gen_rtx_REG (Pmode, 0);
3687 int scratch_regno = ptr_regno_for_savres (SAVRES_SAVE | SAVRES_VR);
3688 rtx scratch_reg = gen_rtx_REG (Pmode, scratch_regno);
3690 gcc_checking_assert (scratch_regno == 11 || scratch_regno == 12);
3692 if (scratch_regno == 12)
3694 if (end_save + frame_off != 0)
3696 rtx offset = GEN_INT (end_save + frame_off);
3698 emit_insn (gen_add3_insn (ptr_reg, frame_reg_rtx, offset));
3701 emit_move_insn (ptr_reg, frame_reg_rtx);
3703 ptr_off = -end_save;
3704 insn = rs6000_emit_savres_rtx (info, scratch_reg,
3705 info->altivec_save_offset + ptr_off,
3706 0, V4SImode, SAVRES_SAVE | SAVRES_VR);
3707 rs6000_frame_related (insn, scratch_reg, sp_off - ptr_off,
3708 NULL_RTX, NULL_RTX);
3709 if (REGNO (frame_reg_rtx) == REGNO (scratch_reg))
3711 /* The oddity mentioned above clobbered our frame reg. */
3712 emit_move_insn (frame_reg_rtx, ptr_reg);
3713 frame_off = ptr_off;
3716 else if (!WORLD_SAVE_P (info)
3717 && info->altivec_size != 0)
3721 for (i = info->first_altivec_reg_save; i <= LAST_ALTIVEC_REGNO; ++i)
3722 if (info->vrsave_mask & ALTIVEC_REG_BIT (i))
3724 rtx areg, savereg, mem;
3725 HOST_WIDE_INT offset;
3727 offset = (info->altivec_save_offset + frame_off
3728 + 16 * (i - info->first_altivec_reg_save));
3730 savereg = gen_rtx_REG (V4SImode, i);
3732 if (TARGET_P9_VECTOR && quad_address_offset_p (offset))
3734 mem = gen_frame_mem (V4SImode,
3735 gen_rtx_PLUS (Pmode, frame_reg_rtx,
3737 insn = emit_insn (gen_rtx_SET (mem, savereg));
3743 areg = gen_rtx_REG (Pmode, 0);
3744 emit_move_insn (areg, GEN_INT (offset));
3746 /* AltiVec addressing mode is [reg+reg]. */
3747 mem = gen_frame_mem (V4SImode,
3748 gen_rtx_PLUS (Pmode, frame_reg_rtx, areg));
3750 /* Rather than emitting a generic move, force use of the stvx
3751 instruction, which we always want on ISA 2.07 (power8) systems.
3752 In particular we don't want xxpermdi/stxvd2x for little
3754 insn = emit_insn (gen_altivec_stvx_v4si_internal (mem, savereg));
3757 rs6000_frame_related (insn, frame_reg_rtx, sp_off - frame_off,
3758 areg, GEN_INT (offset));
3762 /* VRSAVE is a bit vector representing which AltiVec registers
3763 are used. The OS uses this to determine which vector
3764 registers to save on a context switch. We need to save
3765 VRSAVE on the stack frame, add whatever AltiVec registers we
3766 used in this function, and do the corresponding magic in the
3769 if (!WORLD_SAVE_P (info) && info->vrsave_size != 0)
3771 /* Get VRSAVE into a GPR. Note that ABI_V4 and ABI_DARWIN might
3772 be using r12 as frame_reg_rtx and r11 as the static chain
3773 pointer for nested functions. */
3774 int save_regno = 12;
3775 if ((DEFAULT_ABI == ABI_AIX || DEFAULT_ABI == ABI_ELFv2)
3776 && !using_static_chain_p)
3778 else if (using_split_stack || REGNO (frame_reg_rtx) == 12)
3781 if (using_static_chain_p)
3784 NOT_INUSE (save_regno);
3786 emit_vrsave_prologue (info, save_regno, frame_off, frame_reg_rtx);
3789 /* If we are using RS6000_PIC_OFFSET_TABLE_REGNUM, we need to set it up. */
3790 if (!TARGET_SINGLE_PIC_BASE
3791 && ((TARGET_TOC && TARGET_MINIMAL_TOC
3792 && !constant_pool_empty_p ())
3793 || (DEFAULT_ABI == ABI_V4
3794 && (flag_pic == 1 || (flag_pic && TARGET_SECURE_PLT))
3795 && df_regs_ever_live_p (RS6000_PIC_OFFSET_TABLE_REGNUM))))
3797 /* If emit_load_toc_table will use the link register, we need to save
3798 it. We use R12 for this purpose because emit_load_toc_table
3799 can use register 0. This allows us to use a plain 'blr' to return
3800 from the procedure more often. */
3801 int save_LR_around_toc_setup = (TARGET_ELF
3802 && DEFAULT_ABI == ABI_V4
3804 && ! info->lr_save_p
3805 && EDGE_COUNT (EXIT_BLOCK_PTR_FOR_FN (cfun)->preds) > 0);
3806 if (save_LR_around_toc_setup)
3808 rtx lr = gen_rtx_REG (Pmode, LR_REGNO);
3809 rtx tmp = gen_rtx_REG (Pmode, 12);
3812 insn = emit_move_insn (tmp, lr);
3813 RTX_FRAME_RELATED_P (insn) = 1;
3815 rs6000_emit_load_toc_table (TRUE);
3817 insn = emit_move_insn (lr, tmp);
3818 add_reg_note (insn, REG_CFA_RESTORE, lr);
3819 RTX_FRAME_RELATED_P (insn) = 1;
3822 rs6000_emit_load_toc_table (TRUE);
3826 if (!TARGET_SINGLE_PIC_BASE
3827 && DEFAULT_ABI == ABI_DARWIN
3828 && flag_pic && crtl->uses_pic_offset_table)
3830 rtx lr = gen_rtx_REG (Pmode, LR_REGNO);
3831 rtx src = gen_rtx_SYMBOL_REF (Pmode, MACHOPIC_FUNCTION_BASE_NAME);
3833 /* Save and restore LR locally around this call (in R0). */
3834 if (!info->lr_save_p)
3835 emit_move_insn (gen_rtx_REG (Pmode, 0), lr);
3837 emit_insn (gen_load_macho_picbase (src));
3839 emit_move_insn (gen_rtx_REG (Pmode,
3840 RS6000_PIC_OFFSET_TABLE_REGNUM),
3843 if (!info->lr_save_p)
3844 emit_move_insn (lr, gen_rtx_REG (Pmode, 0));
3848 /* If we need to, save the TOC register after doing the stack setup.
3849 Do not emit eh frame info for this save. The unwinder wants info,
3850 conceptually attached to instructions in this function, about
3851 register values in the caller of this function. This R2 may have
3852 already been changed from the value in the caller.
3853 We don't attempt to write accurate DWARF EH frame info for R2
3854 because code emitted by gcc for a (non-pointer) function call
3855 doesn't save and restore R2. Instead, R2 is managed out-of-line
3856 by a linker generated plt call stub when the function resides in
3857 a shared library. This behavior is costly to describe in DWARF,
3858 both in terms of the size of DWARF info and the time taken in the
3859 unwinder to interpret it. R2 changes, apart from the
3860 calls_eh_return case earlier in this function, are handled by
3861 linux-unwind.h frob_update_context. */
3862 if (rs6000_save_toc_in_prologue_p ()
3863 && !cfun->machine->toc_is_wrapped_separately)
3865 rtx reg = gen_rtx_REG (reg_mode, TOC_REGNUM);
3866 emit_insn (gen_frame_store (reg, sp_reg_rtx, RS6000_TOC_SAVE_SLOT));
3869 /* Set up the arg pointer (r12) for -fsplit-stack code. */
3870 if (using_split_stack && split_stack_arg_pointer_used_p ())
3871 emit_split_stack_prologue (info, sp_adjust, frame_off, frame_reg_rtx);
3874 /* Output .extern statements for the save/restore routines we use. */
3877 rs6000_output_savres_externs (FILE *file)
3879 rs6000_stack_t *info = rs6000_stack_info ();
3881 if (TARGET_DEBUG_STACK)
3882 debug_stack_info (info);
3884 /* Write .extern for any function we will call to save and restore
3886 if (info->first_fp_reg_save < 64
3891 int regno = info->first_fp_reg_save - 32;
3893 if ((info->savres_strategy & SAVE_INLINE_FPRS) == 0)
3895 bool lr = (info->savres_strategy & SAVE_NOINLINE_FPRS_SAVES_LR) != 0;
3896 int sel = SAVRES_SAVE | SAVRES_FPR | (lr ? SAVRES_LR : 0);
3897 name = rs6000_savres_routine_name (regno, sel);
3898 fprintf (file, "\t.extern %s\n", name);
3900 if ((info->savres_strategy & REST_INLINE_FPRS) == 0)
3902 bool lr = (info->savres_strategy
3903 & REST_NOINLINE_FPRS_DOESNT_RESTORE_LR) == 0;
3904 int sel = SAVRES_FPR | (lr ? SAVRES_LR : 0);
3905 name = rs6000_savres_routine_name (regno, sel);
3906 fprintf (file, "\t.extern %s\n", name);
3911 /* Write function prologue. */
3914 rs6000_output_function_prologue (FILE *file)
3916 if (!cfun->is_thunk)
3918 rs6000_output_savres_externs (file);
3919 #ifdef USING_ELFOS_H
3920 const char *curr_machine = rs6000_machine_from_flags ();
3921 if (rs6000_machine != curr_machine)
3923 rs6000_machine = curr_machine;
3924 emit_asm_machine ();
3929 /* ELFv2 ABI r2 setup code and local entry point. This must follow
3930 immediately after the global entry point label. */
3931 if (rs6000_global_entry_point_prologue_needed_p ())
3933 const char *name = XSTR (XEXP (DECL_RTL (current_function_decl), 0), 0);
3934 (*targetm.asm_out.internal_label) (file, "LCF", rs6000_pic_labelno);
3936 if (TARGET_CMODEL != CMODEL_LARGE)
3938 /* In the small and medium code models, we assume the TOC is less
3939 2 GB away from the text section, so it can be computed via the
3940 following two-instruction sequence. */
3943 ASM_GENERATE_INTERNAL_LABEL (buf, "LCF", rs6000_pic_labelno);
3944 fprintf (file, "0:\taddis 2,12,.TOC.-");
3945 assemble_name (file, buf);
3946 fprintf (file, "@ha\n");
3947 fprintf (file, "\taddi 2,2,.TOC.-");
3948 assemble_name (file, buf);
3949 fprintf (file, "@l\n");
3953 /* In the large code model, we allow arbitrary offsets between the
3954 TOC and the text section, so we have to load the offset from
3955 memory. The data field is emitted directly before the global
3956 entry point in rs6000_elf_declare_function_name. */
3959 #ifdef HAVE_AS_ENTRY_MARKERS
3960 /* If supported by the linker, emit a marker relocation. If the
3961 total code size of the final executable or shared library
3962 happens to fit into 2 GB after all, the linker will replace
3963 this code sequence with the sequence for the small or medium
3965 fprintf (file, "\t.reloc .,R_PPC64_ENTRY\n");
3967 fprintf (file, "\tld 2,");
3968 ASM_GENERATE_INTERNAL_LABEL (buf, "LCL", rs6000_pic_labelno);
3969 assemble_name (file, buf);
3970 fprintf (file, "-");
3971 ASM_GENERATE_INTERNAL_LABEL (buf, "LCF", rs6000_pic_labelno);
3972 assemble_name (file, buf);
3973 fprintf (file, "(12)\n");
3974 fprintf (file, "\tadd 2,2,12\n");
3977 fputs ("\t.localentry\t", file);
3978 assemble_name (file, name);
3979 fputs (",.-", file);
3980 assemble_name (file, name);
3984 else if (rs6000_pcrel_p (cfun))
3986 const char *name = XSTR (XEXP (DECL_RTL (current_function_decl), 0), 0);
3987 /* All functions compiled to use PC-relative addressing will
3988 have a .localentry value of 0 or 1. For now we set it to
3989 1 all the time, indicating that the function may clobber
3990 the TOC register r2. Later we may optimize this by setting
3991 it to 0 if the function is a leaf and does not clobber r2. */
3992 fputs ("\t.localentry\t", file);
3993 assemble_name (file, name);
3994 fputs (",1\n", file);
3997 /* Output -mprofile-kernel code. This needs to be done here instead of
3998 in output_function_profile since it must go after the ELFv2 ABI
3999 local entry point. */
4000 if (TARGET_PROFILE_KERNEL && crtl->profile)
4002 gcc_assert (DEFAULT_ABI == ABI_AIX || DEFAULT_ABI == ABI_ELFv2);
4003 gcc_assert (!TARGET_32BIT);
4005 asm_fprintf (file, "\tmflr %s\n", reg_names[0]);
4007 /* In the ELFv2 ABI we have no compiler stack word. It must be
4008 the resposibility of _mcount to preserve the static chain
4009 register if required. */
4010 if (DEFAULT_ABI != ABI_ELFv2
4011 && cfun->static_chain_decl != NULL)
4013 asm_fprintf (file, "\tstd %s,24(%s)\n",
4014 reg_names[STATIC_CHAIN_REGNUM], reg_names[1]);
4015 fprintf (file, "\tbl %s\n", RS6000_MCOUNT);
4016 asm_fprintf (file, "\tld %s,24(%s)\n",
4017 reg_names[STATIC_CHAIN_REGNUM], reg_names[1]);
4020 fprintf (file, "\tbl %s\n", RS6000_MCOUNT);
4023 rs6000_pic_labelno++;
4026 /* -mprofile-kernel code calls mcount before the function prolog,
4027 so a profiled leaf function should stay a leaf function. */
4029 rs6000_keep_leaf_when_profiled ()
4031 return TARGET_PROFILE_KERNEL;
4034 /* Non-zero if vmx regs are restored before the frame pop, zero if
4035 we restore after the pop when possible. */
4036 #define ALWAYS_RESTORE_ALTIVEC_BEFORE_POP 0
4038 /* Restoring cr is a two step process: loading a reg from the frame
4039 save, then moving the reg to cr. For ABI_V4 we must let the
4040 unwinder know that the stack location is no longer valid at or
4041 before the stack deallocation, but we can't emit a cfa_restore for
4042 cr at the stack deallocation like we do for other registers.
4043 The trouble is that it is possible for the move to cr to be
4044 scheduled after the stack deallocation. So say exactly where cr
4045 is located on each of the two insns. */
4048 load_cr_save (int regno, rtx frame_reg_rtx, int offset, bool exit_func)
4050 rtx mem = gen_frame_mem_offset (SImode, frame_reg_rtx, offset);
4051 rtx reg = gen_rtx_REG (SImode, regno);
4052 rtx_insn *insn = emit_move_insn (reg, mem);
4054 if (!exit_func && DEFAULT_ABI == ABI_V4)
4056 rtx cr = gen_rtx_REG (SImode, CR2_REGNO);
4057 rtx set = gen_rtx_SET (reg, cr);
4059 add_reg_note (insn, REG_CFA_REGISTER, set);
4060 RTX_FRAME_RELATED_P (insn) = 1;
4065 /* Reload CR from REG. */
4068 restore_saved_cr (rtx reg, bool using_mfcr_multiple, bool exit_func)
4073 if (using_mfcr_multiple)
4075 for (i = 0; i < 8; i++)
4076 if (save_reg_p (CR0_REGNO + i))
4081 if (using_mfcr_multiple && count > 1)
4087 p = rtvec_alloc (count);
4090 for (i = 0; i < 8; i++)
4091 if (save_reg_p (CR0_REGNO + i))
4093 rtvec r = rtvec_alloc (2);
4094 RTVEC_ELT (r, 0) = reg;
4095 RTVEC_ELT (r, 1) = GEN_INT (1 << (7-i));
4096 RTVEC_ELT (p, ndx) =
4097 gen_rtx_SET (gen_rtx_REG (CCmode, CR0_REGNO + i),
4098 gen_rtx_UNSPEC (CCmode, r, UNSPEC_MOVESI_TO_CR));
4101 insn = emit_insn (gen_rtx_PARALLEL (VOIDmode, p));
4102 gcc_assert (ndx == count);
4104 /* For the ELFv2 ABI we generate a CFA_RESTORE for each
4105 CR field separately. */
4106 if (!exit_func && DEFAULT_ABI == ABI_ELFv2 && flag_shrink_wrap)
4108 for (i = 0; i < 8; i++)
4109 if (save_reg_p (CR0_REGNO + i))
4110 add_reg_note (insn, REG_CFA_RESTORE,
4111 gen_rtx_REG (SImode, CR0_REGNO + i));
4113 RTX_FRAME_RELATED_P (insn) = 1;
4117 for (i = 0; i < 8; i++)
4118 if (save_reg_p (CR0_REGNO + i))
4120 rtx insn = emit_insn (gen_movsi_to_cr_one
4121 (gen_rtx_REG (CCmode, CR0_REGNO + i), reg));
4123 /* For the ELFv2 ABI we generate a CFA_RESTORE for each
4124 CR field separately, attached to the insn that in fact
4125 restores this particular CR field. */
4126 if (!exit_func && DEFAULT_ABI == ABI_ELFv2 && flag_shrink_wrap)
4128 add_reg_note (insn, REG_CFA_RESTORE,
4129 gen_rtx_REG (SImode, CR0_REGNO + i));
4131 RTX_FRAME_RELATED_P (insn) = 1;
4135 /* For other ABIs, we just generate a single CFA_RESTORE for CR2. */
4136 if (!exit_func && DEFAULT_ABI != ABI_ELFv2
4137 && (DEFAULT_ABI == ABI_V4 || flag_shrink_wrap))
4139 rtx_insn *insn = get_last_insn ();
4140 rtx cr = gen_rtx_REG (SImode, CR2_REGNO);
4142 add_reg_note (insn, REG_CFA_RESTORE, cr);
4143 RTX_FRAME_RELATED_P (insn) = 1;
4147 /* Like cr, the move to lr instruction can be scheduled after the
4148 stack deallocation, but unlike cr, its stack frame save is still
4149 valid. So we only need to emit the cfa_restore on the correct
4153 load_lr_save (int regno, rtx frame_reg_rtx, int offset)
4155 rtx mem = gen_frame_mem_offset (Pmode, frame_reg_rtx, offset);
4156 rtx reg = gen_rtx_REG (Pmode, regno);
4158 emit_move_insn (reg, mem);
4162 restore_saved_lr (int regno, bool exit_func)
4164 rtx reg = gen_rtx_REG (Pmode, regno);
4165 rtx lr = gen_rtx_REG (Pmode, LR_REGNO);
4166 rtx_insn *insn = emit_move_insn (lr, reg);
4168 if (!exit_func && flag_shrink_wrap)
4170 add_reg_note (insn, REG_CFA_RESTORE, lr);
4171 RTX_FRAME_RELATED_P (insn) = 1;
4176 add_crlr_cfa_restore (const rs6000_stack_t *info, rtx cfa_restores)
4178 if (DEFAULT_ABI == ABI_ELFv2)
4181 for (i = 0; i < 8; i++)
4182 if (save_reg_p (CR0_REGNO + i))
4184 rtx cr = gen_rtx_REG (SImode, CR0_REGNO + i);
4185 cfa_restores = alloc_reg_note (REG_CFA_RESTORE, cr,
4189 else if (info->cr_save_p)
4190 cfa_restores = alloc_reg_note (REG_CFA_RESTORE,
4191 gen_rtx_REG (SImode, CR2_REGNO),
4194 if (info->lr_save_p)
4195 cfa_restores = alloc_reg_note (REG_CFA_RESTORE,
4196 gen_rtx_REG (Pmode, LR_REGNO),
4198 return cfa_restores;
4201 /* Return true if OFFSET from stack pointer can be clobbered by signals.
4202 V.4 doesn't have any stack cushion, AIX ABIs have 220 or 288 bytes
4203 below stack pointer not cloberred by signals. */
4206 offset_below_red_zone_p (HOST_WIDE_INT offset)
4208 return offset < (DEFAULT_ABI == ABI_V4
4210 : TARGET_32BIT ? -220 : -288);
4213 /* Append CFA_RESTORES to any existing REG_NOTES on the last insn. */
4216 emit_cfa_restores (rtx cfa_restores)
4218 rtx_insn *insn = get_last_insn ();
4219 rtx *loc = ®_NOTES (insn);
4222 loc = &XEXP (*loc, 1);
4223 *loc = cfa_restores;
4224 RTX_FRAME_RELATED_P (insn) = 1;
4227 /* Emit function epilogue as insns. */
4230 rs6000_emit_epilogue (enum epilogue_type epilogue_type)
4232 HOST_WIDE_INT frame_off = 0;
4233 rtx sp_reg_rtx = gen_rtx_REG (Pmode, 1);
4234 rtx frame_reg_rtx = sp_reg_rtx;
4235 rtx cfa_restores = NULL_RTX;
4237 rtx cr_save_reg = NULL_RTX;
4238 machine_mode reg_mode = Pmode;
4239 int reg_size = TARGET_32BIT ? 4 : 8;
4240 machine_mode fp_reg_mode = TARGET_HARD_FLOAT ? DFmode : SFmode;
4241 int fp_reg_size = 8;
4245 rs6000_stack_t *info = rs6000_stack_info ();
4247 if (epilogue_type == EPILOGUE_TYPE_NORMAL && crtl->calls_eh_return)
4248 epilogue_type = EPILOGUE_TYPE_EH_RETURN;
4250 int strategy = info->savres_strategy;
4251 bool using_load_multiple = !!(strategy & REST_MULTIPLE);
4252 bool restoring_GPRs_inline = !!(strategy & REST_INLINE_GPRS);
4253 bool restoring_FPRs_inline = !!(strategy & REST_INLINE_FPRS);
4254 if (epilogue_type == EPILOGUE_TYPE_SIBCALL)
4256 restoring_GPRs_inline = true;
4257 restoring_FPRs_inline = true;
4260 bool using_mtcr_multiple = (rs6000_tune == PROCESSOR_PPC601
4261 || rs6000_tune == PROCESSOR_PPC603
4262 || rs6000_tune == PROCESSOR_PPC750
4265 /* Restore via the backchain when we have a large frame, since this
4266 is more efficient than an addis, addi pair. The second condition
4267 here will not trigger at the moment; We don't actually need a
4268 frame pointer for alloca, but the generic parts of the compiler
4269 give us one anyway. */
4270 bool use_backchain_to_restore_sp
4271 = (info->total_size + (info->lr_save_p ? info->lr_save_offset : 0) > 32767
4272 || (cfun->calls_alloca && !frame_pointer_needed));
4274 bool restore_lr = (info->lr_save_p
4275 && (restoring_FPRs_inline
4276 || (strategy & REST_NOINLINE_FPRS_DOESNT_RESTORE_LR))
4277 && (restoring_GPRs_inline
4278 || info->first_fp_reg_save < 64)
4279 && !cfun->machine->lr_is_wrapped_separately);
4282 if (WORLD_SAVE_P (info))
4284 gcc_assert (epilogue_type != EPILOGUE_TYPE_SIBCALL);
4286 /* eh_rest_world_r10 will return to the location saved in the LR
4287 stack slot (which is not likely to be our caller.)
4288 Input: R10 -- stack adjustment. Clobbers R0, R11, R12, R7, R8.
4289 rest_world is similar, except any R10 parameter is ignored.
4290 The exception-handling stuff that was here in 2.95 is no
4291 longer necessary. */
4295 + 32 - info->first_gp_reg_save
4296 + LAST_ALTIVEC_REGNO + 1 - info->first_altivec_reg_save
4297 + 63 + 1 - info->first_fp_reg_save);
4300 switch (epilogue_type)
4302 case EPILOGUE_TYPE_NORMAL:
4303 rname = ggc_strdup ("*rest_world");
4306 case EPILOGUE_TYPE_EH_RETURN:
4307 rname = ggc_strdup ("*eh_rest_world_r10");
4315 RTVEC_ELT (p, j++) = ret_rtx;
4317 = gen_rtx_USE (VOIDmode, gen_rtx_SYMBOL_REF (Pmode, rname));
4318 /* The instruction pattern requires a clobber here;
4319 it is shared with the restVEC helper. */
4320 RTVEC_ELT (p, j++) = gen_hard_reg_clobber (Pmode, 11);
4323 /* CR register traditionally saved as CR2. */
4324 rtx reg = gen_rtx_REG (SImode, CR2_REGNO);
4326 = gen_frame_load (reg, frame_reg_rtx, info->cr_save_offset);
4327 if (flag_shrink_wrap)
4329 cfa_restores = alloc_reg_note (REG_CFA_RESTORE,
4330 gen_rtx_REG (Pmode, LR_REGNO),
4332 cfa_restores = alloc_reg_note (REG_CFA_RESTORE, reg, cfa_restores);
4337 for (i = 0; i < 32 - info->first_gp_reg_save; i++)
4339 rtx reg = gen_rtx_REG (reg_mode, info->first_gp_reg_save + i);
4341 = gen_frame_load (reg,
4342 frame_reg_rtx, info->gp_save_offset + reg_size * i);
4343 if (flag_shrink_wrap
4344 && save_reg_p (info->first_gp_reg_save + i))
4345 cfa_restores = alloc_reg_note (REG_CFA_RESTORE, reg, cfa_restores);
4347 for (i = 0; info->first_altivec_reg_save + i <= LAST_ALTIVEC_REGNO; i++)
4349 rtx reg = gen_rtx_REG (V4SImode, info->first_altivec_reg_save + i);
4351 = gen_frame_load (reg,
4352 frame_reg_rtx, info->altivec_save_offset + 16 * i);
4353 if (flag_shrink_wrap
4354 && save_reg_p (info->first_altivec_reg_save + i))
4355 cfa_restores = alloc_reg_note (REG_CFA_RESTORE, reg, cfa_restores);
4357 for (i = 0; info->first_fp_reg_save + i <= 63; i++)
4359 rtx reg = gen_rtx_REG (TARGET_HARD_FLOAT ? DFmode : SFmode,
4360 info->first_fp_reg_save + i);
4362 = gen_frame_load (reg, frame_reg_rtx, info->fp_save_offset + 8 * i);
4363 if (flag_shrink_wrap
4364 && save_reg_p (info->first_fp_reg_save + i))
4365 cfa_restores = alloc_reg_note (REG_CFA_RESTORE, reg, cfa_restores);
4367 RTVEC_ELT (p, j++) = gen_hard_reg_clobber (Pmode, 0);
4368 RTVEC_ELT (p, j++) = gen_hard_reg_clobber (SImode, 12);
4369 RTVEC_ELT (p, j++) = gen_hard_reg_clobber (SImode, 7);
4370 RTVEC_ELT (p, j++) = gen_hard_reg_clobber (SImode, 8);
4372 = gen_rtx_USE (VOIDmode, gen_rtx_REG (SImode, 10));
4373 insn = emit_jump_insn (gen_rtx_PARALLEL (VOIDmode, p));
4375 if (flag_shrink_wrap)
4377 REG_NOTES (insn) = cfa_restores;
4378 add_reg_note (insn, REG_CFA_DEF_CFA, sp_reg_rtx);
4379 RTX_FRAME_RELATED_P (insn) = 1;
4384 /* frame_reg_rtx + frame_off points to the top of this stack frame. */
4386 frame_off = info->total_size;
4388 /* Restore AltiVec registers if we must do so before adjusting the
4390 if (info->altivec_size != 0
4391 && (ALWAYS_RESTORE_ALTIVEC_BEFORE_POP
4392 || (DEFAULT_ABI != ABI_V4
4393 && offset_below_red_zone_p (info->altivec_save_offset))))
4396 int scratch_regno = ptr_regno_for_savres (SAVRES_VR);
4398 gcc_checking_assert (scratch_regno == 11 || scratch_regno == 12);
4399 if (use_backchain_to_restore_sp)
4401 int frame_regno = 11;
4403 if ((strategy & REST_INLINE_VRS) == 0)
4405 /* Of r11 and r12, select the one not clobbered by an
4406 out-of-line restore function for the frame register. */
4407 frame_regno = 11 + 12 - scratch_regno;
4409 frame_reg_rtx = gen_rtx_REG (Pmode, frame_regno);
4410 emit_move_insn (frame_reg_rtx,
4411 gen_rtx_MEM (Pmode, sp_reg_rtx));
4414 else if (frame_pointer_needed)
4415 frame_reg_rtx = hard_frame_pointer_rtx;
4417 if ((strategy & REST_INLINE_VRS) == 0)
4419 int end_save = info->altivec_save_offset + info->altivec_size;
4421 rtx ptr_reg = gen_rtx_REG (Pmode, 0);
4422 rtx scratch_reg = gen_rtx_REG (Pmode, scratch_regno);
4424 if (end_save + frame_off != 0)
4426 rtx offset = GEN_INT (end_save + frame_off);
4428 emit_insn (gen_add3_insn (ptr_reg, frame_reg_rtx, offset));
4431 emit_move_insn (ptr_reg, frame_reg_rtx);
4433 ptr_off = -end_save;
4434 insn = rs6000_emit_savres_rtx (info, scratch_reg,
4435 info->altivec_save_offset + ptr_off,
4436 0, V4SImode, SAVRES_VR);
4440 for (i = info->first_altivec_reg_save; i <= LAST_ALTIVEC_REGNO; ++i)
4441 if (info->vrsave_mask & ALTIVEC_REG_BIT (i))
4443 rtx addr, areg, mem, insn;
4444 rtx reg = gen_rtx_REG (V4SImode, i);
4445 HOST_WIDE_INT offset
4446 = (info->altivec_save_offset + frame_off
4447 + 16 * (i - info->first_altivec_reg_save));
4449 if (TARGET_P9_VECTOR && quad_address_offset_p (offset))
4451 mem = gen_frame_mem (V4SImode,
4452 gen_rtx_PLUS (Pmode, frame_reg_rtx,
4454 insn = gen_rtx_SET (reg, mem);
4458 areg = gen_rtx_REG (Pmode, 0);
4459 emit_move_insn (areg, GEN_INT (offset));
4461 /* AltiVec addressing mode is [reg+reg]. */
4462 addr = gen_rtx_PLUS (Pmode, frame_reg_rtx, areg);
4463 mem = gen_frame_mem (V4SImode, addr);
4465 /* Rather than emitting a generic move, force use of the
4466 lvx instruction, which we always want. In particular we
4467 don't want lxvd2x/xxpermdi for little endian. */
4468 insn = gen_altivec_lvx_v4si_internal (reg, mem);
4471 (void) emit_insn (insn);
4475 for (i = info->first_altivec_reg_save; i <= LAST_ALTIVEC_REGNO; ++i)
4476 if (((strategy & REST_INLINE_VRS) == 0
4477 || (info->vrsave_mask & ALTIVEC_REG_BIT (i)) != 0)
4478 && (flag_shrink_wrap
4479 || (offset_below_red_zone_p
4480 (info->altivec_save_offset
4481 + 16 * (i - info->first_altivec_reg_save))))
4484 rtx reg = gen_rtx_REG (V4SImode, i);
4485 cfa_restores = alloc_reg_note (REG_CFA_RESTORE, reg, cfa_restores);
4489 /* Restore VRSAVE if we must do so before adjusting the stack. */
4490 if (info->vrsave_size != 0
4491 && (ALWAYS_RESTORE_ALTIVEC_BEFORE_POP
4492 || (DEFAULT_ABI != ABI_V4
4493 && offset_below_red_zone_p (info->vrsave_save_offset))))
4497 if (frame_reg_rtx == sp_reg_rtx)
4499 if (use_backchain_to_restore_sp)
4501 frame_reg_rtx = gen_rtx_REG (Pmode, 11);
4502 emit_move_insn (frame_reg_rtx,
4503 gen_rtx_MEM (Pmode, sp_reg_rtx));
4506 else if (frame_pointer_needed)
4507 frame_reg_rtx = hard_frame_pointer_rtx;
4510 reg = gen_rtx_REG (SImode, 12);
4511 emit_insn (gen_frame_load (reg, frame_reg_rtx,
4512 info->vrsave_save_offset + frame_off));
4514 emit_insn (generate_set_vrsave (reg, info, 1));
4518 /* If we have a large stack frame, restore the old stack pointer
4519 using the backchain. */
4520 if (use_backchain_to_restore_sp)
4522 if (frame_reg_rtx == sp_reg_rtx)
4524 /* Under V.4, don't reset the stack pointer until after we're done
4525 loading the saved registers. */
4526 if (DEFAULT_ABI == ABI_V4)
4527 frame_reg_rtx = gen_rtx_REG (Pmode, 11);
4529 insn = emit_move_insn (frame_reg_rtx,
4530 gen_rtx_MEM (Pmode, sp_reg_rtx));
4533 else if (ALWAYS_RESTORE_ALTIVEC_BEFORE_POP
4534 && DEFAULT_ABI == ABI_V4)
4535 /* frame_reg_rtx has been set up by the altivec restore. */
4539 insn = emit_move_insn (sp_reg_rtx, frame_reg_rtx);
4540 frame_reg_rtx = sp_reg_rtx;
4543 /* If we have a frame pointer, we can restore the old stack pointer
4545 else if (frame_pointer_needed)
4547 frame_reg_rtx = sp_reg_rtx;
4548 if (DEFAULT_ABI == ABI_V4)
4549 frame_reg_rtx = gen_rtx_REG (Pmode, 11);
4550 /* Prevent reordering memory accesses against stack pointer restore. */
4551 else if (cfun->calls_alloca
4552 || offset_below_red_zone_p (-info->total_size))
4553 rs6000_emit_stack_tie (frame_reg_rtx, true);
4555 insn = emit_insn (gen_add3_insn (frame_reg_rtx, hard_frame_pointer_rtx,
4556 GEN_INT (info->total_size)));
4559 else if (info->push_p
4560 && DEFAULT_ABI != ABI_V4
4561 && epilogue_type != EPILOGUE_TYPE_EH_RETURN)
4563 /* Prevent reordering memory accesses against stack pointer restore. */
4564 if (cfun->calls_alloca
4565 || offset_below_red_zone_p (-info->total_size))
4566 rs6000_emit_stack_tie (frame_reg_rtx, false);
4567 insn = emit_insn (gen_add3_insn (sp_reg_rtx, sp_reg_rtx,
4568 GEN_INT (info->total_size)));
4571 if (insn && frame_reg_rtx == sp_reg_rtx)
4575 REG_NOTES (insn) = cfa_restores;
4576 cfa_restores = NULL_RTX;
4578 add_reg_note (insn, REG_CFA_DEF_CFA, sp_reg_rtx);
4579 RTX_FRAME_RELATED_P (insn) = 1;
4582 /* Restore AltiVec registers if we have not done so already. */
4583 if (!ALWAYS_RESTORE_ALTIVEC_BEFORE_POP
4584 && info->altivec_size != 0
4585 && (DEFAULT_ABI == ABI_V4
4586 || !offset_below_red_zone_p (info->altivec_save_offset)))
4590 if ((strategy & REST_INLINE_VRS) == 0)
4592 int end_save = info->altivec_save_offset + info->altivec_size;
4594 rtx ptr_reg = gen_rtx_REG (Pmode, 0);
4595 int scratch_regno = ptr_regno_for_savres (SAVRES_VR);
4596 rtx scratch_reg = gen_rtx_REG (Pmode, scratch_regno);
4598 if (end_save + frame_off != 0)
4600 rtx offset = GEN_INT (end_save + frame_off);
4602 emit_insn (gen_add3_insn (ptr_reg, frame_reg_rtx, offset));
4605 emit_move_insn (ptr_reg, frame_reg_rtx);
4607 ptr_off = -end_save;
4608 insn = rs6000_emit_savres_rtx (info, scratch_reg,
4609 info->altivec_save_offset + ptr_off,
4610 0, V4SImode, SAVRES_VR);
4611 if (REGNO (frame_reg_rtx) == REGNO (scratch_reg))
4613 /* Frame reg was clobbered by out-of-line save. Restore it
4614 from ptr_reg, and if we are calling out-of-line gpr or
4615 fpr restore set up the correct pointer and offset. */
4616 unsigned newptr_regno = 1;
4617 if (!restoring_GPRs_inline)
4619 bool lr = info->gp_save_offset + info->gp_size == 0;
4620 int sel = SAVRES_GPR | (lr ? SAVRES_LR : 0);
4621 newptr_regno = ptr_regno_for_savres (sel);
4622 end_save = info->gp_save_offset + info->gp_size;
4624 else if (!restoring_FPRs_inline)
4626 bool lr = !(strategy & REST_NOINLINE_FPRS_DOESNT_RESTORE_LR);
4627 int sel = SAVRES_FPR | (lr ? SAVRES_LR : 0);
4628 newptr_regno = ptr_regno_for_savres (sel);
4629 end_save = info->fp_save_offset + info->fp_size;
4632 if (newptr_regno != 1 && REGNO (frame_reg_rtx) != newptr_regno)
4633 frame_reg_rtx = gen_rtx_REG (Pmode, newptr_regno);
4635 if (end_save + ptr_off != 0)
4637 rtx offset = GEN_INT (end_save + ptr_off);
4639 frame_off = -end_save;
4641 emit_insn (gen_addsi3_carry (frame_reg_rtx,
4644 emit_insn (gen_adddi3_carry (frame_reg_rtx,
4649 frame_off = ptr_off;
4650 emit_move_insn (frame_reg_rtx, ptr_reg);
4656 for (i = info->first_altivec_reg_save; i <= LAST_ALTIVEC_REGNO; ++i)
4657 if (info->vrsave_mask & ALTIVEC_REG_BIT (i))
4659 rtx addr, areg, mem, insn;
4660 rtx reg = gen_rtx_REG (V4SImode, i);
4661 HOST_WIDE_INT offset
4662 = (info->altivec_save_offset + frame_off
4663 + 16 * (i - info->first_altivec_reg_save));
4665 if (TARGET_P9_VECTOR && quad_address_offset_p (offset))
4667 mem = gen_frame_mem (V4SImode,
4668 gen_rtx_PLUS (Pmode, frame_reg_rtx,
4670 insn = gen_rtx_SET (reg, mem);
4674 areg = gen_rtx_REG (Pmode, 0);
4675 emit_move_insn (areg, GEN_INT (offset));
4677 /* AltiVec addressing mode is [reg+reg]. */
4678 addr = gen_rtx_PLUS (Pmode, frame_reg_rtx, areg);
4679 mem = gen_frame_mem (V4SImode, addr);
4681 /* Rather than emitting a generic move, force use of the
4682 lvx instruction, which we always want. In particular we
4683 don't want lxvd2x/xxpermdi for little endian. */
4684 insn = gen_altivec_lvx_v4si_internal (reg, mem);
4687 (void) emit_insn (insn);
4691 for (i = info->first_altivec_reg_save; i <= LAST_ALTIVEC_REGNO; ++i)
4692 if (((strategy & REST_INLINE_VRS) == 0
4693 || (info->vrsave_mask & ALTIVEC_REG_BIT (i)) != 0)
4694 && (DEFAULT_ABI == ABI_V4 || flag_shrink_wrap)
4697 rtx reg = gen_rtx_REG (V4SImode, i);
4698 cfa_restores = alloc_reg_note (REG_CFA_RESTORE, reg, cfa_restores);
4702 /* Restore VRSAVE if we have not done so already. */
4703 if (!ALWAYS_RESTORE_ALTIVEC_BEFORE_POP
4704 && info->vrsave_size != 0
4705 && (DEFAULT_ABI == ABI_V4
4706 || !offset_below_red_zone_p (info->vrsave_save_offset)))
4710 reg = gen_rtx_REG (SImode, 12);
4711 emit_insn (gen_frame_load (reg, frame_reg_rtx,
4712 info->vrsave_save_offset + frame_off));
4714 emit_insn (generate_set_vrsave (reg, info, 1));
4717 /* If we exit by an out-of-line restore function on ABI_V4 then that
4718 function will deallocate the stack, so we don't need to worry
4719 about the unwinder restoring cr from an invalid stack frame
4721 bool exit_func = (!restoring_FPRs_inline
4722 || (!restoring_GPRs_inline
4723 && info->first_fp_reg_save == 64));
4725 /* In the ELFv2 ABI we need to restore all call-saved CR fields from
4726 *separate* slots if the routine calls __builtin_eh_return, so
4727 that they can be independently restored by the unwinder. */
4728 if (DEFAULT_ABI == ABI_ELFv2 && crtl->calls_eh_return)
4730 int i, cr_off = info->ehcr_offset;
4732 for (i = 0; i < 8; i++)
4733 if (!call_used_regs[CR0_REGNO + i])
4735 rtx reg = gen_rtx_REG (SImode, 0);
4736 emit_insn (gen_frame_load (reg, frame_reg_rtx,
4737 cr_off + frame_off));
4739 insn = emit_insn (gen_movsi_to_cr_one
4740 (gen_rtx_REG (CCmode, CR0_REGNO + i), reg));
4742 if (!exit_func && flag_shrink_wrap)
4744 add_reg_note (insn, REG_CFA_RESTORE,
4745 gen_rtx_REG (SImode, CR0_REGNO + i));
4747 RTX_FRAME_RELATED_P (insn) = 1;
4754 /* Get the old lr if we saved it. If we are restoring registers
4755 out-of-line, then the out-of-line routines can do this for us. */
4756 if (restore_lr && restoring_GPRs_inline)
4757 load_lr_save (0, frame_reg_rtx, info->lr_save_offset + frame_off);
4759 /* Get the old cr if we saved it. */
4760 if (info->cr_save_p)
4762 unsigned cr_save_regno = 12;
4764 if (!restoring_GPRs_inline)
4766 /* Ensure we don't use the register used by the out-of-line
4767 gpr register restore below. */
4768 bool lr = info->gp_save_offset + info->gp_size == 0;
4769 int sel = SAVRES_GPR | (lr ? SAVRES_LR : 0);
4770 int gpr_ptr_regno = ptr_regno_for_savres (sel);
4772 if (gpr_ptr_regno == 12)
4774 gcc_checking_assert (REGNO (frame_reg_rtx) != cr_save_regno);
4776 else if (REGNO (frame_reg_rtx) == 12)
4779 cr_save_reg = load_cr_save (cr_save_regno, frame_reg_rtx,
4780 info->cr_save_offset + frame_off,
4784 /* Set LR here to try to overlap restores below. */
4785 if (restore_lr && restoring_GPRs_inline)
4786 restore_saved_lr (0, exit_func);
4788 /* Load exception handler data registers, if needed. */
4789 if (epilogue_type == EPILOGUE_TYPE_EH_RETURN)
4791 unsigned int i, regno;
4795 rtx reg = gen_rtx_REG (reg_mode, 2);
4796 emit_insn (gen_frame_load (reg, frame_reg_rtx,
4797 frame_off + RS6000_TOC_SAVE_SLOT));
4804 regno = EH_RETURN_DATA_REGNO (i);
4805 if (regno == INVALID_REGNUM)
4808 mem = gen_frame_mem_offset (reg_mode, frame_reg_rtx,
4809 info->ehrd_offset + frame_off
4810 + reg_size * (int) i);
4812 emit_move_insn (gen_rtx_REG (reg_mode, regno), mem);
4816 /* Restore GPRs. This is done as a PARALLEL if we are using
4817 the load-multiple instructions. */
4818 if (!restoring_GPRs_inline)
4820 /* We are jumping to an out-of-line function. */
4822 int end_save = info->gp_save_offset + info->gp_size;
4823 bool can_use_exit = end_save == 0;
4824 int sel = SAVRES_GPR | (can_use_exit ? SAVRES_LR : 0);
4827 /* Emit stack reset code if we need it. */
4828 ptr_regno = ptr_regno_for_savres (sel);
4829 ptr_reg = gen_rtx_REG (Pmode, ptr_regno);
4831 rs6000_emit_stack_reset (frame_reg_rtx, frame_off, ptr_regno);
4832 else if (end_save + frame_off != 0)
4833 emit_insn (gen_add3_insn (ptr_reg, frame_reg_rtx,
4834 GEN_INT (end_save + frame_off)));
4835 else if (REGNO (frame_reg_rtx) != ptr_regno)
4836 emit_move_insn (ptr_reg, frame_reg_rtx);
4837 if (REGNO (frame_reg_rtx) == ptr_regno)
4838 frame_off = -end_save;
4840 if (can_use_exit && info->cr_save_p)
4841 restore_saved_cr (cr_save_reg, using_mtcr_multiple, true);
4843 ptr_off = -end_save;
4844 rs6000_emit_savres_rtx (info, ptr_reg,
4845 info->gp_save_offset + ptr_off,
4846 info->lr_save_offset + ptr_off,
4849 else if (using_load_multiple)
4852 p = rtvec_alloc (32 - info->first_gp_reg_save);
4853 for (i = 0; i < 32 - info->first_gp_reg_save; i++)
4855 = gen_frame_load (gen_rtx_REG (reg_mode, info->first_gp_reg_save + i),
4857 info->gp_save_offset + frame_off + reg_size * i);
4858 emit_insn (gen_rtx_PARALLEL (VOIDmode, p));
4862 int offset = info->gp_save_offset + frame_off;
4863 for (i = info->first_gp_reg_save; i < 32; i++)
4866 && !cfun->machine->gpr_is_wrapped_separately[i])
4868 rtx reg = gen_rtx_REG (reg_mode, i);
4869 emit_insn (gen_frame_load (reg, frame_reg_rtx, offset));
4876 if (DEFAULT_ABI == ABI_V4 || flag_shrink_wrap)
4878 /* If the frame pointer was used then we can't delay emitting
4879 a REG_CFA_DEF_CFA note. This must happen on the insn that
4880 restores the frame pointer, r31. We may have already emitted
4881 a REG_CFA_DEF_CFA note, but that's OK; A duplicate is
4882 discarded by dwarf2cfi.c/dwarf2out.c, and in any case would
4883 be harmless if emitted. */
4884 if (frame_pointer_needed)
4886 insn = get_last_insn ();
4887 add_reg_note (insn, REG_CFA_DEF_CFA,
4888 plus_constant (Pmode, frame_reg_rtx, frame_off));
4889 RTX_FRAME_RELATED_P (insn) = 1;
4892 /* Set up cfa_restores. We always need these when
4893 shrink-wrapping. If not shrink-wrapping then we only need
4894 the cfa_restore when the stack location is no longer valid.
4895 The cfa_restores must be emitted on or before the insn that
4896 invalidates the stack, and of course must not be emitted
4897 before the insn that actually does the restore. The latter
4898 is why it is a bad idea to emit the cfa_restores as a group
4899 on the last instruction here that actually does a restore:
4900 That insn may be reordered with respect to others doing
4902 if (flag_shrink_wrap
4903 && !restoring_GPRs_inline
4904 && info->first_fp_reg_save == 64)
4905 cfa_restores = add_crlr_cfa_restore (info, cfa_restores);
4907 for (i = info->first_gp_reg_save; i < 32; i++)
4909 && !cfun->machine->gpr_is_wrapped_separately[i])
4911 rtx reg = gen_rtx_REG (reg_mode, i);
4912 cfa_restores = alloc_reg_note (REG_CFA_RESTORE, reg, cfa_restores);
4916 if (!restoring_GPRs_inline
4917 && info->first_fp_reg_save == 64)
4919 /* We are jumping to an out-of-line function. */
4921 emit_cfa_restores (cfa_restores);
4925 if (restore_lr && !restoring_GPRs_inline)
4927 load_lr_save (0, frame_reg_rtx, info->lr_save_offset + frame_off);
4928 restore_saved_lr (0, exit_func);
4931 /* Restore fpr's if we need to do it without calling a function. */
4932 if (restoring_FPRs_inline)
4934 int offset = info->fp_save_offset + frame_off;
4935 for (i = info->first_fp_reg_save; i < 64; i++)
4938 && !cfun->machine->fpr_is_wrapped_separately[i - 32])
4940 rtx reg = gen_rtx_REG (fp_reg_mode, i);
4941 emit_insn (gen_frame_load (reg, frame_reg_rtx, offset));
4942 if (DEFAULT_ABI == ABI_V4 || flag_shrink_wrap)
4943 cfa_restores = alloc_reg_note (REG_CFA_RESTORE, reg,
4947 offset += fp_reg_size;
4951 /* If we saved cr, restore it here. Just those that were used. */
4952 if (info->cr_save_p)
4953 restore_saved_cr (cr_save_reg, using_mtcr_multiple, exit_func);
4955 /* If this is V.4, unwind the stack pointer after all of the loads
4956 have been done, or set up r11 if we are restoring fp out of line. */
4958 if (!restoring_FPRs_inline)
4960 bool lr = (strategy & REST_NOINLINE_FPRS_DOESNT_RESTORE_LR) == 0;
4961 int sel = SAVRES_FPR | (lr ? SAVRES_LR : 0);
4962 ptr_regno = ptr_regno_for_savres (sel);
4965 insn = rs6000_emit_stack_reset (frame_reg_rtx, frame_off, ptr_regno);
4966 if (REGNO (frame_reg_rtx) == ptr_regno)
4969 if (insn && restoring_FPRs_inline)
4973 REG_NOTES (insn) = cfa_restores;
4974 cfa_restores = NULL_RTX;
4976 add_reg_note (insn, REG_CFA_DEF_CFA, sp_reg_rtx);
4977 RTX_FRAME_RELATED_P (insn) = 1;
4980 if (epilogue_type == EPILOGUE_TYPE_EH_RETURN)
4982 rtx sa = EH_RETURN_STACKADJ_RTX;
4983 emit_insn (gen_add3_insn (sp_reg_rtx, sp_reg_rtx, sa));
4986 if (epilogue_type != EPILOGUE_TYPE_SIBCALL && restoring_FPRs_inline)
4990 /* We can't hang the cfa_restores off a simple return,
4991 since the shrink-wrap code sometimes uses an existing
4992 return. This means there might be a path from
4993 pre-prologue code to this return, and dwarf2cfi code
4994 wants the eh_frame unwinder state to be the same on
4995 all paths to any point. So we need to emit the
4996 cfa_restores before the return. For -m64 we really
4997 don't need epilogue cfa_restores at all, except for
4998 this irritating dwarf2cfi with shrink-wrap
4999 requirement; The stack red-zone means eh_frame info
5000 from the prologue telling the unwinder to restore
5001 from the stack is perfectly good right to the end of
5003 emit_insn (gen_blockage ());
5004 emit_cfa_restores (cfa_restores);
5005 cfa_restores = NULL_RTX;
5008 emit_jump_insn (targetm.gen_simple_return ());
5011 if (epilogue_type != EPILOGUE_TYPE_SIBCALL && !restoring_FPRs_inline)
5013 bool lr = (strategy & REST_NOINLINE_FPRS_DOESNT_RESTORE_LR) == 0;
5014 rtvec p = rtvec_alloc (3 + !!lr + 64 - info->first_fp_reg_save);
5016 RTVEC_ELT (p, elt++) = ret_rtx;
5018 RTVEC_ELT (p, elt++) = gen_hard_reg_clobber (Pmode, LR_REGNO);
5020 /* We have to restore more than two FP registers, so branch to the
5021 restore function. It will return to our caller. */
5026 if (flag_shrink_wrap)
5027 cfa_restores = add_crlr_cfa_restore (info, cfa_restores);
5029 sym = rs6000_savres_routine_sym (info, SAVRES_FPR | (lr ? SAVRES_LR : 0));
5030 RTVEC_ELT (p, elt++) = gen_rtx_USE (VOIDmode, sym);
5031 reg = (DEFAULT_ABI == ABI_AIX || DEFAULT_ABI == ABI_ELFv2)? 1 : 11;
5032 RTVEC_ELT (p, elt++) = gen_rtx_USE (VOIDmode, gen_rtx_REG (Pmode, reg));
5034 for (i = 0; i < 64 - info->first_fp_reg_save; i++)
5036 rtx reg = gen_rtx_REG (DFmode, info->first_fp_reg_save + i);
5038 RTVEC_ELT (p, elt++)
5039 = gen_frame_load (reg, sp_reg_rtx, info->fp_save_offset + 8 * i);
5040 if (flag_shrink_wrap
5041 && save_reg_p (info->first_fp_reg_save + i))
5042 cfa_restores = alloc_reg_note (REG_CFA_RESTORE, reg, cfa_restores);
5045 emit_jump_insn (gen_rtx_PARALLEL (VOIDmode, p));
5050 if (epilogue_type == EPILOGUE_TYPE_SIBCALL)
5051 /* Ensure the cfa_restores are hung off an insn that won't
5052 be reordered above other restores. */
5053 emit_insn (gen_blockage ());
5055 emit_cfa_restores (cfa_restores);
5059 /* Write function epilogue. */
5062 rs6000_output_function_epilogue (FILE *file)
5065 macho_branch_islands ();
5068 rtx_insn *insn = get_last_insn ();
5069 rtx_insn *deleted_debug_label = NULL;
5071 /* Mach-O doesn't support labels at the end of objects, so if
5072 it looks like we might want one, take special action.
5074 First, collect any sequence of deleted debug labels. */
5077 && NOTE_KIND (insn) != NOTE_INSN_DELETED_LABEL)
5079 /* Don't insert a nop for NOTE_INSN_DELETED_DEBUG_LABEL
5080 notes only, instead set their CODE_LABEL_NUMBER to -1,
5081 otherwise there would be code generation differences
5082 in between -g and -g0. */
5083 if (NOTE_P (insn) && NOTE_KIND (insn) == NOTE_INSN_DELETED_DEBUG_LABEL)
5084 deleted_debug_label = insn;
5085 insn = PREV_INSN (insn);
5088 /* Second, if we have:
5091 then this needs to be detected, so skip past the barrier. */
5093 if (insn && BARRIER_P (insn))
5094 insn = PREV_INSN (insn);
5096 /* Up to now we've only seen notes or barriers. */
5101 && NOTE_KIND (insn) == NOTE_INSN_DELETED_LABEL))
5102 /* Trailing label: <barrier>. */
5103 fputs ("\tnop\n", file);
5106 /* Lastly, see if we have a completely empty function body. */
5107 while (insn && ! INSN_P (insn))
5108 insn = PREV_INSN (insn);
5109 /* If we don't find any insns, we've got an empty function body;
5110 I.e. completely empty - without a return or branch. This is
5111 taken as the case where a function body has been removed
5112 because it contains an inline __builtin_unreachable(). GCC
5113 states that reaching __builtin_unreachable() means UB so we're
5114 not obliged to do anything special; however, we want
5115 non-zero-sized function bodies. To meet this, and help the
5116 user out, let's trap the case. */
5118 fputs ("\ttrap\n", file);
5121 else if (deleted_debug_label)
5122 for (insn = deleted_debug_label; insn; insn = NEXT_INSN (insn))
5123 if (NOTE_KIND (insn) == NOTE_INSN_DELETED_DEBUG_LABEL)
5124 CODE_LABEL_NUMBER (insn) = -1;
5128 /* Output a traceback table here. See /usr/include/sys/debug.h for info
5131 We don't output a traceback table if -finhibit-size-directive was
5132 used. The documentation for -finhibit-size-directive reads
5133 ``don't output a @code{.size} assembler directive, or anything
5134 else that would cause trouble if the function is split in the
5135 middle, and the two halves are placed at locations far apart in
5136 memory.'' The traceback table has this property, since it
5137 includes the offset from the start of the function to the
5138 traceback table itself.
5140 System V.4 Powerpc's (and the embedded ABI derived from it) use a
5141 different traceback table. */
5142 if ((DEFAULT_ABI == ABI_AIX || DEFAULT_ABI == ABI_ELFv2)
5143 && ! flag_inhibit_size_directive
5144 && rs6000_traceback != traceback_none && !cfun->is_thunk)
5146 const char *fname = NULL;
5147 const char *language_string = lang_hooks.name;
5148 int fixed_parms = 0, float_parms = 0, parm_info = 0;
5151 rs6000_stack_t *info = rs6000_stack_info ();
5153 if (rs6000_traceback == traceback_full)
5155 else if (rs6000_traceback == traceback_part)
5158 optional_tbtab = !optimize_size && !TARGET_ELF;
5162 fname = XSTR (XEXP (DECL_RTL (current_function_decl), 0), 0);
5163 while (*fname == '.') /* V.4 encodes . in the name */
5166 /* Need label immediately before tbtab, so we can compute
5167 its offset from the function start. */
5168 ASM_OUTPUT_INTERNAL_LABEL_PREFIX (file, "LT");
5169 ASM_OUTPUT_LABEL (file, fname);
5172 /* The .tbtab pseudo-op can only be used for the first eight
5173 expressions, since it can't handle the possibly variable
5174 length fields that follow. However, if you omit the optional
5175 fields, the assembler outputs zeros for all optional fields
5176 anyways, giving each variable length field is minimum length
5177 (as defined in sys/debug.h). Thus we cannot use the .tbtab
5178 pseudo-op at all. */
5180 /* An all-zero word flags the start of the tbtab, for debuggers
5181 that have to find it by searching forward from the entry
5182 point or from the current pc. */
5183 fputs ("\t.long 0\n", file);
5185 /* Tbtab format type. Use format type 0. */
5186 fputs ("\t.byte 0,", file);
5188 /* Language type. Unfortunately, there does not seem to be any
5189 official way to discover the language being compiled, so we
5190 use language_string.
5191 C is 0. Fortran is 1. Ada is 3. C++ is 9.
5192 Java is 13. Objective-C is 14. Objective-C++ isn't assigned
5193 a number, so for now use 9. LTO, Go, D, and JIT aren't assigned
5194 numbers either, so for now use 0. */
5196 || ! strcmp (language_string, "GNU GIMPLE")
5197 || ! strcmp (language_string, "GNU Go")
5198 || ! strcmp (language_string, "GNU D")
5199 || ! strcmp (language_string, "libgccjit"))
5201 else if (! strcmp (language_string, "GNU F77")
5202 || lang_GNU_Fortran ())
5204 else if (! strcmp (language_string, "GNU Ada"))
5206 else if (lang_GNU_CXX ()
5207 || ! strcmp (language_string, "GNU Objective-C++"))
5209 else if (! strcmp (language_string, "GNU Java"))
5211 else if (! strcmp (language_string, "GNU Objective-C"))
5215 fprintf (file, "%d,", i);
5217 /* 8 single bit fields: global linkage (not set for C extern linkage,
5218 apparently a PL/I convention?), out-of-line epilogue/prologue, offset
5219 from start of procedure stored in tbtab, internal function, function
5220 has controlled storage, function has no toc, function uses fp,
5221 function logs/aborts fp operations. */
5222 /* Assume that fp operations are used if any fp reg must be saved. */
5223 fprintf (file, "%d,",
5224 (optional_tbtab << 5) | ((info->first_fp_reg_save != 64) << 1));
5226 /* 6 bitfields: function is interrupt handler, name present in
5227 proc table, function calls alloca, on condition directives
5228 (controls stack walks, 3 bits), saves condition reg, saves
5230 /* The `function calls alloca' bit seems to be set whenever reg 31 is
5231 set up as a frame pointer, even when there is no alloca call. */
5232 fprintf (file, "%d,",
5233 ((optional_tbtab << 6)
5234 | ((optional_tbtab & frame_pointer_needed) << 5)
5235 | (info->cr_save_p << 1)
5236 | (info->lr_save_p)));
5238 /* 3 bitfields: saves backchain, fixup code, number of fpr saved
5240 fprintf (file, "%d,",
5241 (info->push_p << 7) | (64 - info->first_fp_reg_save));
5243 /* 2 bitfields: spare bits (2 bits), number of gpr saved (6 bits). */
5244 fprintf (file, "%d,", (32 - first_reg_to_save ()));
5248 /* Compute the parameter info from the function decl argument
5251 int next_parm_info_bit = 31;
5253 for (decl = DECL_ARGUMENTS (current_function_decl);
5254 decl; decl = DECL_CHAIN (decl))
5256 rtx parameter = DECL_INCOMING_RTL (decl);
5257 machine_mode mode = GET_MODE (parameter);
5259 if (REG_P (parameter))
5261 if (SCALAR_FLOAT_MODE_P (mode))
5287 /* If only one bit will fit, don't or in this entry. */
5288 if (next_parm_info_bit > 0)
5289 parm_info |= (bits << (next_parm_info_bit - 1));
5290 next_parm_info_bit -= 2;
5294 fixed_parms += ((GET_MODE_SIZE (mode)
5295 + (UNITS_PER_WORD - 1))
5297 next_parm_info_bit -= 1;
5303 /* Number of fixed point parameters. */
5304 /* This is actually the number of words of fixed point parameters; thus
5305 an 8 byte struct counts as 2; and thus the maximum value is 8. */
5306 fprintf (file, "%d,", fixed_parms);
5308 /* 2 bitfields: number of floating point parameters (7 bits), parameters
5310 /* This is actually the number of fp registers that hold parameters;
5311 and thus the maximum value is 13. */
5312 /* Set parameters on stack bit if parameters are not in their original
5313 registers, regardless of whether they are on the stack? Xlc
5314 seems to set the bit when not optimizing. */
5315 fprintf (file, "%d\n", ((float_parms << 1) | (! optimize)));
5319 /* Optional fields follow. Some are variable length. */
5321 /* Parameter types, left adjusted bit fields: 0 fixed, 10 single
5322 float, 11 double float. */
5323 /* There is an entry for each parameter in a register, in the order
5324 that they occur in the parameter list. Any intervening arguments
5325 on the stack are ignored. If the list overflows a long (max
5326 possible length 34 bits) then completely leave off all elements
5328 /* Only emit this long if there was at least one parameter. */
5329 if (fixed_parms || float_parms)
5330 fprintf (file, "\t.long %d\n", parm_info);
5332 /* Offset from start of code to tb table. */
5333 fputs ("\t.long ", file);
5334 ASM_OUTPUT_INTERNAL_LABEL_PREFIX (file, "LT");
5335 RS6000_OUTPUT_BASENAME (file, fname);
5337 rs6000_output_function_entry (file, fname);
5340 /* Interrupt handler mask. */
5341 /* Omit this long, since we never set the interrupt handler bit
5344 /* Number of CTL (controlled storage) anchors. */
5345 /* Omit this long, since the has_ctl bit is never set above. */
5347 /* Displacement into stack of each CTL anchor. */
5348 /* Omit this list of longs, because there are no CTL anchors. */
5350 /* Length of function name. */
5353 fprintf (file, "\t.short %d\n", (int) strlen (fname));
5355 /* Function name. */
5356 assemble_string (fname, strlen (fname));
5358 /* Register for alloca automatic storage; this is always reg 31.
5359 Only emit this if the alloca bit was set above. */
5360 if (frame_pointer_needed)
5361 fputs ("\t.byte 31\n", file);
5363 fputs ("\t.align 2\n", file);
5367 /* Arrange to define .LCTOC1 label, if not already done. */
5371 if (!toc_initialized)
5373 switch_to_section (toc_section);
5374 switch_to_section (current_function_section ());
5379 /* -fsplit-stack support. */
5381 /* A SYMBOL_REF for __morestack. */
5382 static GTY(()) rtx morestack_ref;
5385 gen_add3_const (rtx rt, rtx ra, long c)
5388 return gen_adddi3 (rt, ra, GEN_INT (c));
5390 return gen_addsi3 (rt, ra, GEN_INT (c));
5393 /* Emit -fsplit-stack prologue, which goes before the regular function
5394 prologue (at local entry point in the case of ELFv2). */
5397 rs6000_expand_split_stack_prologue (void)
5399 rs6000_stack_t *info = rs6000_stack_info ();
5400 unsigned HOST_WIDE_INT allocate;
5401 long alloc_hi, alloc_lo;
5402 rtx r0, r1, r12, lr, ok_label, compare, jump, call_fusage;
5405 gcc_assert (flag_split_stack && reload_completed);
5410 if (global_regs[29])
5412 error ("%qs uses register r29", "%<-fsplit-stack%>");
5413 inform (DECL_SOURCE_LOCATION (global_regs_decl[29]),
5414 "conflicts with %qD", global_regs_decl[29]);
5417 allocate = info->total_size;
5418 if (allocate > (unsigned HOST_WIDE_INT) 1 << 31)
5420 sorry ("Stack frame larger than 2G is not supported for "
5421 "%<-fsplit-stack%>");
5424 if (morestack_ref == NULL_RTX)
5426 morestack_ref = gen_rtx_SYMBOL_REF (Pmode, "__morestack");
5427 SYMBOL_REF_FLAGS (morestack_ref) |= (SYMBOL_FLAG_LOCAL
5428 | SYMBOL_FLAG_FUNCTION);
5431 r0 = gen_rtx_REG (Pmode, 0);
5432 r1 = gen_rtx_REG (Pmode, STACK_POINTER_REGNUM);
5433 r12 = gen_rtx_REG (Pmode, 12);
5434 emit_insn (gen_load_split_stack_limit (r0));
5435 /* Always emit two insns here to calculate the requested stack,
5436 so that the linker can edit them when adjusting size for calling
5437 non-split-stack code. */
5438 alloc_hi = (-allocate + 0x8000) & ~0xffffL;
5439 alloc_lo = -allocate - alloc_hi;
5442 emit_insn (gen_add3_const (r12, r1, alloc_hi));
5444 emit_insn (gen_add3_const (r12, r12, alloc_lo));
5446 emit_insn (gen_nop ());
5450 emit_insn (gen_add3_const (r12, r1, alloc_lo));
5451 emit_insn (gen_nop ());
5454 compare = gen_rtx_REG (CCUNSmode, CR7_REGNO);
5455 emit_insn (gen_rtx_SET (compare, gen_rtx_COMPARE (CCUNSmode, r12, r0)));
5456 ok_label = gen_label_rtx ();
5457 jump = gen_rtx_IF_THEN_ELSE (VOIDmode,
5458 gen_rtx_GEU (VOIDmode, compare, const0_rtx),
5459 gen_rtx_LABEL_REF (VOIDmode, ok_label),
5461 insn = emit_jump_insn (gen_rtx_SET (pc_rtx, jump));
5462 JUMP_LABEL (insn) = ok_label;
5463 /* Mark the jump as very likely to be taken. */
5464 add_reg_br_prob_note (insn, profile_probability::very_likely ());
5466 lr = gen_rtx_REG (Pmode, LR_REGNO);
5467 insn = emit_move_insn (r0, lr);
5468 RTX_FRAME_RELATED_P (insn) = 1;
5469 insn = emit_insn (gen_frame_store (r0, r1, info->lr_save_offset));
5470 RTX_FRAME_RELATED_P (insn) = 1;
5472 insn = emit_call_insn (gen_call (gen_rtx_MEM (SImode, morestack_ref),
5473 const0_rtx, const0_rtx));
5474 call_fusage = NULL_RTX;
5475 use_reg (&call_fusage, r12);
5476 /* Say the call uses r0, even though it doesn't, to stop regrename
5477 from twiddling with the insns saving lr, trashing args for cfun.
5478 The insns restoring lr are similarly protected by making
5479 split_stack_return use r0. */
5480 use_reg (&call_fusage, r0);
5481 add_function_usage_to (insn, call_fusage);
5482 /* Indicate that this function can't jump to non-local gotos. */
5483 make_reg_eh_region_note_nothrow_nononlocal (insn);
5484 emit_insn (gen_frame_load (r0, r1, info->lr_save_offset));
5485 insn = emit_move_insn (lr, r0);
5486 add_reg_note (insn, REG_CFA_RESTORE, lr);
5487 RTX_FRAME_RELATED_P (insn) = 1;
5488 emit_insn (gen_split_stack_return ());
5490 emit_label (ok_label);
5491 LABEL_NUSES (ok_label) = 1;
5494 /* We may have to tell the dataflow pass that the split stack prologue
5495 is initializing a register. */
5498 rs6000_live_on_entry (bitmap regs)
5500 if (flag_split_stack)
5501 bitmap_set_bit (regs, 12);
5504 /* Emit -fsplit-stack dynamic stack allocation space check. */
5507 rs6000_split_stack_space_check (rtx size, rtx label)
5509 rtx sp = gen_rtx_REG (Pmode, STACK_POINTER_REGNUM);
5510 rtx limit = gen_reg_rtx (Pmode);
5511 rtx requested = gen_reg_rtx (Pmode);
5512 rtx cmp = gen_reg_rtx (CCUNSmode);
5515 emit_insn (gen_load_split_stack_limit (limit));
5516 if (CONST_INT_P (size))
5517 emit_insn (gen_add3_insn (requested, sp, GEN_INT (-INTVAL (size))));
5520 size = force_reg (Pmode, size);
5521 emit_move_insn (requested, gen_rtx_MINUS (Pmode, sp, size));
5523 emit_insn (gen_rtx_SET (cmp, gen_rtx_COMPARE (CCUNSmode, requested, limit)));
5524 jump = gen_rtx_IF_THEN_ELSE (VOIDmode,
5525 gen_rtx_GEU (VOIDmode, cmp, const0_rtx),
5526 gen_rtx_LABEL_REF (VOIDmode, label),
5528 jump = emit_jump_insn (gen_rtx_SET (pc_rtx, jump));
5529 JUMP_LABEL (jump) = label;
5533 /* Return whether we need to always update the saved TOC pointer when we update
5534 the stack pointer. */
5537 rs6000_save_toc_in_prologue_p (void)
5539 return (cfun && cfun->machine && cfun->machine->save_toc_in_prologue);
5542 #include "gt-rs6000-logue.h"