sreal: Fix typo in function name
[official-gcc.git] / gcc / config / rs6000 / rs6000-logue.cc
blob98846f781eccfb01fa8bf6b3da9deb3e3bc3c9f6
1 /* Subroutines used to generate function prologues and epilogues
2 on IBM RS/6000.
3 Copyright (C) 1991-2023 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
23 #include "config.h"
24 #include "system.h"
25 #include "coretypes.h"
26 #include "backend.h"
27 #include "rtl.h"
28 #include "tree.h"
29 #include "memmodel.h"
30 #include "df.h"
31 #include "tm_p.h"
32 #include "ira.h"
33 #include "print-tree.h"
34 #include "varasm.h"
35 #include "explow.h"
36 #include "expr.h"
37 #include "output.h"
38 #include "tree-pass.h"
39 #include "rtx-vector-builder.h"
40 #include "predict.h"
41 #include "target.h"
42 #include "stringpool.h"
43 #include "attribs.h"
44 #include "except.h"
45 #include "langhooks.h"
46 #include "optabs.h"
47 #include "diagnostic-core.h"
48 #include "alias.h"
49 #include "rs6000-internal.h"
51 static int rs6000_ra_ever_killed (void);
52 static void is_altivec_return_reg (rtx, void *);
53 static bool rs6000_save_toc_in_prologue_p (void);
55 static rs6000_stack_t stack_info;
57 /* Set if HARD_FRAM_POINTER_REGNUM is really needed. */
58 static bool frame_pointer_needed_indeed = false;
60 /* Label number of label created for -mrelocatable, to call to so we can
61 get the address of the GOT section */
62 int rs6000_pic_labelno = 0;
65 #ifndef TARGET_PROFILE_KERNEL
66 #define TARGET_PROFILE_KERNEL 0
67 #endif
70 /* Function to init struct machine_function.
71 This will be called, via a pointer variable,
72 from push_function_context. */
74 struct machine_function *
75 rs6000_init_machine_status (void)
77 stack_info.reload_completed = 0;
78 return ggc_cleared_alloc<machine_function> ();
81 /* This page contains routines that are used to determine what the
82 function prologue and epilogue code will do and write them out. */
84 /* Determine whether the REG is really used. */
86 bool
87 save_reg_p (int reg)
89 if (reg == RS6000_PIC_OFFSET_TABLE_REGNUM && !TARGET_SINGLE_PIC_BASE)
91 /* When calling eh_return, we must return true for all the cases
92 where conditional_register_usage marks the PIC offset reg
93 call used or fixed. */
94 if (crtl->calls_eh_return
95 && ((DEFAULT_ABI == ABI_V4 && flag_pic)
96 || (DEFAULT_ABI == ABI_DARWIN && flag_pic)
97 || (TARGET_TOC && TARGET_MINIMAL_TOC)))
98 return true;
100 /* We need to mark the PIC offset register live for the same
101 conditions as it is set up in rs6000_emit_prologue, or
102 otherwise it won't be saved before we clobber it. */
103 if (TARGET_TOC && TARGET_MINIMAL_TOC
104 && !constant_pool_empty_p ())
105 return true;
107 if (DEFAULT_ABI == ABI_V4
108 && (flag_pic == 1 || (flag_pic && TARGET_SECURE_PLT))
109 && df_regs_ever_live_p (RS6000_PIC_OFFSET_TABLE_REGNUM))
110 return true;
112 if (DEFAULT_ABI == ABI_DARWIN
113 && flag_pic && crtl->uses_pic_offset_table)
114 return true;
117 return !call_used_or_fixed_reg_p (reg) && df_regs_ever_live_p (reg);
120 /* Return the first fixed-point register that is required to be
121 saved. 32 if none. */
124 first_reg_to_save (void)
126 int first_reg;
128 /* Find lowest numbered live register. */
129 for (first_reg = 13; first_reg <= 31; first_reg++)
130 if (save_reg_p (first_reg))
131 break;
133 return first_reg;
136 /* Similar, for FP regs. */
139 first_fp_reg_to_save (void)
141 int first_reg;
143 /* Find lowest numbered live register. */
144 for (first_reg = 14 + 32; first_reg <= 63; first_reg++)
145 if (save_reg_p (first_reg))
146 break;
148 return first_reg;
151 /* Similar, for AltiVec regs. */
153 static int
154 first_altivec_reg_to_save (void)
156 int i;
158 /* Stack frame remains as is unless we are in AltiVec ABI. */
159 if (! TARGET_ALTIVEC_ABI)
160 return LAST_ALTIVEC_REGNO + 1;
162 /* On Darwin, the unwind routines are compiled without
163 TARGET_ALTIVEC, and use save_world to save/restore the
164 altivec registers when necessary. */
165 if (DEFAULT_ABI == ABI_DARWIN && crtl->calls_eh_return
166 && ! TARGET_ALTIVEC)
167 return FIRST_ALTIVEC_REGNO + 20;
169 /* Find lowest numbered live register. */
170 for (i = FIRST_ALTIVEC_REGNO + 20; i <= LAST_ALTIVEC_REGNO; ++i)
171 if (save_reg_p (i))
172 break;
174 return i;
177 /* Return a 32-bit mask of the AltiVec registers we need to set in
178 VRSAVE. Bit n of the return value is 1 if Vn is live. The MSB in
179 the 32-bit word is 0. */
181 static unsigned int
182 compute_vrsave_mask (void)
184 unsigned int i, mask = 0;
186 /* On Darwin, the unwind routines are compiled without
187 TARGET_ALTIVEC, and use save_world to save/restore the
188 call-saved altivec registers when necessary. */
189 if (DEFAULT_ABI == ABI_DARWIN && crtl->calls_eh_return
190 && ! TARGET_ALTIVEC)
191 mask |= 0xFFF;
193 /* First, find out if we use _any_ altivec registers. */
194 for (i = FIRST_ALTIVEC_REGNO; i <= LAST_ALTIVEC_REGNO; ++i)
195 if (df_regs_ever_live_p (i))
196 mask |= ALTIVEC_REG_BIT (i);
198 if (mask == 0)
199 return mask;
201 /* Next, remove the argument registers from the set. These must
202 be in the VRSAVE mask set by the caller, so we don't need to add
203 them in again. More importantly, the mask we compute here is
204 used to generate CLOBBERs in the set_vrsave insn, and we do not
205 wish the argument registers to die. */
206 for (i = ALTIVEC_ARG_MIN_REG; i < (unsigned) crtl->args.info.vregno; i++)
207 mask &= ~ALTIVEC_REG_BIT (i);
209 /* Similarly, remove the return value from the set. */
211 bool yes = false;
212 diddle_return_value (is_altivec_return_reg, &yes);
213 if (yes)
214 mask &= ~ALTIVEC_REG_BIT (ALTIVEC_ARG_RETURN);
217 return mask;
220 /* For a very restricted set of circumstances, we can cut down the
221 size of prologues/epilogues by calling our own save/restore-the-world
222 routines. */
224 static void
225 compute_save_world_info (rs6000_stack_t *info)
227 info->world_save_p = 1;
228 info->world_save_p
229 = (WORLD_SAVE_P (info)
230 && DEFAULT_ABI == ABI_DARWIN
231 && !cfun->has_nonlocal_label
232 && info->first_fp_reg_save == FIRST_SAVED_FP_REGNO
233 && info->first_gp_reg_save == FIRST_SAVED_GP_REGNO
234 && info->first_altivec_reg_save == FIRST_SAVED_ALTIVEC_REGNO
235 && info->cr_save_p);
237 /* This will not work in conjunction with sibcalls. Make sure there
238 are none. (This check is expensive, but seldom executed.) */
239 if (WORLD_SAVE_P (info))
241 rtx_insn *insn;
242 for (insn = get_last_insn_anywhere (); insn; insn = PREV_INSN (insn))
243 if (CALL_P (insn) && SIBLING_CALL_P (insn))
245 info->world_save_p = 0;
246 break;
250 if (WORLD_SAVE_P (info))
252 /* Even if we're not touching VRsave, make sure there's room on the
253 stack for it, if it looks like we're calling SAVE_WORLD, which
254 will attempt to save it. */
255 info->vrsave_size = 4;
257 /* If we are going to save the world, we need to save the link register too. */
258 info->lr_save_p = 1;
260 /* "Save" the VRsave register too if we're saving the world. */
261 if (info->vrsave_mask == 0)
262 info->vrsave_mask = compute_vrsave_mask ();
264 /* Because the Darwin register save/restore routines only handle
265 F14 .. F31 and V20 .. V31 as per the ABI, perform a consistency
266 check. */
267 gcc_assert (info->first_fp_reg_save >= FIRST_SAVED_FP_REGNO
268 && (info->first_altivec_reg_save
269 >= FIRST_SAVED_ALTIVEC_REGNO));
272 return;
276 static void
277 is_altivec_return_reg (rtx reg, void *xyes)
279 bool *yes = (bool *) xyes;
280 if (REGNO (reg) == ALTIVEC_ARG_RETURN)
281 *yes = true;
285 /* Return whether REG is a global user reg or has been specifed by
286 -ffixed-REG. We should not restore these, and so cannot use
287 lmw or out-of-line restore functions if there are any. We also
288 can't save them (well, emit frame notes for them), because frame
289 unwinding during exception handling will restore saved registers. */
291 static bool
292 fixed_reg_p (int reg)
294 /* Ignore fixed_regs[RS6000_PIC_OFFSET_TABLE_REGNUM] when the
295 backend sets it, overriding anything the user might have given. */
296 if (reg == RS6000_PIC_OFFSET_TABLE_REGNUM
297 && ((DEFAULT_ABI == ABI_V4 && flag_pic)
298 || (DEFAULT_ABI == ABI_DARWIN && flag_pic)
299 || (TARGET_TOC && TARGET_MINIMAL_TOC)))
300 return false;
302 return fixed_regs[reg];
305 /* Determine the strategy for savings/restoring registers. */
307 enum {
308 SAVE_MULTIPLE = 0x1,
309 SAVE_INLINE_GPRS = 0x2,
310 SAVE_INLINE_FPRS = 0x4,
311 SAVE_NOINLINE_GPRS_SAVES_LR = 0x8,
312 SAVE_NOINLINE_FPRS_SAVES_LR = 0x10,
313 SAVE_INLINE_VRS = 0x20,
314 REST_MULTIPLE = 0x100,
315 REST_INLINE_GPRS = 0x200,
316 REST_INLINE_FPRS = 0x400,
317 REST_NOINLINE_FPRS_DOESNT_RESTORE_LR = 0x800,
318 REST_INLINE_VRS = 0x1000
321 static int
322 rs6000_savres_strategy (rs6000_stack_t *info,
323 bool using_static_chain_p)
325 int strategy = 0;
327 /* Select between in-line and out-of-line save and restore of regs.
328 First, all the obvious cases where we don't use out-of-line. */
329 if (crtl->calls_eh_return
330 || cfun->machine->ra_need_lr)
331 strategy |= (SAVE_INLINE_FPRS | REST_INLINE_FPRS
332 | SAVE_INLINE_GPRS | REST_INLINE_GPRS
333 | SAVE_INLINE_VRS | REST_INLINE_VRS);
335 if (info->first_gp_reg_save == 32)
336 strategy |= SAVE_INLINE_GPRS | REST_INLINE_GPRS;
338 if (info->first_fp_reg_save == 64)
339 strategy |= SAVE_INLINE_FPRS | REST_INLINE_FPRS;
341 if (info->first_altivec_reg_save == LAST_ALTIVEC_REGNO + 1)
342 strategy |= SAVE_INLINE_VRS | REST_INLINE_VRS;
344 /* Define cutoff for using out-of-line functions to save registers. */
345 if (DEFAULT_ABI == ABI_V4 || TARGET_ELF)
347 if (!optimize_size)
349 strategy |= SAVE_INLINE_FPRS | REST_INLINE_FPRS;
350 strategy |= SAVE_INLINE_GPRS | REST_INLINE_GPRS;
351 strategy |= SAVE_INLINE_VRS | REST_INLINE_VRS;
353 else
355 /* Prefer out-of-line restore if it will exit. */
356 if (info->first_fp_reg_save > 61)
357 strategy |= SAVE_INLINE_FPRS;
358 if (info->first_gp_reg_save > 29)
360 if (info->first_fp_reg_save == 64)
361 strategy |= SAVE_INLINE_GPRS;
362 else
363 strategy |= SAVE_INLINE_GPRS | REST_INLINE_GPRS;
365 if (info->first_altivec_reg_save == LAST_ALTIVEC_REGNO)
366 strategy |= SAVE_INLINE_VRS | REST_INLINE_VRS;
369 else if (DEFAULT_ABI == ABI_DARWIN)
371 if (info->first_fp_reg_save > 60)
372 strategy |= SAVE_INLINE_FPRS | REST_INLINE_FPRS;
373 if (info->first_gp_reg_save > 29)
374 strategy |= SAVE_INLINE_GPRS | REST_INLINE_GPRS;
375 strategy |= SAVE_INLINE_VRS | REST_INLINE_VRS;
377 else
379 gcc_checking_assert (DEFAULT_ABI == ABI_AIX || DEFAULT_ABI == ABI_ELFv2);
380 if ((flag_shrink_wrap_separate && optimize_function_for_speed_p (cfun))
381 || info->first_fp_reg_save > 61)
382 strategy |= SAVE_INLINE_FPRS | REST_INLINE_FPRS;
383 strategy |= SAVE_INLINE_GPRS | REST_INLINE_GPRS;
384 strategy |= SAVE_INLINE_VRS | REST_INLINE_VRS;
387 /* Don't bother to try to save things out-of-line if r11 is occupied
388 by the static chain. It would require too much fiddling and the
389 static chain is rarely used anyway. FPRs are saved w.r.t the stack
390 pointer on Darwin, and AIX uses r1 or r12. */
391 if (using_static_chain_p
392 && (DEFAULT_ABI == ABI_V4 || DEFAULT_ABI == ABI_DARWIN))
393 strategy |= ((DEFAULT_ABI == ABI_DARWIN ? 0 : SAVE_INLINE_FPRS)
394 | SAVE_INLINE_GPRS
395 | SAVE_INLINE_VRS);
397 /* Don't ever restore fixed regs. That means we can't use the
398 out-of-line register restore functions if a fixed reg is in the
399 range of regs restored. */
400 if (!(strategy & REST_INLINE_FPRS))
401 for (int i = info->first_fp_reg_save; i < 64; i++)
402 if (fixed_regs[i])
404 strategy |= REST_INLINE_FPRS;
405 break;
408 /* We can only use the out-of-line routines to restore fprs if we've
409 saved all the registers from first_fp_reg_save in the prologue.
410 Otherwise, we risk loading garbage. Of course, if we have saved
411 out-of-line then we know we haven't skipped any fprs. */
412 if ((strategy & SAVE_INLINE_FPRS)
413 && !(strategy & REST_INLINE_FPRS))
414 for (int i = info->first_fp_reg_save; i < 64; i++)
415 if (!save_reg_p (i))
417 strategy |= REST_INLINE_FPRS;
418 break;
421 /* Similarly, for altivec regs. */
422 if (!(strategy & REST_INLINE_VRS))
423 for (int i = info->first_altivec_reg_save; i < LAST_ALTIVEC_REGNO + 1; i++)
424 if (fixed_regs[i])
426 strategy |= REST_INLINE_VRS;
427 break;
430 if ((strategy & SAVE_INLINE_VRS)
431 && !(strategy & REST_INLINE_VRS))
432 for (int i = info->first_altivec_reg_save; i < LAST_ALTIVEC_REGNO + 1; i++)
433 if (!save_reg_p (i))
435 strategy |= REST_INLINE_VRS;
436 break;
439 /* info->lr_save_p isn't yet set if the only reason lr needs to be
440 saved is an out-of-line save or restore. Set up the value for
441 the next test (excluding out-of-line gprs). */
442 bool lr_save_p = (info->lr_save_p
443 || !(strategy & SAVE_INLINE_FPRS)
444 || !(strategy & SAVE_INLINE_VRS)
445 || !(strategy & REST_INLINE_FPRS)
446 || !(strategy & REST_INLINE_VRS));
448 if (TARGET_MULTIPLE
449 && !TARGET_POWERPC64
450 && info->first_gp_reg_save < 31
451 && !(flag_shrink_wrap
452 && flag_shrink_wrap_separate
453 && optimize_function_for_speed_p (cfun)))
455 int count = 0;
456 for (int i = info->first_gp_reg_save; i < 32; i++)
457 if (save_reg_p (i))
458 count++;
460 if (count <= 1)
461 /* Don't use store multiple if only one reg needs to be
462 saved. This can occur for example when the ABI_V4 pic reg
463 (r30) needs to be saved to make calls, but r31 is not
464 used. */
465 strategy |= SAVE_INLINE_GPRS | REST_INLINE_GPRS;
466 else
468 /* Prefer store multiple for saves over out-of-line
469 routines, since the store-multiple instruction will
470 always be smaller. */
471 strategy |= SAVE_INLINE_GPRS | SAVE_MULTIPLE;
473 /* The situation is more complicated with load multiple.
474 We'd prefer to use the out-of-line routines for restores,
475 since the "exit" out-of-line routines can handle the
476 restore of LR and the frame teardown. However if doesn't
477 make sense to use the out-of-line routine if that is the
478 only reason we'd need to save LR, and we can't use the
479 "exit" out-of-line gpr restore if we have saved some
480 fprs; In those cases it is advantageous to use load
481 multiple when available. */
482 if (info->first_fp_reg_save != 64 || !lr_save_p)
483 strategy |= REST_INLINE_GPRS | REST_MULTIPLE;
487 /* Using the "exit" out-of-line routine does not improve code size
488 if using it would require lr to be saved and if only saving one
489 or two gprs. */
490 else if (!lr_save_p && info->first_gp_reg_save > 29)
491 strategy |= SAVE_INLINE_GPRS | REST_INLINE_GPRS;
493 /* Don't ever restore fixed regs. */
494 if ((strategy & (REST_INLINE_GPRS | REST_MULTIPLE)) != REST_INLINE_GPRS)
495 for (int i = info->first_gp_reg_save; i < 32; i++)
496 if (fixed_reg_p (i))
498 strategy |= REST_INLINE_GPRS;
499 strategy &= ~REST_MULTIPLE;
500 break;
503 /* We can only use load multiple or the out-of-line routines to
504 restore gprs if we've saved all the registers from
505 first_gp_reg_save. Otherwise, we risk loading garbage.
506 Of course, if we have saved out-of-line or used stmw then we know
507 we haven't skipped any gprs. */
508 if ((strategy & (SAVE_INLINE_GPRS | SAVE_MULTIPLE)) == SAVE_INLINE_GPRS
509 && (strategy & (REST_INLINE_GPRS | REST_MULTIPLE)) != REST_INLINE_GPRS)
510 for (int i = info->first_gp_reg_save; i < 32; i++)
511 if (!save_reg_p (i))
513 strategy |= REST_INLINE_GPRS;
514 strategy &= ~REST_MULTIPLE;
515 break;
518 if (TARGET_ELF && TARGET_64BIT)
520 if (!(strategy & SAVE_INLINE_FPRS))
521 strategy |= SAVE_NOINLINE_FPRS_SAVES_LR;
522 else if (!(strategy & SAVE_INLINE_GPRS)
523 && info->first_fp_reg_save == 64)
524 strategy |= SAVE_NOINLINE_GPRS_SAVES_LR;
526 else if (TARGET_AIX && !(strategy & REST_INLINE_FPRS))
527 strategy |= REST_NOINLINE_FPRS_DOESNT_RESTORE_LR;
529 if (TARGET_MACHO && !(strategy & SAVE_INLINE_FPRS))
530 strategy |= SAVE_NOINLINE_FPRS_SAVES_LR;
532 return strategy;
535 /* Calculate the stack information for the current function. This is
536 complicated by having two separate calling sequences, the AIX calling
537 sequence and the V.4 calling sequence.
539 AIX (and Darwin/Mac OS X) stack frames look like:
540 32-bit 64-bit
541 SP----> +---------------------------------------+
542 | back chain to caller | 0 0
543 +---------------------------------------+
544 | saved CR | 4 8 (8-11)
545 +---------------------------------------+
546 | saved LR | 8 16
547 +---------------------------------------+
548 | reserved for compilers | 12 24
549 +---------------------------------------+
550 | reserved for binders | 16 32
551 +---------------------------------------+
552 | saved TOC pointer | 20 40
553 +---------------------------------------+
554 | Parameter save area (+padding*) (P) | 24 48
555 +---------------------------------------+
556 | Alloca space (A) | 24+P etc.
557 +---------------------------------------+
558 | Local variable space (L) | 24+P+A
559 +---------------------------------------+
560 | Float/int conversion temporary (X) | 24+P+A+L
561 +---------------------------------------+
562 | Save area for AltiVec registers (W) | 24+P+A+L+X
563 +---------------------------------------+
564 | AltiVec alignment padding (Y) | 24+P+A+L+X+W
565 +---------------------------------------+
566 | Save area for VRSAVE register (Z) | 24+P+A+L+X+W+Y
567 +---------------------------------------+
568 | Save area for GP registers (G) | 24+P+A+X+L+X+W+Y+Z
569 +---------------------------------------+
570 | Save area for FP registers (F) | 24+P+A+X+L+X+W+Y+Z+G
571 +---------------------------------------+
572 old SP->| back chain to caller's caller |
573 +---------------------------------------+
575 * If the alloca area is present, the parameter save area is
576 padded so that the former starts 16-byte aligned.
578 The required alignment for AIX configurations is two words (i.e., 8
579 or 16 bytes).
581 The ELFv2 ABI is a variant of the AIX ABI. Stack frames look like:
583 SP----> +---------------------------------------+
584 | Back chain to caller | 0
585 +---------------------------------------+
586 | Save area for CR | 8
587 +---------------------------------------+
588 | Saved LR | 16
589 +---------------------------------------+
590 | Saved TOC pointer | 24
591 +---------------------------------------+
592 | Parameter save area (+padding*) (P) | 32
593 +---------------------------------------+
594 | Optional ROP hash slot (R) | 32+P
595 +---------------------------------------+
596 | Alloca space (A) | 32+P+R
597 +---------------------------------------+
598 | Local variable space (L) | 32+P+R+A
599 +---------------------------------------+
600 | Save area for AltiVec registers (W) | 32+P+R+A+L
601 +---------------------------------------+
602 | AltiVec alignment padding (Y) | 32+P+R+A+L+W
603 +---------------------------------------+
604 | Save area for GP registers (G) | 32+P+R+A+L+W+Y
605 +---------------------------------------+
606 | Save area for FP registers (F) | 32+P+R+A+L+W+Y+G
607 +---------------------------------------+
608 old SP->| back chain to caller's caller | 32+P+R+A+L+W+Y+G+F
609 +---------------------------------------+
611 * If the alloca area is present, the parameter save area is
612 padded so that the former starts 16-byte aligned.
614 V.4 stack frames look like:
616 SP----> +---------------------------------------+
617 | back chain to caller | 0
618 +---------------------------------------+
619 | caller's saved LR | 4
620 +---------------------------------------+
621 | Parameter save area (+padding*) (P) | 8
622 +---------------------------------------+
623 | Alloca space (A) | 8+P
624 +---------------------------------------+
625 | Varargs save area (V) | 8+P+A
626 +---------------------------------------+
627 | Local variable space (L) | 8+P+A+V
628 +---------------------------------------+
629 | Float/int conversion temporary (X) | 8+P+A+V+L
630 +---------------------------------------+
631 | Save area for AltiVec registers (W) | 8+P+A+V+L+X
632 +---------------------------------------+
633 | AltiVec alignment padding (Y) | 8+P+A+V+L+X+W
634 +---------------------------------------+
635 | Save area for VRSAVE register (Z) | 8+P+A+V+L+X+W+Y
636 +---------------------------------------+
637 | saved CR (C) | 8+P+A+V+L+X+W+Y+Z
638 +---------------------------------------+
639 | Save area for GP registers (G) | 8+P+A+V+L+X+W+Y+Z+C
640 +---------------------------------------+
641 | Save area for FP registers (F) | 8+P+A+V+L+X+W+Y+Z+C+G
642 +---------------------------------------+
643 old SP->| back chain to caller's caller |
644 +---------------------------------------+
646 * If the alloca area is present and the required alignment is
647 16 bytes, the parameter save area is padded so that the
648 alloca area starts 16-byte aligned.
650 The required alignment for V.4 is 16 bytes, or 8 bytes if -meabi is
651 given. (But note below and in sysv4.h that we require only 8 and
652 may round up the size of our stack frame anyways. The historical
653 reason is early versions of powerpc-linux which didn't properly
654 align the stack at program startup. A happy side-effect is that
655 -mno-eabi libraries can be used with -meabi programs.)
657 The EABI configuration defaults to the V.4 layout. However,
658 the stack alignment requirements may differ. If -mno-eabi is not
659 given, the required stack alignment is 8 bytes; if -mno-eabi is
660 given, the required alignment is 16 bytes. (But see V.4 comment
661 above.) */
663 #ifndef ABI_STACK_BOUNDARY
664 #define ABI_STACK_BOUNDARY STACK_BOUNDARY
665 #endif
667 rs6000_stack_t *
668 rs6000_stack_info (void)
670 /* We should never be called for thunks, we are not set up for that. */
671 gcc_assert (!cfun->is_thunk);
673 rs6000_stack_t *info = &stack_info;
674 int reg_size = TARGET_32BIT ? 4 : 8;
675 int ehrd_size;
676 int ehcr_size;
677 int save_align;
678 int first_gp;
679 HOST_WIDE_INT non_fixed_size;
680 bool using_static_chain_p;
682 if (reload_completed && info->reload_completed)
683 return info;
685 memset (info, 0, sizeof (*info));
686 info->reload_completed = reload_completed;
688 /* Select which calling sequence. */
689 info->abi = DEFAULT_ABI;
691 /* Calculate which registers need to be saved & save area size. */
692 info->first_gp_reg_save = first_reg_to_save ();
693 /* Assume that we will have to save RS6000_PIC_OFFSET_TABLE_REGNUM,
694 even if it currently looks like we won't. Reload may need it to
695 get at a constant; if so, it will have already created a constant
696 pool entry for it. */
697 if (((TARGET_TOC && TARGET_MINIMAL_TOC)
698 || (flag_pic == 1 && DEFAULT_ABI == ABI_V4)
699 || (flag_pic && DEFAULT_ABI == ABI_DARWIN))
700 && crtl->uses_const_pool
701 && info->first_gp_reg_save > RS6000_PIC_OFFSET_TABLE_REGNUM)
702 first_gp = RS6000_PIC_OFFSET_TABLE_REGNUM;
703 else
704 first_gp = info->first_gp_reg_save;
706 info->gp_size = reg_size * (32 - first_gp);
708 info->first_fp_reg_save = first_fp_reg_to_save ();
709 info->fp_size = 8 * (64 - info->first_fp_reg_save);
711 info->first_altivec_reg_save = first_altivec_reg_to_save ();
712 info->altivec_size = 16 * (LAST_ALTIVEC_REGNO + 1
713 - info->first_altivec_reg_save);
715 /* Does this function call anything (apart from sibling calls)? */
716 info->calls_p = (!crtl->is_leaf || cfun->machine->ra_needs_full_frame);
717 info->rop_hash_size = 0;
719 if (TARGET_POWER10
720 && info->calls_p
721 && DEFAULT_ABI == ABI_ELFv2
722 && rs6000_rop_protect)
723 info->rop_hash_size = 8;
724 else if (rs6000_rop_protect && DEFAULT_ABI != ABI_ELFv2)
726 /* We can't check this in rs6000_option_override_internal since
727 DEFAULT_ABI isn't established yet. */
728 error ("%qs requires the ELFv2 ABI", "-mrop-protect");
731 /* Determine if we need to save the condition code registers. */
732 if (save_reg_p (CR2_REGNO)
733 || save_reg_p (CR3_REGNO)
734 || save_reg_p (CR4_REGNO))
736 info->cr_save_p = 1;
737 if (DEFAULT_ABI == ABI_V4)
738 info->cr_size = reg_size;
741 /* If the current function calls __builtin_eh_return, then we need
742 to allocate stack space for registers that will hold data for
743 the exception handler. */
744 if (crtl->calls_eh_return)
746 unsigned int i;
747 for (i = 0; EH_RETURN_DATA_REGNO (i) != INVALID_REGNUM; ++i)
748 continue;
750 ehrd_size = i * UNITS_PER_WORD;
752 else
753 ehrd_size = 0;
755 /* In the ELFv2 ABI, we also need to allocate space for separate
756 CR field save areas if the function calls __builtin_eh_return. */
757 if (DEFAULT_ABI == ABI_ELFv2 && crtl->calls_eh_return)
759 /* This hard-codes that we have three call-saved CR fields. */
760 ehcr_size = 3 * reg_size;
761 /* We do *not* use the regular CR save mechanism. */
762 info->cr_save_p = 0;
764 else
765 ehcr_size = 0;
767 /* Determine various sizes. */
768 info->reg_size = reg_size;
769 info->fixed_size = RS6000_SAVE_AREA;
770 info->vars_size = RS6000_ALIGN (get_frame_size (), 8);
771 if (cfun->calls_alloca)
772 info->parm_size =
773 RS6000_ALIGN (crtl->outgoing_args_size + info->fixed_size,
774 STACK_BOUNDARY / BITS_PER_UNIT) - info->fixed_size;
775 else
776 info->parm_size = RS6000_ALIGN (crtl->outgoing_args_size,
777 TARGET_ALTIVEC ? 16 : 8);
778 if (FRAME_GROWS_DOWNWARD)
779 info->vars_size
780 += RS6000_ALIGN (info->fixed_size + info->vars_size + info->parm_size,
781 ABI_STACK_BOUNDARY / BITS_PER_UNIT)
782 - (info->fixed_size + info->vars_size + info->parm_size);
784 if (TARGET_ALTIVEC_ABI)
785 info->vrsave_mask = compute_vrsave_mask ();
787 if (TARGET_ALTIVEC_VRSAVE && info->vrsave_mask)
788 info->vrsave_size = 4;
790 compute_save_world_info (info);
792 /* Calculate the offsets. */
793 switch (DEFAULT_ABI)
795 case ABI_NONE:
796 default:
797 gcc_unreachable ();
799 case ABI_AIX:
800 case ABI_ELFv2:
801 case ABI_DARWIN:
802 info->fp_save_offset = -info->fp_size;
803 info->gp_save_offset = info->fp_save_offset - info->gp_size;
805 if (TARGET_ALTIVEC_ABI)
807 info->vrsave_save_offset = info->gp_save_offset - info->vrsave_size;
809 /* Align stack so vector save area is on a quadword boundary.
810 The padding goes above the vectors. */
811 if (info->altivec_size != 0)
812 info->altivec_padding_size = info->vrsave_save_offset & 0xF;
814 info->altivec_save_offset = info->vrsave_save_offset
815 - info->altivec_padding_size
816 - info->altivec_size;
817 gcc_assert (info->altivec_size == 0
818 || info->altivec_save_offset % 16 == 0);
820 /* Adjust for AltiVec case. */
821 info->ehrd_offset = info->altivec_save_offset - ehrd_size;
823 /* Adjust for ROP protection. */
824 info->rop_hash_save_offset
825 = info->altivec_save_offset - info->rop_hash_size;
826 info->ehrd_offset -= info->rop_hash_size;
828 else
829 info->ehrd_offset = info->gp_save_offset - ehrd_size;
831 info->ehcr_offset = info->ehrd_offset - ehcr_size;
832 info->cr_save_offset = reg_size; /* first word when 64-bit. */
833 info->lr_save_offset = 2*reg_size;
834 break;
836 case ABI_V4:
837 info->fp_save_offset = -info->fp_size;
838 info->gp_save_offset = info->fp_save_offset - info->gp_size;
839 info->cr_save_offset = info->gp_save_offset - info->cr_size;
841 if (TARGET_ALTIVEC_ABI)
843 info->vrsave_save_offset = info->cr_save_offset - info->vrsave_size;
845 /* Align stack so vector save area is on a quadword boundary. */
846 if (info->altivec_size != 0)
847 info->altivec_padding_size = 16 - (-info->vrsave_save_offset % 16);
849 info->altivec_save_offset = info->vrsave_save_offset
850 - info->altivec_padding_size
851 - info->altivec_size;
853 /* Adjust for AltiVec case. */
854 info->ehrd_offset = info->altivec_save_offset;
856 else
857 info->ehrd_offset = info->cr_save_offset;
859 info->ehrd_offset -= ehrd_size;
860 info->lr_save_offset = reg_size;
863 save_align = (TARGET_ALTIVEC_ABI || DEFAULT_ABI == ABI_DARWIN) ? 16 : 8;
864 info->save_size = RS6000_ALIGN (info->fp_size
865 + info->gp_size
866 + info->altivec_size
867 + info->altivec_padding_size
868 + info->rop_hash_size
869 + ehrd_size
870 + ehcr_size
871 + info->cr_size
872 + info->vrsave_size,
873 save_align);
875 non_fixed_size = info->vars_size + info->parm_size + info->save_size;
877 info->total_size = RS6000_ALIGN (non_fixed_size + info->fixed_size,
878 ABI_STACK_BOUNDARY / BITS_PER_UNIT);
880 /* Determine if we need to save the link register. */
881 if (info->calls_p
882 || ((DEFAULT_ABI == ABI_AIX || DEFAULT_ABI == ABI_ELFv2)
883 && crtl->profile
884 && !TARGET_PROFILE_KERNEL)
885 || (DEFAULT_ABI == ABI_V4 && cfun->calls_alloca)
886 #ifdef TARGET_RELOCATABLE
887 || (DEFAULT_ABI == ABI_V4
888 && (TARGET_RELOCATABLE || flag_pic > 1)
889 && !constant_pool_empty_p ())
890 #endif
891 || rs6000_ra_ever_killed ())
892 info->lr_save_p = 1;
894 using_static_chain_p = (cfun->static_chain_decl != NULL_TREE
895 && df_regs_ever_live_p (STATIC_CHAIN_REGNUM)
896 && call_used_or_fixed_reg_p (STATIC_CHAIN_REGNUM));
897 info->savres_strategy = rs6000_savres_strategy (info, using_static_chain_p);
899 if (!(info->savres_strategy & SAVE_INLINE_GPRS)
900 || !(info->savres_strategy & SAVE_INLINE_FPRS)
901 || !(info->savres_strategy & SAVE_INLINE_VRS)
902 || !(info->savres_strategy & REST_INLINE_GPRS)
903 || !(info->savres_strategy & REST_INLINE_FPRS)
904 || !(info->savres_strategy & REST_INLINE_VRS))
905 info->lr_save_p = 1;
907 if (info->lr_save_p)
908 df_set_regs_ever_live (LR_REGNO, true);
910 /* Determine if we need to allocate any stack frame:
912 For AIX we need to push the stack if a frame pointer is needed
913 (because the stack might be dynamically adjusted), if we are
914 debugging, if we make calls, or if the sum of fp_save, gp_save,
915 and local variables are more than the space needed to save all
916 non-volatile registers: 32-bit: 18*8 + 19*4 = 220 or 64-bit: 18*8
917 + 18*8 = 288 (GPR13 reserved).
919 For V.4 we don't have the stack cushion that AIX uses, but assume
920 that the debugger can handle stackless frames. */
922 if (info->calls_p)
923 info->push_p = 1;
925 else if (DEFAULT_ABI == ABI_V4)
926 info->push_p = non_fixed_size != 0;
928 else if (frame_pointer_needed)
929 info->push_p = 1;
931 else
932 info->push_p = non_fixed_size > (TARGET_32BIT ? 220 : 288);
934 return info;
937 static void
938 debug_stack_info (rs6000_stack_t *info)
940 const char *abi_string;
942 if (! info)
943 info = rs6000_stack_info ();
945 fprintf (stderr, "\nStack information for function %s:\n",
946 ((current_function_decl && DECL_NAME (current_function_decl))
947 ? IDENTIFIER_POINTER (DECL_NAME (current_function_decl))
948 : "<unknown>"));
950 switch (info->abi)
952 default: abi_string = "Unknown"; break;
953 case ABI_NONE: abi_string = "NONE"; break;
954 case ABI_AIX: abi_string = "AIX"; break;
955 case ABI_ELFv2: abi_string = "ELFv2"; break;
956 case ABI_DARWIN: abi_string = "Darwin"; break;
957 case ABI_V4: abi_string = "V.4"; break;
960 fprintf (stderr, "\tABI = %5s\n", abi_string);
962 if (TARGET_ALTIVEC_ABI)
963 fprintf (stderr, "\tALTIVEC ABI extensions enabled.\n");
965 if (info->first_gp_reg_save != 32)
966 fprintf (stderr, "\tfirst_gp_reg_save = %5d\n", info->first_gp_reg_save);
968 if (info->first_fp_reg_save != 64)
969 fprintf (stderr, "\tfirst_fp_reg_save = %5d\n", info->first_fp_reg_save);
971 if (info->first_altivec_reg_save <= LAST_ALTIVEC_REGNO)
972 fprintf (stderr, "\tfirst_altivec_reg_save = %5d\n",
973 info->first_altivec_reg_save);
975 if (info->lr_save_p)
976 fprintf (stderr, "\tlr_save_p = %5d\n", info->lr_save_p);
978 if (info->cr_save_p)
979 fprintf (stderr, "\tcr_save_p = %5d\n", info->cr_save_p);
981 if (info->vrsave_mask)
982 fprintf (stderr, "\tvrsave_mask = 0x%x\n", info->vrsave_mask);
984 if (info->push_p)
985 fprintf (stderr, "\tpush_p = %5d\n", info->push_p);
987 if (info->calls_p)
988 fprintf (stderr, "\tcalls_p = %5d\n", info->calls_p);
990 if (info->gp_size)
991 fprintf (stderr, "\tgp_save_offset = %5d\n", info->gp_save_offset);
993 if (info->fp_size)
994 fprintf (stderr, "\tfp_save_offset = %5d\n", info->fp_save_offset);
996 if (info->altivec_size)
997 fprintf (stderr, "\taltivec_save_offset = %5d\n",
998 info->altivec_save_offset);
1000 if (info->vrsave_size)
1001 fprintf (stderr, "\tvrsave_save_offset = %5d\n",
1002 info->vrsave_save_offset);
1004 if (info->rop_hash_size)
1005 fprintf (stderr, "\trop_hash_save_offset = %5d\n",
1006 info->rop_hash_save_offset);
1008 if (info->lr_save_p)
1009 fprintf (stderr, "\tlr_save_offset = %5d\n", info->lr_save_offset);
1011 if (info->cr_save_p)
1012 fprintf (stderr, "\tcr_save_offset = %5d\n", info->cr_save_offset);
1014 if (info->varargs_save_offset)
1015 fprintf (stderr, "\tvarargs_save_offset = %5d\n", info->varargs_save_offset);
1017 if (info->total_size)
1018 fprintf (stderr, "\ttotal_size = " HOST_WIDE_INT_PRINT_DEC"\n",
1019 info->total_size);
1021 if (info->vars_size)
1022 fprintf (stderr, "\tvars_size = " HOST_WIDE_INT_PRINT_DEC"\n",
1023 info->vars_size);
1025 if (info->parm_size)
1026 fprintf (stderr, "\tparm_size = %5d\n", info->parm_size);
1028 if (info->fixed_size)
1029 fprintf (stderr, "\tfixed_size = %5d\n", info->fixed_size);
1031 if (info->gp_size)
1032 fprintf (stderr, "\tgp_size = %5d\n", info->gp_size);
1034 if (info->fp_size)
1035 fprintf (stderr, "\tfp_size = %5d\n", info->fp_size);
1037 if (info->altivec_size)
1038 fprintf (stderr, "\taltivec_size = %5d\n", info->altivec_size);
1040 if (info->vrsave_size)
1041 fprintf (stderr, "\tvrsave_size = %5d\n", info->vrsave_size);
1043 if (info->altivec_padding_size)
1044 fprintf (stderr, "\taltivec_padding_size= %5d\n",
1045 info->altivec_padding_size);
1047 if (info->rop_hash_size)
1048 fprintf (stderr, "\trop_hash_size = %5d\n", info->rop_hash_size);
1050 if (info->cr_size)
1051 fprintf (stderr, "\tcr_size = %5d\n", info->cr_size);
1053 if (info->save_size)
1054 fprintf (stderr, "\tsave_size = %5d\n", info->save_size);
1056 if (info->reg_size != 4)
1057 fprintf (stderr, "\treg_size = %5d\n", info->reg_size);
1059 fprintf (stderr, "\tsave-strategy = %04x\n", info->savres_strategy);
1061 if (info->abi == ABI_DARWIN)
1062 fprintf (stderr, "\tWORLD_SAVE_P = %5d\n", WORLD_SAVE_P(info));
1064 fprintf (stderr, "\n");
1068 rs6000_return_addr (int count, rtx frame)
1070 /* We can't use get_hard_reg_initial_val for LR when count == 0 if LR
1071 is trashed by the prologue, as it is for PIC on ABI_V4 and Darwin. */
1072 if (count != 0
1073 || ((DEFAULT_ABI == ABI_V4 || DEFAULT_ABI == ABI_DARWIN) && flag_pic))
1075 cfun->machine->ra_needs_full_frame = 1;
1077 if (count == 0)
1078 /* FRAME is set to frame_pointer_rtx by the generic code, but that
1079 is good for loading 0(r1) only when !FRAME_GROWS_DOWNWARD. */
1080 frame = stack_pointer_rtx;
1081 rtx prev_frame_addr = memory_address (Pmode, frame);
1082 rtx prev_frame = copy_to_reg (gen_rtx_MEM (Pmode, prev_frame_addr));
1083 rtx lr_save_off = plus_constant (Pmode,
1084 prev_frame, RETURN_ADDRESS_OFFSET);
1085 rtx lr_save_addr = memory_address (Pmode, lr_save_off);
1086 return gen_rtx_MEM (Pmode, lr_save_addr);
1089 cfun->machine->ra_need_lr = 1;
1090 return get_hard_reg_initial_val (Pmode, LR_REGNO);
1093 /* Helper function for rs6000_function_ok_for_sibcall. */
1095 bool
1096 rs6000_decl_ok_for_sibcall (tree decl)
1098 /* Sibcalls are always fine for the Darwin ABI. */
1099 if (DEFAULT_ABI == ABI_DARWIN)
1100 return true;
1102 if (DEFAULT_ABI == ABI_AIX || DEFAULT_ABI == ABI_ELFv2)
1104 /* A function compiled using the PC-relative addressing model does not
1105 use a TOC pointer; nor is it guaranteed to preserve the value of
1106 r2 for its caller's TOC. Such a function may make sibcalls to any
1107 function, whether local or external, without restriction based on
1108 TOC-save/restore rules. */
1109 if (rs6000_pcrel_p ())
1110 return true;
1112 /* Otherwise, under the AIX or ELFv2 ABIs we can't allow sibcalls
1113 to non-local functions, because the callee may not preserve the
1114 TOC pointer, and there's no way to ensure we restore the TOC when
1115 we return. */
1116 if (!decl || DECL_EXTERNAL (decl) || DECL_WEAK (decl)
1117 || !(*targetm.binds_local_p) (decl))
1118 return false;
1120 /* A local sibcall from a function that preserves the TOC pointer
1121 to a function that does not is invalid for the same reason. */
1122 if (rs6000_fndecl_pcrel_p (decl))
1123 return false;
1125 return true;
1128 /* With the secure-plt SYSV ABI we can't make non-local calls when
1129 -fpic/PIC because the plt call stubs use r30. */
1130 if (DEFAULT_ABI != ABI_V4
1131 || (TARGET_SECURE_PLT
1132 && flag_pic
1133 && (!decl || !((*targetm.binds_local_p) (decl)))))
1134 return false;
1136 return true;
1139 /* Say whether a function is a candidate for sibcall handling or not. */
1141 bool
1142 rs6000_function_ok_for_sibcall (tree decl, tree exp)
1144 tree fntype;
1146 /* The sibcall epilogue may clobber the static chain register.
1147 ??? We could work harder and avoid that, but it's probably
1148 not worth the hassle in practice. */
1149 if (CALL_EXPR_STATIC_CHAIN (exp))
1150 return false;
1152 if (decl)
1153 fntype = TREE_TYPE (decl);
1154 else
1155 fntype = TREE_TYPE (TREE_TYPE (CALL_EXPR_FN (exp)));
1157 /* We can't do it if the called function has more vector parameters
1158 than the current function; there's nowhere to put the VRsave code. */
1159 if (TARGET_ALTIVEC_ABI
1160 && TARGET_ALTIVEC_VRSAVE
1161 && !(decl && decl == current_function_decl))
1163 function_args_iterator args_iter;
1164 tree type;
1165 int nvreg = 0;
1167 /* Functions with vector parameters are required to have a
1168 prototype, so the argument type info must be available
1169 here. */
1170 FOREACH_FUNCTION_ARGS(fntype, type, args_iter)
1171 if (VECTOR_TYPE_P (type)
1172 && ALTIVEC_OR_VSX_VECTOR_MODE (TYPE_MODE (type)))
1173 nvreg++;
1175 FOREACH_FUNCTION_ARGS(TREE_TYPE (current_function_decl), type, args_iter)
1176 if (VECTOR_TYPE_P (type)
1177 && ALTIVEC_OR_VSX_VECTOR_MODE (TYPE_MODE (type)))
1178 nvreg--;
1180 if (nvreg > 0)
1181 return false;
1184 if (rs6000_decl_ok_for_sibcall (decl))
1186 tree attr_list = TYPE_ATTRIBUTES (fntype);
1188 if (!lookup_attribute ("longcall", attr_list)
1189 || lookup_attribute ("shortcall", attr_list))
1190 return true;
1193 return false;
1196 static int
1197 rs6000_ra_ever_killed (void)
1199 rtx_insn *top;
1200 rtx reg;
1201 rtx_insn *insn;
1203 if (cfun->is_thunk)
1204 return 0;
1206 if (cfun->machine->lr_save_state)
1207 return cfun->machine->lr_save_state - 1;
1209 /* regs_ever_live has LR marked as used if any sibcalls are present,
1210 but this should not force saving and restoring in the
1211 pro/epilogue. Likewise, reg_set_between_p thinks a sibcall
1212 clobbers LR, so that is inappropriate. */
1214 /* Also, the prologue can generate a store into LR that
1215 doesn't really count, like this:
1217 move LR->R0
1218 bcl to set PIC register
1219 move LR->R31
1220 move R0->LR
1222 When we're called from the epilogue, we need to avoid counting
1223 this as a store. */
1225 push_topmost_sequence ();
1226 top = get_insns ();
1227 pop_topmost_sequence ();
1228 reg = gen_rtx_REG (Pmode, LR_REGNO);
1230 for (insn = NEXT_INSN (top); insn != NULL_RTX; insn = NEXT_INSN (insn))
1232 if (INSN_P (insn))
1234 if (CALL_P (insn))
1236 if (!SIBLING_CALL_P (insn))
1237 return 1;
1239 else if (find_regno_note (insn, REG_INC, LR_REGNO))
1240 return 1;
1241 else if (set_of (reg, insn) != NULL_RTX
1242 && !prologue_epilogue_contains (insn))
1243 return 1;
1246 return 0;
1249 /* Emit instructions needed to load the TOC register.
1250 This is only needed when TARGET_TOC, TARGET_MINIMAL_TOC, and there is
1251 a constant pool; or for SVR4 -fpic. */
1253 void
1254 rs6000_emit_load_toc_table (int fromprolog)
1256 rtx dest;
1257 dest = gen_rtx_REG (Pmode, RS6000_PIC_OFFSET_TABLE_REGNUM);
1259 if (TARGET_ELF && TARGET_SECURE_PLT && DEFAULT_ABI == ABI_V4 && flag_pic)
1261 char buf[30];
1262 rtx lab, tmp1, tmp2, got;
1264 lab = gen_label_rtx ();
1265 ASM_GENERATE_INTERNAL_LABEL (buf, "L", CODE_LABEL_NUMBER (lab));
1266 lab = gen_rtx_SYMBOL_REF (Pmode, ggc_strdup (buf));
1267 if (flag_pic == 2)
1269 got = gen_rtx_SYMBOL_REF (Pmode, ggc_strdup (toc_label_name));
1270 need_toc_init = 1;
1272 else
1273 got = rs6000_got_sym ();
1274 tmp1 = tmp2 = dest;
1275 if (!fromprolog)
1277 tmp1 = gen_reg_rtx (Pmode);
1278 tmp2 = gen_reg_rtx (Pmode);
1280 emit_insn (gen_load_toc_v4_PIC_1 (lab));
1281 emit_move_insn (tmp1, gen_rtx_REG (Pmode, LR_REGNO));
1282 emit_insn (gen_load_toc_v4_PIC_3b (tmp2, tmp1, got, lab));
1283 emit_insn (gen_load_toc_v4_PIC_3c (dest, tmp2, got, lab));
1285 else if (TARGET_ELF && DEFAULT_ABI == ABI_V4 && flag_pic == 1)
1287 emit_insn (gen_load_toc_v4_pic_si ());
1288 emit_move_insn (dest, gen_rtx_REG (Pmode, LR_REGNO));
1290 else if (TARGET_ELF && DEFAULT_ABI == ABI_V4 && flag_pic == 2)
1292 char buf[30];
1293 rtx temp0 = (fromprolog
1294 ? gen_rtx_REG (Pmode, 0)
1295 : gen_reg_rtx (Pmode));
1297 if (fromprolog)
1299 rtx symF, symL;
1301 ASM_GENERATE_INTERNAL_LABEL (buf, "LCF", rs6000_pic_labelno);
1302 symF = gen_rtx_SYMBOL_REF (Pmode, ggc_strdup (buf));
1304 ASM_GENERATE_INTERNAL_LABEL (buf, "LCL", rs6000_pic_labelno);
1305 symL = gen_rtx_SYMBOL_REF (Pmode, ggc_strdup (buf));
1307 emit_insn (gen_load_toc_v4_PIC_1 (symF));
1308 emit_move_insn (dest, gen_rtx_REG (Pmode, LR_REGNO));
1309 emit_insn (gen_load_toc_v4_PIC_2 (temp0, dest, symL, symF));
1311 else
1313 rtx tocsym, lab;
1315 tocsym = gen_rtx_SYMBOL_REF (Pmode, ggc_strdup (toc_label_name));
1316 need_toc_init = 1;
1317 lab = gen_label_rtx ();
1318 emit_insn (gen_load_toc_v4_PIC_1b (tocsym, lab));
1319 emit_move_insn (dest, gen_rtx_REG (Pmode, LR_REGNO));
1320 if (TARGET_LINK_STACK)
1321 emit_insn (gen_addsi3 (dest, dest, GEN_INT (4)));
1322 emit_move_insn (temp0, gen_rtx_MEM (Pmode, dest));
1324 emit_insn (gen_addsi3 (dest, temp0, dest));
1326 else if (TARGET_ELF && !TARGET_AIX && flag_pic == 0 && TARGET_MINIMAL_TOC)
1328 /* This is for AIX code running in non-PIC ELF32. */
1329 rtx realsym = gen_rtx_SYMBOL_REF (Pmode, ggc_strdup (toc_label_name));
1331 need_toc_init = 1;
1332 emit_insn (gen_elf_high (dest, realsym));
1333 emit_insn (gen_elf_low (dest, dest, realsym));
1335 else
1337 gcc_assert (DEFAULT_ABI == ABI_AIX || DEFAULT_ABI == ABI_ELFv2);
1339 if (TARGET_32BIT)
1340 emit_insn (gen_load_toc_aix_si (dest));
1341 else
1342 emit_insn (gen_load_toc_aix_di (dest));
1346 /* Emit instructions to restore the link register after determining where
1347 its value has been stored. */
1349 void
1350 rs6000_emit_eh_reg_restore (rtx source, rtx scratch)
1352 rs6000_stack_t *info = rs6000_stack_info ();
1353 rtx operands[2];
1355 operands[0] = source;
1356 operands[1] = scratch;
1358 if (info->lr_save_p)
1360 rtx frame_rtx = stack_pointer_rtx;
1361 HOST_WIDE_INT sp_offset = 0;
1362 rtx tmp;
1364 if (frame_pointer_needed
1365 || cfun->calls_alloca
1366 || info->total_size > 32767)
1368 tmp = gen_frame_mem (Pmode, frame_rtx);
1369 emit_move_insn (operands[1], tmp);
1370 frame_rtx = operands[1];
1372 else if (info->push_p)
1373 sp_offset = info->total_size;
1375 tmp = plus_constant (Pmode, frame_rtx,
1376 info->lr_save_offset + sp_offset);
1377 tmp = gen_frame_mem (Pmode, tmp);
1378 emit_move_insn (tmp, operands[0]);
1380 else
1381 emit_move_insn (gen_rtx_REG (Pmode, LR_REGNO), operands[0]);
1383 /* Freeze lr_save_p. We've just emitted rtl that depends on the
1384 state of lr_save_p so any change from here on would be a bug. In
1385 particular, stop rs6000_ra_ever_killed from considering the SET
1386 of lr we may have added just above. */
1387 cfun->machine->lr_save_state = info->lr_save_p + 1;
1390 /* This returns nonzero if the current function uses the TOC. This is
1391 determined by the presence of (use (unspec ... UNSPEC_TOC)), which
1392 is generated by the ABI_V4 load_toc_* patterns.
1393 Return 2 instead of 1 if the load_toc_* pattern is in the function
1394 partition that doesn't start the function. */
1395 #if TARGET_ELF
1397 uses_TOC (void)
1399 rtx_insn *insn;
1400 int ret = 1;
1402 for (insn = get_insns (); insn; insn = NEXT_INSN (insn))
1404 if (INSN_P (insn))
1406 rtx pat = PATTERN (insn);
1407 int i;
1409 if (GET_CODE (pat) == PARALLEL)
1410 for (i = 0; i < XVECLEN (pat, 0); i++)
1412 rtx sub = XVECEXP (pat, 0, i);
1413 if (GET_CODE (sub) == USE)
1415 sub = XEXP (sub, 0);
1416 if (GET_CODE (sub) == UNSPEC
1417 && XINT (sub, 1) == UNSPEC_TOC)
1418 return ret;
1422 else if (crtl->has_bb_partition
1423 && NOTE_P (insn)
1424 && NOTE_KIND (insn) == NOTE_INSN_SWITCH_TEXT_SECTIONS)
1425 ret = 2;
1427 return 0;
1429 #endif
1431 /* Issue assembly directives that create a reference to the given DWARF
1432 FRAME_TABLE_LABEL from the current function section. */
1433 void
1434 rs6000_aix_asm_output_dwarf_table_ref (char * frame_table_label)
1436 fprintf (asm_out_file, "\t.ref %s\n",
1437 (* targetm.strip_name_encoding) (frame_table_label));
1440 /* This ties together stack memory (MEM with an alias set of frame_alias_set)
1441 and the change to the stack pointer. */
1443 static void
1444 rs6000_emit_stack_tie (rtx fp, bool hard_frame_needed)
1446 rtvec p;
1447 int i;
1448 rtx regs[3];
1450 i = 0;
1451 regs[i++] = gen_rtx_REG (Pmode, STACK_POINTER_REGNUM);
1452 if (hard_frame_needed)
1453 regs[i++] = gen_rtx_REG (Pmode, HARD_FRAME_POINTER_REGNUM);
1454 if (!(REGNO (fp) == STACK_POINTER_REGNUM
1455 || (hard_frame_needed
1456 && REGNO (fp) == HARD_FRAME_POINTER_REGNUM)))
1457 regs[i++] = fp;
1459 p = rtvec_alloc (i);
1460 while (--i >= 0)
1462 rtx mem = gen_frame_mem (BLKmode, regs[i]);
1463 RTVEC_ELT (p, i) = gen_rtx_SET (mem, const0_rtx);
1466 emit_insn (gen_stack_tie (gen_rtx_PARALLEL (VOIDmode, p)));
1469 /* Allocate SIZE_INT bytes on the stack using a store with update style insn
1470 and set the appropriate attributes for the generated insn. Return the
1471 first insn which adjusts the stack pointer or the last insn before
1472 the stack adjustment loop.
1474 SIZE_INT is used to create the CFI note for the allocation.
1476 SIZE_RTX is an rtx containing the size of the adjustment. Note that
1477 since stacks grow to lower addresses its runtime value is -SIZE_INT.
1479 ORIG_SP contains the backchain value that must be stored at *sp. */
1481 static rtx_insn *
1482 rs6000_emit_allocate_stack_1 (HOST_WIDE_INT size_int, rtx orig_sp)
1484 rtx_insn *insn;
1486 rtx size_rtx = GEN_INT (-size_int);
1487 if (size_int > 32767)
1489 rtx tmp_reg = gen_rtx_REG (Pmode, 0);
1490 /* Need a note here so that try_split doesn't get confused. */
1491 if (get_last_insn () == NULL_RTX)
1492 emit_note (NOTE_INSN_DELETED);
1493 insn = emit_move_insn (tmp_reg, size_rtx);
1494 try_split (PATTERN (insn), insn, 0);
1495 size_rtx = tmp_reg;
1498 if (TARGET_32BIT)
1499 insn = emit_insn (gen_movsi_update_stack (stack_pointer_rtx,
1500 stack_pointer_rtx,
1501 size_rtx,
1502 orig_sp));
1503 else
1504 insn = emit_insn (gen_movdi_update_stack (stack_pointer_rtx,
1505 stack_pointer_rtx,
1506 size_rtx,
1507 orig_sp));
1508 rtx par = PATTERN (insn);
1509 gcc_assert (GET_CODE (par) == PARALLEL);
1510 rtx set = XVECEXP (par, 0, 0);
1511 gcc_assert (GET_CODE (set) == SET);
1512 rtx mem = SET_DEST (set);
1513 gcc_assert (MEM_P (mem));
1514 MEM_NOTRAP_P (mem) = 1;
1515 set_mem_alias_set (mem, get_frame_alias_set ());
1517 RTX_FRAME_RELATED_P (insn) = 1;
1518 add_reg_note (insn, REG_FRAME_RELATED_EXPR,
1519 gen_rtx_SET (stack_pointer_rtx,
1520 gen_rtx_PLUS (Pmode,
1521 stack_pointer_rtx,
1522 GEN_INT (-size_int))));
1524 /* Emit a blockage to ensure the allocation/probing insns are
1525 not optimized, combined, removed, etc. Add REG_STACK_CHECK
1526 note for similar reasons. */
1527 if (flag_stack_clash_protection)
1529 add_reg_note (insn, REG_STACK_CHECK, const0_rtx);
1530 emit_insn (gen_blockage ());
1533 return insn;
1536 static HOST_WIDE_INT
1537 get_stack_clash_protection_probe_interval (void)
1539 return (HOST_WIDE_INT_1U
1540 << param_stack_clash_protection_probe_interval);
1543 static HOST_WIDE_INT
1544 get_stack_clash_protection_guard_size (void)
1546 return (HOST_WIDE_INT_1U
1547 << param_stack_clash_protection_guard_size);
1550 /* Allocate ORIG_SIZE bytes on the stack and probe the newly
1551 allocated space every STACK_CLASH_PROTECTION_PROBE_INTERVAL bytes.
1553 COPY_REG, if non-null, should contain a copy of the original
1554 stack pointer at exit from this function.
1556 This is subtly different than the Ada probing in that it tries hard to
1557 prevent attacks that jump the stack guard. Thus it is never allowed to
1558 allocate more than STACK_CLASH_PROTECTION_PROBE_INTERVAL bytes of stack
1559 space without a suitable probe. */
1560 static rtx_insn *
1561 rs6000_emit_probe_stack_range_stack_clash (HOST_WIDE_INT orig_size,
1562 rtx copy_reg)
1564 rtx orig_sp = copy_reg;
1566 HOST_WIDE_INT probe_interval = get_stack_clash_protection_probe_interval ();
1568 /* Round the size down to a multiple of PROBE_INTERVAL. */
1569 HOST_WIDE_INT rounded_size = ROUND_DOWN (orig_size, probe_interval);
1571 /* If explicitly requested,
1572 or the rounded size is not the same as the original size
1573 or the rounded size is greater than a page,
1574 then we will need a copy of the original stack pointer. */
1575 if (rounded_size != orig_size
1576 || rounded_size > probe_interval
1577 || copy_reg)
1579 /* If the caller did not request a copy of the incoming stack
1580 pointer, then we use r0 to hold the copy. */
1581 if (!copy_reg)
1582 orig_sp = gen_rtx_REG (Pmode, 0);
1583 emit_move_insn (orig_sp, stack_pointer_rtx);
1586 /* There's three cases here.
1588 One is a single probe which is the most common and most efficiently
1589 implemented as it does not have to have a copy of the original
1590 stack pointer if there are no residuals.
1592 Second is unrolled allocation/probes which we use if there's just
1593 a few of them. It needs to save the original stack pointer into a
1594 temporary for use as a source register in the allocation/probe.
1596 Last is a loop. This is the most uncommon case and least efficient. */
1597 rtx_insn *retval = NULL;
1598 if (rounded_size == probe_interval)
1600 retval = rs6000_emit_allocate_stack_1 (probe_interval, stack_pointer_rtx);
1602 dump_stack_clash_frame_info (PROBE_INLINE, rounded_size != orig_size);
1604 else if (rounded_size <= 8 * probe_interval)
1606 /* The ABI requires using the store with update insns to allocate
1607 space and store the backchain into the stack
1609 So we save the current stack pointer into a temporary, then
1610 emit the store-with-update insns to store the saved stack pointer
1611 into the right location in each new page. */
1612 for (int i = 0; i < rounded_size; i += probe_interval)
1614 rtx_insn *insn
1615 = rs6000_emit_allocate_stack_1 (probe_interval, orig_sp);
1617 /* Save the first stack adjustment in RETVAL. */
1618 if (i == 0)
1619 retval = insn;
1622 dump_stack_clash_frame_info (PROBE_INLINE, rounded_size != orig_size);
1624 else
1626 /* Compute the ending address. */
1627 rtx end_addr
1628 = copy_reg ? gen_rtx_REG (Pmode, 0) : gen_rtx_REG (Pmode, 12);
1629 rtx rs = GEN_INT (-rounded_size);
1630 rtx_insn *insn = gen_add3_insn (end_addr, stack_pointer_rtx, rs);
1631 if (insn == NULL)
1633 emit_move_insn (end_addr, rs);
1634 insn = gen_add3_insn (end_addr, end_addr, stack_pointer_rtx);
1635 gcc_assert (insn);
1637 bool add_note = false;
1638 if (!NONJUMP_INSN_P (insn) || NEXT_INSN (insn))
1639 add_note = true;
1640 else
1642 rtx set = single_set (insn);
1643 if (set == NULL_RTX
1644 || SET_DEST (set) != end_addr
1645 || GET_CODE (SET_SRC (set)) != PLUS
1646 || XEXP (SET_SRC (set), 0) != stack_pointer_rtx
1647 || XEXP (SET_SRC (set), 1) != rs)
1648 add_note = true;
1650 insn = emit_insn (insn);
1651 /* Describe the effect of INSN to the CFI engine, unless it
1652 is a single insn that describes it itself. */
1653 if (add_note)
1654 add_reg_note (insn, REG_FRAME_RELATED_EXPR,
1655 gen_rtx_SET (end_addr,
1656 gen_rtx_PLUS (Pmode, stack_pointer_rtx,
1657 rs)));
1658 RTX_FRAME_RELATED_P (insn) = 1;
1660 /* Emit the loop. */
1661 if (TARGET_64BIT)
1662 retval = emit_insn (gen_probe_stack_rangedi (stack_pointer_rtx,
1663 stack_pointer_rtx, orig_sp,
1664 end_addr));
1665 else
1666 retval = emit_insn (gen_probe_stack_rangesi (stack_pointer_rtx,
1667 stack_pointer_rtx, orig_sp,
1668 end_addr));
1669 RTX_FRAME_RELATED_P (retval) = 1;
1670 /* Describe the effect of INSN to the CFI engine. */
1671 add_reg_note (retval, REG_FRAME_RELATED_EXPR,
1672 gen_rtx_SET (stack_pointer_rtx, end_addr));
1674 /* Emit a blockage to ensure the allocation/probing insns are
1675 not optimized, combined, removed, etc. Other cases handle this
1676 within their call to rs6000_emit_allocate_stack_1. */
1677 emit_insn (gen_blockage ());
1679 dump_stack_clash_frame_info (PROBE_LOOP, rounded_size != orig_size);
1682 if (orig_size != rounded_size)
1684 /* Allocate (and implicitly probe) any residual space. */
1685 HOST_WIDE_INT residual = orig_size - rounded_size;
1687 rtx_insn *insn = rs6000_emit_allocate_stack_1 (residual, orig_sp);
1689 /* If the residual was the only allocation, then we can return the
1690 allocating insn. */
1691 if (!retval)
1692 retval = insn;
1695 return retval;
1698 /* Emit the correct code for allocating stack space, as insns.
1699 If COPY_REG, make sure a copy of the old frame is left there.
1700 The generated code may use hard register 0 as a temporary. */
1702 static rtx_insn *
1703 rs6000_emit_allocate_stack (HOST_WIDE_INT size, rtx copy_reg, int copy_off)
1705 rtx_insn *insn;
1706 rtx stack_reg = gen_rtx_REG (Pmode, STACK_POINTER_REGNUM);
1707 rtx tmp_reg = gen_rtx_REG (Pmode, 0);
1708 rtx todec = gen_int_mode (-size, Pmode);
1710 if (INTVAL (todec) != -size)
1712 warning (0, "stack frame too large");
1713 emit_insn (gen_trap ());
1714 return 0;
1717 if (crtl->limit_stack)
1719 if (REG_P (stack_limit_rtx)
1720 && REGNO (stack_limit_rtx) > 1
1721 && REGNO (stack_limit_rtx) <= 31)
1723 rtx_insn *insn
1724 = gen_add3_insn (tmp_reg, stack_limit_rtx, GEN_INT (size));
1725 gcc_assert (insn);
1726 emit_insn (insn);
1727 emit_insn (gen_cond_trap (LTU, stack_reg, tmp_reg, const0_rtx));
1729 else if (SYMBOL_REF_P (stack_limit_rtx)
1730 && TARGET_32BIT
1731 && DEFAULT_ABI == ABI_V4
1732 && !flag_pic)
1734 rtx toload = gen_rtx_CONST (VOIDmode,
1735 gen_rtx_PLUS (Pmode,
1736 stack_limit_rtx,
1737 GEN_INT (size)));
1739 /* We cannot use r0 with elf_low. Lamely solve this problem by
1740 moving registers around. */
1741 rtx r11_reg = gen_rtx_REG (Pmode, 11);
1742 emit_move_insn (tmp_reg, r11_reg);
1743 emit_insn (gen_elf_high (r11_reg, toload));
1744 emit_insn (gen_elf_low (r11_reg, r11_reg, toload));
1745 emit_insn (gen_cond_trap (LTU, stack_reg, r11_reg, const0_rtx));
1746 emit_move_insn (r11_reg, tmp_reg);
1748 else
1749 warning (0, "stack limit expression is not supported");
1752 if (flag_stack_clash_protection)
1754 if (size < get_stack_clash_protection_guard_size ())
1755 dump_stack_clash_frame_info (NO_PROBE_SMALL_FRAME, true);
1756 else
1758 rtx_insn *insn = rs6000_emit_probe_stack_range_stack_clash (size,
1759 copy_reg);
1761 /* If we asked for a copy with an offset, then we still need add in
1762 the offset. */
1763 if (copy_reg && copy_off)
1764 emit_insn (gen_add3_insn (copy_reg, copy_reg, GEN_INT (copy_off)));
1765 return insn;
1769 if (copy_reg)
1771 if (copy_off != 0)
1772 emit_insn (gen_add3_insn (copy_reg, stack_reg, GEN_INT (copy_off)));
1773 else
1774 emit_move_insn (copy_reg, stack_reg);
1777 /* Since we didn't use gen_frame_mem to generate the MEM, grab
1778 it now and set the alias set/attributes. The above gen_*_update
1779 calls will generate a PARALLEL with the MEM set being the first
1780 operation. */
1781 insn = rs6000_emit_allocate_stack_1 (size, stack_reg);
1782 return insn;
1785 #define PROBE_INTERVAL (1 << STACK_CHECK_PROBE_INTERVAL_EXP)
1787 #if PROBE_INTERVAL > 32768
1788 #error Cannot use indexed addressing mode for stack probing
1789 #endif
1791 /* Emit code to probe a range of stack addresses from FIRST to FIRST+SIZE,
1792 inclusive. These are offsets from the current stack pointer. */
1794 static void
1795 rs6000_emit_probe_stack_range (HOST_WIDE_INT first, HOST_WIDE_INT size)
1797 /* See if we have a constant small number of probes to generate. If so,
1798 that's the easy case. */
1799 if (first + size <= 32768)
1801 HOST_WIDE_INT i;
1803 /* Probe at FIRST + N * PROBE_INTERVAL for values of N from 1 until
1804 it exceeds SIZE. If only one probe is needed, this will not
1805 generate any code. Then probe at FIRST + SIZE. */
1806 for (i = PROBE_INTERVAL; i < size; i += PROBE_INTERVAL)
1807 emit_stack_probe (plus_constant (Pmode, stack_pointer_rtx,
1808 -(first + i)));
1810 emit_stack_probe (plus_constant (Pmode, stack_pointer_rtx,
1811 -(first + size)));
1814 /* Otherwise, do the same as above, but in a loop. Note that we must be
1815 extra careful with variables wrapping around because we might be at
1816 the very top (or the very bottom) of the address space and we have
1817 to be able to handle this case properly; in particular, we use an
1818 equality test for the loop condition. */
1819 else
1821 HOST_WIDE_INT rounded_size;
1822 rtx r12 = gen_rtx_REG (Pmode, 12);
1823 rtx r0 = gen_rtx_REG (Pmode, 0);
1825 /* Sanity check for the addressing mode we're going to use. */
1826 gcc_assert (first <= 32768);
1828 /* Step 1: round SIZE to the previous multiple of the interval. */
1830 rounded_size = ROUND_DOWN (size, PROBE_INTERVAL);
1833 /* Step 2: compute initial and final value of the loop counter. */
1835 /* TEST_ADDR = SP + FIRST. */
1836 emit_insn (gen_rtx_SET (r12, plus_constant (Pmode, stack_pointer_rtx,
1837 -first)));
1839 /* LAST_ADDR = SP + FIRST + ROUNDED_SIZE. */
1840 if (rounded_size > 32768)
1842 emit_move_insn (r0, GEN_INT (-rounded_size));
1843 emit_insn (gen_rtx_SET (r0, gen_rtx_PLUS (Pmode, r12, r0)));
1845 else
1846 emit_insn (gen_rtx_SET (r0, plus_constant (Pmode, r12,
1847 -rounded_size)));
1850 /* Step 3: the loop
1854 TEST_ADDR = TEST_ADDR + PROBE_INTERVAL
1855 probe at TEST_ADDR
1857 while (TEST_ADDR != LAST_ADDR)
1859 probes at FIRST + N * PROBE_INTERVAL for values of N from 1
1860 until it is equal to ROUNDED_SIZE. */
1862 if (TARGET_64BIT)
1863 emit_insn (gen_probe_stack_rangedi (r12, r12, stack_pointer_rtx, r0));
1864 else
1865 emit_insn (gen_probe_stack_rangesi (r12, r12, stack_pointer_rtx, r0));
1868 /* Step 4: probe at FIRST + SIZE if we cannot assert at compile-time
1869 that SIZE is equal to ROUNDED_SIZE. */
1871 if (size != rounded_size)
1872 emit_stack_probe (plus_constant (Pmode, r12, rounded_size - size));
1876 /* Probe a range of stack addresses from REG1 to REG2 inclusive. These are
1877 addresses, not offsets. */
1879 static const char *
1880 output_probe_stack_range_1 (rtx reg1, rtx reg2)
1882 static int labelno = 0;
1883 char loop_lab[32];
1884 rtx xops[2];
1886 ASM_GENERATE_INTERNAL_LABEL (loop_lab, "LPSRL", labelno++);
1888 /* Loop. */
1889 ASM_OUTPUT_INTERNAL_LABEL (asm_out_file, loop_lab);
1891 /* TEST_ADDR = TEST_ADDR + PROBE_INTERVAL. */
1892 xops[0] = reg1;
1893 xops[1] = GEN_INT (-PROBE_INTERVAL);
1894 output_asm_insn ("addi %0,%0,%1", xops);
1896 /* Probe at TEST_ADDR. */
1897 xops[1] = gen_rtx_REG (Pmode, 0);
1898 output_asm_insn ("stw %1,0(%0)", xops);
1900 /* Test if TEST_ADDR == LAST_ADDR. */
1901 xops[1] = reg2;
1902 if (TARGET_64BIT)
1903 output_asm_insn ("cmpd 0,%0,%1", xops);
1904 else
1905 output_asm_insn ("cmpw 0,%0,%1", xops);
1907 /* Branch. */
1908 fputs ("\tbne 0,", asm_out_file);
1909 assemble_name_raw (asm_out_file, loop_lab);
1910 fputc ('\n', asm_out_file);
1912 return "";
1915 /* This function is called when rs6000_frame_related is processing
1916 SETs within a PARALLEL, and returns whether the REGNO save ought to
1917 be marked RTX_FRAME_RELATED_P. The PARALLELs involved are those
1918 for out-of-line register save functions, store multiple, and the
1919 Darwin world_save. They may contain registers that don't really
1920 need saving. */
1922 static bool
1923 interesting_frame_related_regno (unsigned int regno)
1925 /* Saves apparently of r0 are actually saving LR. It doesn't make
1926 sense to substitute the regno here to test save_reg_p (LR_REGNO).
1927 We *know* LR needs saving, and dwarf2cfi.cc is able to deduce that
1928 (set (mem) (r0)) is saving LR from a prior (set (r0) (lr)) marked
1929 as frame related. */
1930 if (regno == 0)
1931 return true;
1932 /* If we see CR2 then we are here on a Darwin world save. Saves of
1933 CR2 signify the whole CR is being saved. This is a long-standing
1934 ABI wart fixed by ELFv2. As for r0/lr there is no need to check
1935 that CR needs to be saved. */
1936 if (regno == CR2_REGNO)
1937 return true;
1938 /* Omit frame info for any user-defined global regs. If frame info
1939 is supplied for them, frame unwinding will restore a user reg.
1940 Also omit frame info for any reg we don't need to save, as that
1941 bloats frame info and can cause problems with shrink wrapping.
1942 Since global regs won't be seen as needing to be saved, both of
1943 these conditions are covered by save_reg_p. */
1944 return save_reg_p (regno);
1947 /* Probe a range of stack addresses from REG1 to REG3 inclusive. These are
1948 addresses, not offsets.
1950 REG2 contains the backchain that must be stored into *sp at each allocation.
1952 This is subtly different than the Ada probing above in that it tries hard
1953 to prevent attacks that jump the stack guard. Thus, it is never allowed
1954 to allocate more than PROBE_INTERVAL bytes of stack space without a
1955 suitable probe. */
1957 static const char *
1958 output_probe_stack_range_stack_clash (rtx reg1, rtx reg2, rtx reg3)
1960 static int labelno = 0;
1961 char loop_lab[32];
1962 rtx xops[3];
1964 HOST_WIDE_INT probe_interval = get_stack_clash_protection_probe_interval ();
1966 ASM_GENERATE_INTERNAL_LABEL (loop_lab, "LPSRL", labelno++);
1968 ASM_OUTPUT_INTERNAL_LABEL (asm_out_file, loop_lab);
1970 /* This allocates and probes. */
1971 xops[0] = reg1;
1972 xops[1] = reg2;
1973 xops[2] = GEN_INT (-probe_interval);
1974 if (TARGET_64BIT)
1975 output_asm_insn ("stdu %1,%2(%0)", xops);
1976 else
1977 output_asm_insn ("stwu %1,%2(%0)", xops);
1979 /* Jump to LOOP_LAB if TEST_ADDR != LAST_ADDR. */
1980 xops[0] = reg1;
1981 xops[1] = reg3;
1982 if (TARGET_64BIT)
1983 output_asm_insn ("cmpd 0,%0,%1", xops);
1984 else
1985 output_asm_insn ("cmpw 0,%0,%1", xops);
1987 fputs ("\tbne 0,", asm_out_file);
1988 assemble_name_raw (asm_out_file, loop_lab);
1989 fputc ('\n', asm_out_file);
1991 return "";
1994 /* Wrapper around the output_probe_stack_range routines. */
1995 const char *
1996 output_probe_stack_range (rtx reg1, rtx reg2, rtx reg3)
1998 if (flag_stack_clash_protection)
1999 return output_probe_stack_range_stack_clash (reg1, reg2, reg3);
2000 else
2001 return output_probe_stack_range_1 (reg1, reg3);
2004 /* Add to 'insn' a note which is PATTERN (INSN) but with REG replaced
2005 with (plus:P (reg 1) VAL), and with REG2 replaced with REPL2 if REG2
2006 is not NULL. It would be nice if dwarf2out_frame_debug_expr could
2007 deduce these equivalences by itself so it wasn't necessary to hold
2008 its hand so much. Don't be tempted to always supply d2_f_d_e with
2009 the actual cfa register, ie. r31 when we are using a hard frame
2010 pointer. That fails when saving regs off r1, and sched moves the
2011 r31 setup past the reg saves. */
2013 static rtx_insn *
2014 rs6000_frame_related (rtx_insn *insn, rtx reg, HOST_WIDE_INT val,
2015 rtx reg2, rtx repl2)
2017 rtx repl;
2019 if (REGNO (reg) == STACK_POINTER_REGNUM)
2021 gcc_checking_assert (val == 0);
2022 repl = NULL_RTX;
2024 else
2025 repl = gen_rtx_PLUS (Pmode, gen_rtx_REG (Pmode, STACK_POINTER_REGNUM),
2026 GEN_INT (val));
2028 rtx pat = PATTERN (insn);
2029 if (!repl && !reg2)
2031 /* No need for any replacement. Just set RTX_FRAME_RELATED_P. */
2032 if (GET_CODE (pat) == PARALLEL)
2033 for (int i = 0; i < XVECLEN (pat, 0); i++)
2034 if (GET_CODE (XVECEXP (pat, 0, i)) == SET)
2036 rtx set = XVECEXP (pat, 0, i);
2038 if (!REG_P (SET_SRC (set))
2039 || interesting_frame_related_regno (REGNO (SET_SRC (set))))
2040 RTX_FRAME_RELATED_P (set) = 1;
2042 RTX_FRAME_RELATED_P (insn) = 1;
2043 return insn;
2046 /* We expect that 'pat' is either a SET or a PARALLEL containing
2047 SETs (and possibly other stuff). In a PARALLEL, all the SETs
2048 are important so they all have to be marked RTX_FRAME_RELATED_P.
2049 Call simplify_replace_rtx on the SETs rather than the whole insn
2050 so as to leave the other stuff alone (for example USE of r12). */
2052 set_used_flags (pat);
2053 if (GET_CODE (pat) == SET)
2055 if (repl)
2056 pat = simplify_replace_rtx (pat, reg, repl);
2057 if (reg2)
2058 pat = simplify_replace_rtx (pat, reg2, repl2);
2060 else if (GET_CODE (pat) == PARALLEL)
2062 pat = shallow_copy_rtx (pat);
2063 XVEC (pat, 0) = shallow_copy_rtvec (XVEC (pat, 0));
2065 for (int i = 0; i < XVECLEN (pat, 0); i++)
2066 if (GET_CODE (XVECEXP (pat, 0, i)) == SET)
2068 rtx set = XVECEXP (pat, 0, i);
2070 if (repl)
2071 set = simplify_replace_rtx (set, reg, repl);
2072 if (reg2)
2073 set = simplify_replace_rtx (set, reg2, repl2);
2074 XVECEXP (pat, 0, i) = set;
2076 if (!REG_P (SET_SRC (set))
2077 || interesting_frame_related_regno (REGNO (SET_SRC (set))))
2078 RTX_FRAME_RELATED_P (set) = 1;
2081 else
2082 gcc_unreachable ();
2084 RTX_FRAME_RELATED_P (insn) = 1;
2085 add_reg_note (insn, REG_FRAME_RELATED_EXPR, copy_rtx_if_shared (pat));
2087 return insn;
2090 /* Returns an insn that has a vrsave set operation with the
2091 appropriate CLOBBERs. */
2093 static rtx
2094 generate_set_vrsave (rtx reg, rs6000_stack_t *info, int epiloguep)
2096 int nclobs, i;
2097 rtx insn, clobs[TOTAL_ALTIVEC_REGS + 1];
2098 rtx vrsave = gen_rtx_REG (SImode, VRSAVE_REGNO);
2100 clobs[0]
2101 = gen_rtx_SET (vrsave,
2102 gen_rtx_UNSPEC_VOLATILE (SImode,
2103 gen_rtvec (2, reg, vrsave),
2104 UNSPECV_SET_VRSAVE));
2106 nclobs = 1;
2108 /* We need to clobber the registers in the mask so the scheduler
2109 does not move sets to VRSAVE before sets of AltiVec registers.
2111 However, if the function receives nonlocal gotos, reload will set
2112 all call saved registers live. We will end up with:
2114 (set (reg 999) (mem))
2115 (parallel [ (set (reg vrsave) (unspec blah))
2116 (clobber (reg 999))])
2118 The clobber will cause the store into reg 999 to be dead, and
2119 flow will attempt to delete an epilogue insn. In this case, we
2120 need an unspec use/set of the register. */
2122 for (i = FIRST_ALTIVEC_REGNO; i <= LAST_ALTIVEC_REGNO; ++i)
2123 if (info->vrsave_mask & ALTIVEC_REG_BIT (i))
2125 if (!epiloguep || call_used_or_fixed_reg_p (i))
2126 clobs[nclobs++] = gen_hard_reg_clobber (V4SImode, i);
2127 else
2129 rtx reg = gen_rtx_REG (V4SImode, i);
2131 clobs[nclobs++]
2132 = gen_rtx_SET (reg,
2133 gen_rtx_UNSPEC (V4SImode,
2134 gen_rtvec (1, reg), 27));
2138 insn = gen_rtx_PARALLEL (VOIDmode, rtvec_alloc (nclobs));
2140 for (i = 0; i < nclobs; ++i)
2141 XVECEXP (insn, 0, i) = clobs[i];
2143 return insn;
2146 static rtx
2147 gen_frame_set (rtx reg, rtx frame_reg, int offset, bool store)
2149 rtx addr, mem;
2151 addr = gen_rtx_PLUS (Pmode, frame_reg, GEN_INT (offset));
2152 mem = gen_frame_mem (GET_MODE (reg), addr);
2153 return gen_rtx_SET (store ? mem : reg, store ? reg : mem);
2156 static rtx
2157 gen_frame_load (rtx reg, rtx frame_reg, int offset)
2159 return gen_frame_set (reg, frame_reg, offset, false);
2162 static rtx
2163 gen_frame_store (rtx reg, rtx frame_reg, int offset)
2165 return gen_frame_set (reg, frame_reg, offset, true);
2168 /* Save a register into the frame, and emit RTX_FRAME_RELATED_P notes.
2169 Save REGNO into [FRAME_REG + OFFSET] in mode MODE. */
2171 static rtx_insn *
2172 emit_frame_save (rtx frame_reg, machine_mode mode,
2173 unsigned int regno, int offset, HOST_WIDE_INT frame_reg_to_sp)
2175 rtx reg;
2177 /* Some cases that need register indexed addressing. */
2178 gcc_checking_assert (!(TARGET_ALTIVEC_ABI && ALTIVEC_VECTOR_MODE (mode))
2179 || (TARGET_VSX && ALTIVEC_OR_VSX_VECTOR_MODE (mode)));
2181 reg = gen_rtx_REG (mode, regno);
2182 rtx_insn *insn = emit_insn (gen_frame_store (reg, frame_reg, offset));
2183 return rs6000_frame_related (insn, frame_reg, frame_reg_to_sp,
2184 NULL_RTX, NULL_RTX);
2187 /* Emit an offset memory reference suitable for a frame store, while
2188 converting to a valid addressing mode. */
2190 static rtx
2191 gen_frame_mem_offset (machine_mode mode, rtx reg, int offset)
2193 return gen_frame_mem (mode, gen_rtx_PLUS (Pmode, reg, GEN_INT (offset)));
2196 #ifndef TARGET_FIX_AND_CONTINUE
2197 #define TARGET_FIX_AND_CONTINUE 0
2198 #endif
2200 /* It's really GPR 13 or 14, FPR 14 and VR 20. We need the smallest. */
2201 #define FIRST_SAVRES_REGISTER FIRST_SAVED_GP_REGNO
2202 #define LAST_SAVRES_REGISTER 31
2203 #define N_SAVRES_REGISTERS (LAST_SAVRES_REGISTER - FIRST_SAVRES_REGISTER + 1)
2205 enum {
2206 SAVRES_LR = 0x1,
2207 SAVRES_SAVE = 0x2,
2208 SAVRES_REG = 0x0c,
2209 SAVRES_GPR = 0,
2210 SAVRES_FPR = 4,
2211 SAVRES_VR = 8
2214 static GTY(()) rtx savres_routine_syms[N_SAVRES_REGISTERS][12];
2216 /* Temporary holding space for an out-of-line register save/restore
2217 routine name. */
2218 static char savres_routine_name[30];
2220 /* Return the name for an out-of-line register save/restore routine.
2221 We are saving/restoring GPRs if GPR is true. */
2223 static char *
2224 rs6000_savres_routine_name (int regno, int sel)
2226 const char *prefix = "";
2227 const char *suffix = "";
2229 /* Different targets are supposed to define
2230 {SAVE,RESTORE}_FP_{PREFIX,SUFFIX} with the idea that the needed
2231 routine name could be defined with:
2233 sprintf (name, "%s%d%s", SAVE_FP_PREFIX, regno, SAVE_FP_SUFFIX)
2235 This is a nice idea in practice, but in reality, things are
2236 complicated in several ways:
2238 - ELF targets have save/restore routines for GPRs.
2240 - PPC64 ELF targets have routines for save/restore of GPRs that
2241 differ in what they do with the link register, so having a set
2242 prefix doesn't work. (We only use one of the save routines at
2243 the moment, though.)
2245 - PPC32 elf targets have "exit" versions of the restore routines
2246 that restore the link register and can save some extra space.
2247 These require an extra suffix. (There are also "tail" versions
2248 of the restore routines and "GOT" versions of the save routines,
2249 but we don't generate those at present. Same problems apply,
2250 though.)
2252 We deal with all this by synthesizing our own prefix/suffix and
2253 using that for the simple sprintf call shown above. */
2254 if (DEFAULT_ABI == ABI_V4)
2256 if (TARGET_64BIT)
2257 goto aix_names;
2259 if ((sel & SAVRES_REG) == SAVRES_GPR)
2260 prefix = (sel & SAVRES_SAVE) ? "_savegpr_" : "_restgpr_";
2261 else if ((sel & SAVRES_REG) == SAVRES_FPR)
2262 prefix = (sel & SAVRES_SAVE) ? "_savefpr_" : "_restfpr_";
2263 else if ((sel & SAVRES_REG) == SAVRES_VR)
2264 prefix = (sel & SAVRES_SAVE) ? "_savevr_" : "_restvr_";
2265 else
2266 abort ();
2268 if ((sel & SAVRES_LR))
2269 suffix = "_x";
2271 else if (DEFAULT_ABI == ABI_AIX || DEFAULT_ABI == ABI_ELFv2)
2273 #if !defined (POWERPC_LINUX) && !defined (POWERPC_FREEBSD)
2274 /* No out-of-line save/restore routines for GPRs on AIX. */
2275 gcc_assert (!TARGET_AIX || (sel & SAVRES_REG) != SAVRES_GPR);
2276 #endif
2278 aix_names:
2279 if ((sel & SAVRES_REG) == SAVRES_GPR)
2280 prefix = ((sel & SAVRES_SAVE)
2281 ? ((sel & SAVRES_LR) ? "_savegpr0_" : "_savegpr1_")
2282 : ((sel & SAVRES_LR) ? "_restgpr0_" : "_restgpr1_"));
2283 else if ((sel & SAVRES_REG) == SAVRES_FPR)
2285 #if defined (POWERPC_LINUX) || defined (POWERPC_FREEBSD)
2286 if ((sel & SAVRES_LR))
2287 prefix = ((sel & SAVRES_SAVE) ? "_savefpr_" : "_restfpr_");
2288 else
2289 #endif
2291 prefix = (sel & SAVRES_SAVE) ? SAVE_FP_PREFIX : RESTORE_FP_PREFIX;
2292 suffix = (sel & SAVRES_SAVE) ? SAVE_FP_SUFFIX : RESTORE_FP_SUFFIX;
2295 else if ((sel & SAVRES_REG) == SAVRES_VR)
2296 prefix = (sel & SAVRES_SAVE) ? "_savevr_" : "_restvr_";
2297 else
2298 abort ();
2301 if (DEFAULT_ABI == ABI_DARWIN)
2303 /* The Darwin approach is (slightly) different, in order to be
2304 compatible with code generated by the system toolchain. There is a
2305 single symbol for the start of save sequence, and the code here
2306 embeds an offset into that code on the basis of the first register
2307 to be saved. */
2308 prefix = (sel & SAVRES_SAVE) ? "save" : "rest" ;
2309 if ((sel & SAVRES_REG) == SAVRES_GPR)
2310 sprintf (savres_routine_name, "*%sGPR%s%s%.0d ; %s r%d-r31", prefix,
2311 ((sel & SAVRES_LR) ? "x" : ""), (regno == 13 ? "" : "+"),
2312 (regno - 13) * 4, prefix, regno);
2313 else if ((sel & SAVRES_REG) == SAVRES_FPR)
2314 sprintf (savres_routine_name, "*%sFP%s%.0d ; %s f%d-f31", prefix,
2315 (regno == 14 ? "" : "+"), (regno - 14) * 4, prefix, regno);
2316 else if ((sel & SAVRES_REG) == SAVRES_VR)
2317 sprintf (savres_routine_name, "*%sVEC%s%.0d ; %s v%d-v31", prefix,
2318 (regno == 20 ? "" : "+"), (regno - 20) * 8, prefix, regno);
2319 else
2320 abort ();
2322 else
2323 sprintf (savres_routine_name, "%s%d%s", prefix, regno, suffix);
2325 return savres_routine_name;
2328 /* Return an RTL SYMBOL_REF for an out-of-line register save/restore routine.
2329 We are saving/restoring GPRs if GPR is true. */
2331 static rtx
2332 rs6000_savres_routine_sym (rs6000_stack_t *info, int sel)
2334 int regno = ((sel & SAVRES_REG) == SAVRES_GPR
2335 ? info->first_gp_reg_save
2336 : (sel & SAVRES_REG) == SAVRES_FPR
2337 ? info->first_fp_reg_save - 32
2338 : (sel & SAVRES_REG) == SAVRES_VR
2339 ? info->first_altivec_reg_save - FIRST_ALTIVEC_REGNO
2340 : -1);
2341 rtx sym;
2342 int select = sel;
2344 /* Don't generate bogus routine names. */
2345 gcc_assert (FIRST_SAVRES_REGISTER <= regno
2346 && regno <= LAST_SAVRES_REGISTER
2347 && select >= 0 && select <= 12);
2349 sym = savres_routine_syms[regno-FIRST_SAVRES_REGISTER][select];
2351 if (sym == NULL)
2353 char *name;
2355 name = rs6000_savres_routine_name (regno, sel);
2357 sym = savres_routine_syms[regno-FIRST_SAVRES_REGISTER][select]
2358 = gen_rtx_SYMBOL_REF (Pmode, ggc_strdup (name));
2359 SYMBOL_REF_FLAGS (sym) |= SYMBOL_FLAG_FUNCTION;
2362 return sym;
2365 /* Emit a sequence of insns, including a stack tie if needed, for
2366 resetting the stack pointer. If UPDT_REGNO is not 1, then don't
2367 reset the stack pointer, but move the base of the frame into
2368 reg UPDT_REGNO for use by out-of-line register restore routines. */
2370 static rtx
2371 rs6000_emit_stack_reset (rtx frame_reg_rtx, HOST_WIDE_INT frame_off,
2372 unsigned updt_regno)
2374 /* If there is nothing to do, don't do anything. */
2375 if (frame_off == 0 && REGNO (frame_reg_rtx) == updt_regno)
2376 return NULL_RTX;
2378 rtx updt_reg_rtx = gen_rtx_REG (Pmode, updt_regno);
2380 /* This blockage is needed so that sched doesn't decide to move
2381 the sp change before the register restores. */
2382 if (DEFAULT_ABI == ABI_V4)
2383 return emit_insn (gen_stack_restore_tie (updt_reg_rtx, frame_reg_rtx,
2384 GEN_INT (frame_off)));
2386 /* If we are restoring registers out-of-line, we will be using the
2387 "exit" variants of the restore routines, which will reset the
2388 stack for us. But we do need to point updt_reg into the
2389 right place for those routines. */
2390 if (frame_off != 0)
2391 return emit_insn (gen_add3_insn (updt_reg_rtx,
2392 frame_reg_rtx, GEN_INT (frame_off)));
2393 else
2394 return emit_move_insn (updt_reg_rtx, frame_reg_rtx);
2396 return NULL_RTX;
2399 /* Return the register number used as a pointer by out-of-line
2400 save/restore functions. */
2402 static inline unsigned
2403 ptr_regno_for_savres (int sel)
2405 if (DEFAULT_ABI == ABI_AIX || DEFAULT_ABI == ABI_ELFv2)
2406 return (sel & SAVRES_REG) == SAVRES_FPR || (sel & SAVRES_LR) ? 1 : 12;
2407 return DEFAULT_ABI == ABI_DARWIN && (sel & SAVRES_REG) == SAVRES_FPR ? 1 : 11;
2410 /* Construct a parallel rtx describing the effect of a call to an
2411 out-of-line register save/restore routine, and emit the insn
2412 or jump_insn as appropriate. */
2414 static rtx_insn *
2415 rs6000_emit_savres_rtx (rs6000_stack_t *info,
2416 rtx frame_reg_rtx, int save_area_offset, int lr_offset,
2417 machine_mode reg_mode, int sel)
2419 int i;
2420 int offset, start_reg, end_reg, n_regs, use_reg;
2421 int reg_size = GET_MODE_SIZE (reg_mode);
2422 rtx sym;
2423 rtvec p;
2424 rtx par;
2425 rtx_insn *insn;
2427 offset = 0;
2428 start_reg = ((sel & SAVRES_REG) == SAVRES_GPR
2429 ? info->first_gp_reg_save
2430 : (sel & SAVRES_REG) == SAVRES_FPR
2431 ? info->first_fp_reg_save
2432 : (sel & SAVRES_REG) == SAVRES_VR
2433 ? info->first_altivec_reg_save
2434 : -1);
2435 end_reg = ((sel & SAVRES_REG) == SAVRES_GPR
2436 ? 32
2437 : (sel & SAVRES_REG) == SAVRES_FPR
2438 ? 64
2439 : (sel & SAVRES_REG) == SAVRES_VR
2440 ? LAST_ALTIVEC_REGNO + 1
2441 : -1);
2442 n_regs = end_reg - start_reg;
2443 p = rtvec_alloc (3 + ((sel & SAVRES_LR) ? 1 : 0)
2444 + ((sel & SAVRES_REG) == SAVRES_VR ? 1 : 0)
2445 + n_regs);
2447 if (!(sel & SAVRES_SAVE) && (sel & SAVRES_LR))
2448 RTVEC_ELT (p, offset++) = ret_rtx;
2450 RTVEC_ELT (p, offset++) = gen_hard_reg_clobber (Pmode, LR_REGNO);
2452 sym = rs6000_savres_routine_sym (info, sel);
2453 RTVEC_ELT (p, offset++) = gen_rtx_USE (VOIDmode, sym);
2455 use_reg = ptr_regno_for_savres (sel);
2456 if ((sel & SAVRES_REG) == SAVRES_VR)
2458 /* Vector regs are saved/restored using [reg+reg] addressing. */
2459 RTVEC_ELT (p, offset++) = gen_hard_reg_clobber (Pmode, use_reg);
2460 RTVEC_ELT (p, offset++)
2461 = gen_rtx_USE (VOIDmode, gen_rtx_REG (Pmode, 0));
2463 else
2464 RTVEC_ELT (p, offset++)
2465 = gen_rtx_USE (VOIDmode, gen_rtx_REG (Pmode, use_reg));
2467 for (i = 0; i < end_reg - start_reg; i++)
2468 RTVEC_ELT (p, i + offset)
2469 = gen_frame_set (gen_rtx_REG (reg_mode, start_reg + i),
2470 frame_reg_rtx, save_area_offset + reg_size * i,
2471 (sel & SAVRES_SAVE) != 0);
2473 if ((sel & SAVRES_SAVE) && (sel & SAVRES_LR))
2474 RTVEC_ELT (p, i + offset)
2475 = gen_frame_store (gen_rtx_REG (Pmode, 0), frame_reg_rtx, lr_offset);
2477 par = gen_rtx_PARALLEL (VOIDmode, p);
2479 if (!(sel & SAVRES_SAVE) && (sel & SAVRES_LR))
2481 insn = emit_jump_insn (par);
2482 JUMP_LABEL (insn) = ret_rtx;
2484 else
2485 insn = emit_insn (par);
2486 return insn;
2489 /* Emit prologue code to store CR fields that need to be saved into REG. This
2490 function should only be called when moving the non-volatile CRs to REG, it
2491 is not a general purpose routine to move the entire set of CRs to REG.
2492 Specifically, gen_prologue_movesi_from_cr() does not contain uses of the
2493 volatile CRs. */
2495 static void
2496 rs6000_emit_prologue_move_from_cr (rtx reg)
2498 /* Only the ELFv2 ABI allows storing only selected fields. */
2499 if (DEFAULT_ABI == ABI_ELFv2 && TARGET_MFCRF)
2501 int i, cr_reg[8], count = 0;
2503 /* Collect CR fields that must be saved. */
2504 for (i = 0; i < 8; i++)
2505 if (save_reg_p (CR0_REGNO + i))
2506 cr_reg[count++] = i;
2508 /* If it's just a single one, use mfcrf. */
2509 if (count == 1)
2511 rtvec p = rtvec_alloc (1);
2512 rtvec r = rtvec_alloc (2);
2513 RTVEC_ELT (r, 0) = gen_rtx_REG (CCmode, CR0_REGNO + cr_reg[0]);
2514 RTVEC_ELT (r, 1) = GEN_INT (1 << (7 - cr_reg[0]));
2515 RTVEC_ELT (p, 0)
2516 = gen_rtx_SET (reg,
2517 gen_rtx_UNSPEC (SImode, r, UNSPEC_MOVESI_FROM_CR));
2519 emit_insn (gen_rtx_PARALLEL (VOIDmode, p));
2520 return;
2523 /* ??? It might be better to handle count == 2 / 3 cases here
2524 as well, using logical operations to combine the values. */
2527 emit_insn (gen_prologue_movesi_from_cr (reg));
2530 /* Return whether the split-stack arg pointer (r12) is used. */
2532 static bool
2533 split_stack_arg_pointer_used_p (void)
2535 /* If the pseudo holding the arg pointer is no longer a pseudo,
2536 then the arg pointer is used. */
2537 if (cfun->machine->split_stack_arg_pointer != NULL_RTX
2538 && (!REG_P (cfun->machine->split_stack_arg_pointer)
2539 || HARD_REGISTER_P (cfun->machine->split_stack_arg_pointer)))
2540 return true;
2542 /* Unfortunately we also need to do some code scanning, since
2543 r12 may have been substituted for the pseudo. */
2544 rtx_insn *insn;
2545 basic_block bb = ENTRY_BLOCK_PTR_FOR_FN (cfun)->next_bb;
2546 FOR_BB_INSNS (bb, insn)
2547 if (NONDEBUG_INSN_P (insn))
2549 /* A call destroys r12. */
2550 if (CALL_P (insn))
2551 return false;
2553 df_ref use;
2554 FOR_EACH_INSN_USE (use, insn)
2556 rtx x = DF_REF_REG (use);
2557 if (REG_P (x) && REGNO (x) == 12)
2558 return true;
2560 df_ref def;
2561 FOR_EACH_INSN_DEF (def, insn)
2563 rtx x = DF_REF_REG (def);
2564 if (REG_P (x) && REGNO (x) == 12)
2565 return false;
2568 return bitmap_bit_p (DF_LR_OUT (bb), 12);
2571 /* Return whether we need to emit an ELFv2 global entry point prologue. */
2573 bool
2574 rs6000_global_entry_point_prologue_needed_p (void)
2576 /* Only needed for the ELFv2 ABI. */
2577 if (DEFAULT_ABI != ABI_ELFv2)
2578 return false;
2580 /* With -msingle-pic-base, we assume the whole program shares the same
2581 TOC, so no global entry point prologues are needed anywhere. */
2582 if (TARGET_SINGLE_PIC_BASE)
2583 return false;
2585 /* PC-relative functions never generate a global entry point prologue. */
2586 if (rs6000_pcrel_p ())
2587 return false;
2589 /* Ensure we have a global entry point for thunks. ??? We could
2590 avoid that if the target routine doesn't need a global entry point,
2591 but we do not know whether this is the case at this point. */
2592 if (cfun->is_thunk)
2593 return true;
2595 /* For regular functions, rs6000_emit_prologue sets this flag if the
2596 routine ever uses the TOC pointer. */
2597 return cfun->machine->r2_setup_needed;
2600 /* Implement TARGET_SHRINK_WRAP_GET_SEPARATE_COMPONENTS. */
2601 sbitmap
2602 rs6000_get_separate_components (void)
2604 rs6000_stack_t *info = rs6000_stack_info ();
2606 if (WORLD_SAVE_P (info))
2607 return NULL;
2609 gcc_assert (!(info->savres_strategy & SAVE_MULTIPLE)
2610 && !(info->savres_strategy & REST_MULTIPLE));
2612 /* Component 0 is the save/restore of LR (done via GPR0).
2613 Component 2 is the save of the TOC (GPR2).
2614 Components 13..31 are the save/restore of GPR13..GPR31.
2615 Components 46..63 are the save/restore of FPR14..FPR31. */
2617 cfun->machine->n_components = 64;
2619 sbitmap components = sbitmap_alloc (cfun->machine->n_components);
2620 bitmap_clear (components);
2622 int reg_size = TARGET_32BIT ? 4 : 8;
2623 int fp_reg_size = 8;
2625 /* The GPRs we need saved to the frame. */
2626 if ((info->savres_strategy & SAVE_INLINE_GPRS)
2627 && (info->savres_strategy & REST_INLINE_GPRS))
2629 int offset = info->gp_save_offset;
2630 if (info->push_p)
2631 offset += info->total_size;
2633 for (unsigned regno = info->first_gp_reg_save; regno < 32; regno++)
2635 if (IN_RANGE (offset, -0x8000, 0x7fff)
2636 && save_reg_p (regno))
2637 bitmap_set_bit (components, regno);
2639 offset += reg_size;
2643 /* Don't mess with the hard frame pointer. */
2644 if (frame_pointer_needed)
2645 bitmap_clear_bit (components, HARD_FRAME_POINTER_REGNUM);
2647 /* Don't mess with the fixed TOC register. */
2648 if ((TARGET_TOC && TARGET_MINIMAL_TOC)
2649 || (flag_pic == 1 && DEFAULT_ABI == ABI_V4)
2650 || (flag_pic && DEFAULT_ABI == ABI_DARWIN))
2651 bitmap_clear_bit (components, RS6000_PIC_OFFSET_TABLE_REGNUM);
2653 /* The FPRs we need saved to the frame. */
2654 if ((info->savres_strategy & SAVE_INLINE_FPRS)
2655 && (info->savres_strategy & REST_INLINE_FPRS))
2657 int offset = info->fp_save_offset;
2658 if (info->push_p)
2659 offset += info->total_size;
2661 for (unsigned regno = info->first_fp_reg_save; regno < 64; regno++)
2663 if (IN_RANGE (offset, -0x8000, 0x7fff) && save_reg_p (regno))
2664 bitmap_set_bit (components, regno);
2666 offset += fp_reg_size;
2670 /* Optimize LR save and restore if we can. This is component 0. Any
2671 out-of-line register save/restore routines need LR. */
2672 if (info->lr_save_p
2673 && !(flag_pic && (DEFAULT_ABI == ABI_V4 || DEFAULT_ABI == ABI_DARWIN))
2674 && (info->savres_strategy & SAVE_INLINE_GPRS)
2675 && (info->savres_strategy & REST_INLINE_GPRS)
2676 && (info->savres_strategy & SAVE_INLINE_FPRS)
2677 && (info->savres_strategy & REST_INLINE_FPRS)
2678 && (info->savres_strategy & SAVE_INLINE_VRS)
2679 && (info->savres_strategy & REST_INLINE_VRS))
2681 int offset = info->lr_save_offset;
2682 if (info->push_p)
2683 offset += info->total_size;
2684 if (IN_RANGE (offset, -0x8000, 0x7fff))
2685 bitmap_set_bit (components, 0);
2688 /* Optimize saving the TOC. This is component 2. */
2689 if (cfun->machine->save_toc_in_prologue)
2690 bitmap_set_bit (components, 2);
2692 return components;
2695 /* Implement TARGET_SHRINK_WRAP_COMPONENTS_FOR_BB. */
2696 sbitmap
2697 rs6000_components_for_bb (basic_block bb)
2699 rs6000_stack_t *info = rs6000_stack_info ();
2701 bitmap in = DF_LIVE_IN (bb);
2702 bitmap gen = &DF_LIVE_BB_INFO (bb)->gen;
2703 bitmap kill = &DF_LIVE_BB_INFO (bb)->kill;
2705 sbitmap components = sbitmap_alloc (cfun->machine->n_components);
2706 bitmap_clear (components);
2708 /* A register is used in a bb if it is in the IN, GEN, or KILL sets. */
2710 /* GPRs. */
2711 for (unsigned regno = info->first_gp_reg_save; regno < 32; regno++)
2712 if (bitmap_bit_p (in, regno)
2713 || bitmap_bit_p (gen, regno)
2714 || bitmap_bit_p (kill, regno))
2715 bitmap_set_bit (components, regno);
2717 /* FPRs. */
2718 for (unsigned regno = info->first_fp_reg_save; regno < 64; regno++)
2719 if (bitmap_bit_p (in, regno)
2720 || bitmap_bit_p (gen, regno)
2721 || bitmap_bit_p (kill, regno))
2722 bitmap_set_bit (components, regno);
2724 /* The link register. */
2725 if (bitmap_bit_p (in, LR_REGNO)
2726 || bitmap_bit_p (gen, LR_REGNO)
2727 || bitmap_bit_p (kill, LR_REGNO))
2728 bitmap_set_bit (components, 0);
2730 /* The TOC save. */
2731 if (bitmap_bit_p (in, TOC_REGNUM)
2732 || bitmap_bit_p (gen, TOC_REGNUM)
2733 || bitmap_bit_p (kill, TOC_REGNUM))
2734 bitmap_set_bit (components, 2);
2736 return components;
2739 /* Implement TARGET_SHRINK_WRAP_DISQUALIFY_COMPONENTS. */
2740 void
2741 rs6000_disqualify_components (sbitmap components, edge e,
2742 sbitmap edge_components, bool /*is_prologue*/)
2744 /* Our LR pro/epilogue code moves LR via R0, so R0 had better not be
2745 live where we want to place that code. */
2746 if (bitmap_bit_p (edge_components, 0)
2747 && bitmap_bit_p (DF_LIVE_IN (e->dest), 0))
2749 if (dump_file)
2750 fprintf (dump_file, "Disqualifying LR because GPR0 is live "
2751 "on entry to bb %d\n", e->dest->index);
2752 bitmap_clear_bit (components, 0);
2756 /* Implement TARGET_SHRINK_WRAP_EMIT_PROLOGUE_COMPONENTS. */
2757 void
2758 rs6000_emit_prologue_components (sbitmap components)
2760 rs6000_stack_t *info = rs6000_stack_info ();
2761 rtx ptr_reg = gen_rtx_REG (Pmode, frame_pointer_needed_indeed
2762 ? HARD_FRAME_POINTER_REGNUM
2763 : STACK_POINTER_REGNUM);
2765 machine_mode reg_mode = Pmode;
2766 int reg_size = TARGET_32BIT ? 4 : 8;
2767 machine_mode fp_reg_mode = TARGET_HARD_FLOAT ? DFmode : SFmode;
2768 int fp_reg_size = 8;
2770 /* Prologue for LR. */
2771 if (bitmap_bit_p (components, 0))
2773 rtx lr = gen_rtx_REG (reg_mode, LR_REGNO);
2774 rtx reg = gen_rtx_REG (reg_mode, 0);
2775 rtx_insn *insn = emit_move_insn (reg, lr);
2776 RTX_FRAME_RELATED_P (insn) = 1;
2777 add_reg_note (insn, REG_CFA_REGISTER, gen_rtx_SET (reg, lr));
2779 int offset = info->lr_save_offset;
2780 if (info->push_p)
2781 offset += info->total_size;
2783 insn = emit_insn (gen_frame_store (reg, ptr_reg, offset));
2784 RTX_FRAME_RELATED_P (insn) = 1;
2785 rtx mem = copy_rtx (SET_DEST (single_set (insn)));
2786 add_reg_note (insn, REG_CFA_OFFSET, gen_rtx_SET (mem, lr));
2789 /* Prologue for TOC. */
2790 if (bitmap_bit_p (components, 2))
2792 rtx reg = gen_rtx_REG (reg_mode, TOC_REGNUM);
2793 rtx sp_reg = gen_rtx_REG (Pmode, STACK_POINTER_REGNUM);
2794 emit_insn (gen_frame_store (reg, sp_reg, RS6000_TOC_SAVE_SLOT));
2797 /* Prologue for the GPRs. */
2798 int offset = info->gp_save_offset;
2799 if (info->push_p)
2800 offset += info->total_size;
2802 for (int i = info->first_gp_reg_save; i < 32; i++)
2804 if (bitmap_bit_p (components, i))
2806 rtx reg = gen_rtx_REG (reg_mode, i);
2807 rtx_insn *insn = emit_insn (gen_frame_store (reg, ptr_reg, offset));
2808 RTX_FRAME_RELATED_P (insn) = 1;
2809 rtx set = copy_rtx (single_set (insn));
2810 add_reg_note (insn, REG_CFA_OFFSET, set);
2813 offset += reg_size;
2816 /* Prologue for the FPRs. */
2817 offset = info->fp_save_offset;
2818 if (info->push_p)
2819 offset += info->total_size;
2821 for (int i = info->first_fp_reg_save; i < 64; i++)
2823 if (bitmap_bit_p (components, i))
2825 rtx reg = gen_rtx_REG (fp_reg_mode, i);
2826 rtx_insn *insn = emit_insn (gen_frame_store (reg, ptr_reg, offset));
2827 RTX_FRAME_RELATED_P (insn) = 1;
2828 rtx set = copy_rtx (single_set (insn));
2829 add_reg_note (insn, REG_CFA_OFFSET, set);
2832 offset += fp_reg_size;
2836 /* Implement TARGET_SHRINK_WRAP_EMIT_EPILOGUE_COMPONENTS. */
2837 void
2838 rs6000_emit_epilogue_components (sbitmap components)
2840 rs6000_stack_t *info = rs6000_stack_info ();
2841 rtx ptr_reg = gen_rtx_REG (Pmode, frame_pointer_needed_indeed
2842 ? HARD_FRAME_POINTER_REGNUM
2843 : STACK_POINTER_REGNUM);
2845 machine_mode reg_mode = Pmode;
2846 int reg_size = TARGET_32BIT ? 4 : 8;
2848 machine_mode fp_reg_mode = TARGET_HARD_FLOAT ? DFmode : SFmode;
2849 int fp_reg_size = 8;
2851 /* Epilogue for the FPRs. */
2852 int offset = info->fp_save_offset;
2853 if (info->push_p)
2854 offset += info->total_size;
2856 for (int i = info->first_fp_reg_save; i < 64; i++)
2858 if (bitmap_bit_p (components, i))
2860 rtx reg = gen_rtx_REG (fp_reg_mode, i);
2861 rtx_insn *insn = emit_insn (gen_frame_load (reg, ptr_reg, offset));
2862 RTX_FRAME_RELATED_P (insn) = 1;
2863 add_reg_note (insn, REG_CFA_RESTORE, reg);
2866 offset += fp_reg_size;
2869 /* Epilogue for the GPRs. */
2870 offset = info->gp_save_offset;
2871 if (info->push_p)
2872 offset += info->total_size;
2874 for (int i = info->first_gp_reg_save; i < 32; i++)
2876 if (bitmap_bit_p (components, i))
2878 rtx reg = gen_rtx_REG (reg_mode, i);
2879 rtx_insn *insn = emit_insn (gen_frame_load (reg, ptr_reg, offset));
2880 RTX_FRAME_RELATED_P (insn) = 1;
2881 add_reg_note (insn, REG_CFA_RESTORE, reg);
2884 offset += reg_size;
2887 /* Epilogue for LR. */
2888 if (bitmap_bit_p (components, 0))
2890 int offset = info->lr_save_offset;
2891 if (info->push_p)
2892 offset += info->total_size;
2894 rtx reg = gen_rtx_REG (reg_mode, 0);
2895 rtx_insn *insn = emit_insn (gen_frame_load (reg, ptr_reg, offset));
2897 rtx lr = gen_rtx_REG (Pmode, LR_REGNO);
2898 insn = emit_move_insn (lr, reg);
2899 RTX_FRAME_RELATED_P (insn) = 1;
2900 add_reg_note (insn, REG_CFA_RESTORE, lr);
2904 /* Implement TARGET_SHRINK_WRAP_SET_HANDLED_COMPONENTS. */
2905 void
2906 rs6000_set_handled_components (sbitmap components)
2908 rs6000_stack_t *info = rs6000_stack_info ();
2910 for (int i = info->first_gp_reg_save; i < 32; i++)
2911 if (bitmap_bit_p (components, i))
2912 cfun->machine->gpr_is_wrapped_separately[i] = true;
2914 for (int i = info->first_fp_reg_save; i < 64; i++)
2915 if (bitmap_bit_p (components, i))
2916 cfun->machine->fpr_is_wrapped_separately[i - 32] = true;
2918 if (bitmap_bit_p (components, 0))
2919 cfun->machine->lr_is_wrapped_separately = true;
2921 if (bitmap_bit_p (components, 2))
2922 cfun->machine->toc_is_wrapped_separately = true;
2925 /* VRSAVE is a bit vector representing which AltiVec registers
2926 are used. The OS uses this to determine which vector
2927 registers to save on a context switch. We need to save
2928 VRSAVE on the stack frame, add whatever AltiVec registers we
2929 used in this function, and do the corresponding magic in the
2930 epilogue. */
2931 static void
2932 emit_vrsave_prologue (rs6000_stack_t *info, int save_regno,
2933 HOST_WIDE_INT frame_off, rtx frame_reg_rtx)
2935 /* Get VRSAVE into a GPR. */
2936 rtx reg = gen_rtx_REG (SImode, save_regno);
2937 rtx vrsave = gen_rtx_REG (SImode, VRSAVE_REGNO);
2938 if (TARGET_MACHO)
2939 emit_insn (gen_get_vrsave_internal (reg));
2940 else
2941 emit_insn (gen_rtx_SET (reg, vrsave));
2943 /* Save VRSAVE. */
2944 int offset = info->vrsave_save_offset + frame_off;
2945 emit_insn (gen_frame_store (reg, frame_reg_rtx, offset));
2947 /* Include the registers in the mask. */
2948 emit_insn (gen_iorsi3 (reg, reg, GEN_INT (info->vrsave_mask)));
2950 emit_insn (generate_set_vrsave (reg, info, 0));
2953 /* Set up the arg pointer (r12) for -fsplit-stack code. If __morestack was
2954 called, it left the arg pointer to the old stack in r29. Otherwise, the
2955 arg pointer is the top of the current frame. */
2956 static void
2957 emit_split_stack_prologue (rs6000_stack_t *info, rtx_insn *sp_adjust,
2958 HOST_WIDE_INT frame_off, rtx frame_reg_rtx)
2960 cfun->machine->split_stack_argp_used = true;
2962 if (sp_adjust)
2964 rtx r12 = gen_rtx_REG (Pmode, 12);
2965 rtx sp_reg_rtx = gen_rtx_REG (Pmode, STACK_POINTER_REGNUM);
2966 rtx set_r12 = gen_rtx_SET (r12, sp_reg_rtx);
2967 emit_insn_before (set_r12, sp_adjust);
2969 else if (frame_off != 0 || REGNO (frame_reg_rtx) != 12)
2971 rtx r12 = gen_rtx_REG (Pmode, 12);
2972 if (frame_off == 0)
2973 emit_move_insn (r12, frame_reg_rtx);
2974 else
2975 emit_insn (gen_add3_insn (r12, frame_reg_rtx, GEN_INT (frame_off)));
2978 if (info->push_p)
2980 rtx r12 = gen_rtx_REG (Pmode, 12);
2981 rtx r29 = gen_rtx_REG (Pmode, 29);
2982 rtx cr7 = gen_rtx_REG (CCUNSmode, CR7_REGNO);
2983 rtx not_more = gen_label_rtx ();
2984 rtx jump;
2986 jump = gen_rtx_IF_THEN_ELSE (VOIDmode,
2987 gen_rtx_GEU (VOIDmode, cr7, const0_rtx),
2988 gen_rtx_LABEL_REF (VOIDmode, not_more),
2989 pc_rtx);
2990 jump = emit_jump_insn (gen_rtx_SET (pc_rtx, jump));
2991 JUMP_LABEL (jump) = not_more;
2992 LABEL_NUSES (not_more) += 1;
2993 emit_move_insn (r12, r29);
2994 emit_label (not_more);
2998 /* Emit function prologue as insns. */
3000 void
3001 rs6000_emit_prologue (void)
3003 rs6000_stack_t *info = rs6000_stack_info ();
3004 machine_mode reg_mode = Pmode;
3005 int reg_size = TARGET_32BIT ? 4 : 8;
3006 machine_mode fp_reg_mode = TARGET_HARD_FLOAT ? DFmode : SFmode;
3007 int fp_reg_size = 8;
3008 rtx sp_reg_rtx = gen_rtx_REG (Pmode, STACK_POINTER_REGNUM);
3009 rtx frame_reg_rtx = sp_reg_rtx;
3010 unsigned int cr_save_regno;
3011 rtx cr_save_rtx = NULL_RTX;
3012 rtx_insn *insn;
3013 int strategy;
3014 int using_static_chain_p
3015 = (cfun->static_chain_decl != NULL_TREE
3016 && df_regs_ever_live_p (STATIC_CHAIN_REGNUM)
3017 && call_used_or_fixed_reg_p (STATIC_CHAIN_REGNUM));
3018 int using_split_stack = (flag_split_stack
3019 && (lookup_attribute ("no_split_stack",
3020 DECL_ATTRIBUTES (cfun->decl))
3021 == NULL));
3023 frame_pointer_needed_indeed
3024 = frame_pointer_needed && df_regs_ever_live_p (HARD_FRAME_POINTER_REGNUM);
3026 /* Offset to top of frame for frame_reg and sp respectively. */
3027 HOST_WIDE_INT frame_off = 0;
3028 HOST_WIDE_INT sp_off = 0;
3029 /* sp_adjust is the stack adjusting instruction, tracked so that the
3030 insn setting up the split-stack arg pointer can be emitted just
3031 prior to it, when r12 is not used here for other purposes. */
3032 rtx_insn *sp_adjust = 0;
3034 #if CHECKING_P
3035 /* Track and check usage of r0, r11, r12. */
3036 int reg_inuse = using_static_chain_p ? 1 << 11 : 0;
3037 #define START_USE(R) do \
3039 gcc_assert ((reg_inuse & (1 << (R))) == 0); \
3040 reg_inuse |= 1 << (R); \
3041 } while (0)
3042 #define END_USE(R) do \
3044 gcc_assert ((reg_inuse & (1 << (R))) != 0); \
3045 reg_inuse &= ~(1 << (R)); \
3046 } while (0)
3047 #define NOT_INUSE(R) do \
3049 gcc_assert ((reg_inuse & (1 << (R))) == 0); \
3050 } while (0)
3051 #else
3052 #define START_USE(R) do {} while (0)
3053 #define END_USE(R) do {} while (0)
3054 #define NOT_INUSE(R) do {} while (0)
3055 #endif
3057 if (DEFAULT_ABI == ABI_ELFv2
3058 && !TARGET_SINGLE_PIC_BASE)
3060 cfun->machine->r2_setup_needed = df_regs_ever_live_p (TOC_REGNUM);
3062 /* With -mminimal-toc we may generate an extra use of r2 below. */
3063 if (TARGET_TOC && TARGET_MINIMAL_TOC
3064 && !constant_pool_empty_p ())
3065 cfun->machine->r2_setup_needed = true;
3069 if (flag_stack_usage_info)
3070 current_function_static_stack_size = info->total_size;
3072 if (flag_stack_check == STATIC_BUILTIN_STACK_CHECK)
3074 HOST_WIDE_INT size = info->total_size;
3076 if (crtl->is_leaf && !cfun->calls_alloca)
3078 if (size > PROBE_INTERVAL && size > get_stack_check_protect ())
3079 rs6000_emit_probe_stack_range (get_stack_check_protect (),
3080 size - get_stack_check_protect ());
3082 else if (size > 0)
3083 rs6000_emit_probe_stack_range (get_stack_check_protect (), size);
3086 if (TARGET_FIX_AND_CONTINUE)
3088 /* gdb on darwin arranges to forward a function from the old
3089 address by modifying the first 5 instructions of the function
3090 to branch to the overriding function. This is necessary to
3091 permit function pointers that point to the old function to
3092 actually forward to the new function. */
3093 emit_insn (gen_nop ());
3094 emit_insn (gen_nop ());
3095 emit_insn (gen_nop ());
3096 emit_insn (gen_nop ());
3097 emit_insn (gen_nop ());
3100 /* Handle world saves specially here. */
3101 if (WORLD_SAVE_P (info))
3103 int i, j, sz;
3104 rtx treg;
3105 rtvec p;
3106 rtx reg0;
3108 /* save_world expects lr in r0. */
3109 reg0 = gen_rtx_REG (Pmode, 0);
3110 if (info->lr_save_p)
3112 insn = emit_move_insn (reg0,
3113 gen_rtx_REG (Pmode, LR_REGNO));
3114 RTX_FRAME_RELATED_P (insn) = 1;
3117 /* The SAVE_WORLD and RESTORE_WORLD routines make a number of
3118 assumptions about the offsets of various bits of the stack
3119 frame. */
3120 gcc_assert (info->gp_save_offset == -220
3121 && info->fp_save_offset == -144
3122 && info->lr_save_offset == 8
3123 && info->cr_save_offset == 4
3124 && info->push_p
3125 && info->lr_save_p
3126 && (!crtl->calls_eh_return
3127 || info->ehrd_offset == -432)
3128 && info->vrsave_save_offset == -224
3129 && info->altivec_save_offset == -416);
3131 treg = gen_rtx_REG (SImode, 11);
3132 emit_move_insn (treg, GEN_INT (-info->total_size));
3134 /* SAVE_WORLD takes the caller's LR in R0 and the frame size
3135 in R11. It also clobbers R12, so beware! */
3137 /* Preserve CR2 for save_world prologues */
3138 sz = 5;
3139 sz += 32 - info->first_gp_reg_save;
3140 sz += 64 - info->first_fp_reg_save;
3141 sz += LAST_ALTIVEC_REGNO - info->first_altivec_reg_save + 1;
3142 p = rtvec_alloc (sz);
3143 j = 0;
3144 RTVEC_ELT (p, j++) = gen_hard_reg_clobber (SImode, LR_REGNO);
3145 RTVEC_ELT (p, j++) = gen_rtx_USE (VOIDmode,
3146 gen_rtx_SYMBOL_REF (Pmode,
3147 "*save_world"));
3148 /* We do floats first so that the instruction pattern matches
3149 properly. */
3150 for (i = 0; i < 64 - info->first_fp_reg_save; i++)
3151 RTVEC_ELT (p, j++)
3152 = gen_frame_store (gen_rtx_REG (TARGET_HARD_FLOAT ? DFmode : SFmode,
3153 info->first_fp_reg_save + i),
3154 frame_reg_rtx,
3155 info->fp_save_offset + frame_off + 8 * i);
3156 for (i = 0; info->first_altivec_reg_save + i <= LAST_ALTIVEC_REGNO; i++)
3157 RTVEC_ELT (p, j++)
3158 = gen_frame_store (gen_rtx_REG (V4SImode,
3159 info->first_altivec_reg_save + i),
3160 frame_reg_rtx,
3161 info->altivec_save_offset + frame_off + 16 * i);
3162 for (i = 0; i < 32 - info->first_gp_reg_save; i++)
3163 RTVEC_ELT (p, j++)
3164 = gen_frame_store (gen_rtx_REG (reg_mode, info->first_gp_reg_save + i),
3165 frame_reg_rtx,
3166 info->gp_save_offset + frame_off + reg_size * i);
3168 /* CR register traditionally saved as CR2. */
3169 RTVEC_ELT (p, j++)
3170 = gen_frame_store (gen_rtx_REG (SImode, CR2_REGNO),
3171 frame_reg_rtx, info->cr_save_offset + frame_off);
3172 /* Explain about use of R0. */
3173 if (info->lr_save_p)
3174 RTVEC_ELT (p, j++)
3175 = gen_frame_store (reg0,
3176 frame_reg_rtx, info->lr_save_offset + frame_off);
3177 /* Explain what happens to the stack pointer. */
3179 rtx newval = gen_rtx_PLUS (Pmode, sp_reg_rtx, treg);
3180 RTVEC_ELT (p, j++) = gen_rtx_SET (sp_reg_rtx, newval);
3183 insn = emit_insn (gen_rtx_PARALLEL (VOIDmode, p));
3184 rs6000_frame_related (insn, frame_reg_rtx, sp_off - frame_off,
3185 treg, GEN_INT (-info->total_size));
3186 sp_off = frame_off = info->total_size;
3189 strategy = info->savres_strategy;
3191 /* For V.4, update stack before we do any saving and set back pointer. */
3192 if (! WORLD_SAVE_P (info)
3193 && info->push_p
3194 && (DEFAULT_ABI == ABI_V4
3195 || crtl->calls_eh_return))
3197 bool need_r11 = (!(strategy & SAVE_INLINE_FPRS)
3198 || !(strategy & SAVE_INLINE_GPRS)
3199 || !(strategy & SAVE_INLINE_VRS));
3200 int ptr_regno = -1;
3201 rtx ptr_reg = NULL_RTX;
3202 int ptr_off = 0;
3204 if (info->total_size < 32767)
3205 frame_off = info->total_size;
3206 else if (need_r11)
3207 ptr_regno = 11;
3208 else if (info->cr_save_p
3209 || info->lr_save_p
3210 || info->first_fp_reg_save < 64
3211 || info->first_gp_reg_save < 32
3212 || info->altivec_size != 0
3213 || info->vrsave_size != 0
3214 || crtl->calls_eh_return)
3215 ptr_regno = 12;
3216 else
3218 /* The prologue won't be saving any regs so there is no need
3219 to set up a frame register to access any frame save area.
3220 We also won't be using frame_off anywhere below, but set
3221 the correct value anyway to protect against future
3222 changes to this function. */
3223 frame_off = info->total_size;
3225 if (ptr_regno != -1)
3227 /* Set up the frame offset to that needed by the first
3228 out-of-line save function. */
3229 START_USE (ptr_regno);
3230 ptr_reg = gen_rtx_REG (Pmode, ptr_regno);
3231 frame_reg_rtx = ptr_reg;
3232 if (!(strategy & SAVE_INLINE_FPRS) && info->fp_size != 0)
3233 gcc_checking_assert (info->fp_save_offset + info->fp_size == 0);
3234 else if (!(strategy & SAVE_INLINE_GPRS) && info->first_gp_reg_save < 32)
3235 ptr_off = info->gp_save_offset + info->gp_size;
3236 else if (!(strategy & SAVE_INLINE_VRS) && info->altivec_size != 0)
3237 ptr_off = info->altivec_save_offset + info->altivec_size;
3238 frame_off = -ptr_off;
3240 sp_adjust = rs6000_emit_allocate_stack (info->total_size,
3241 ptr_reg, ptr_off);
3242 if (REGNO (frame_reg_rtx) == 12)
3243 sp_adjust = 0;
3244 sp_off = info->total_size;
3245 if (frame_reg_rtx != sp_reg_rtx)
3246 rs6000_emit_stack_tie (frame_reg_rtx, false);
3249 /* If we use the link register, get it into r0. */
3250 if (!WORLD_SAVE_P (info) && info->lr_save_p
3251 && !cfun->machine->lr_is_wrapped_separately)
3253 rtx reg;
3255 reg = gen_rtx_REG (Pmode, 0);
3256 START_USE (0);
3257 insn = emit_move_insn (reg, gen_rtx_REG (Pmode, LR_REGNO));
3258 RTX_FRAME_RELATED_P (insn) = 1;
3260 if (!(strategy & (SAVE_NOINLINE_GPRS_SAVES_LR
3261 | SAVE_NOINLINE_FPRS_SAVES_LR)))
3263 insn = emit_insn (gen_frame_store (reg, frame_reg_rtx,
3264 info->lr_save_offset + frame_off));
3265 rs6000_frame_related (insn, frame_reg_rtx, sp_off - frame_off,
3266 NULL_RTX, NULL_RTX);
3267 END_USE (0);
3271 /* The ROP hash store must occur before a stack frame is created,
3272 since the hash operates on r1. */
3273 /* NOTE: The hashst isn't needed if we're going to do a sibcall,
3274 but there's no way to know that here. Harmless except for
3275 performance, of course. */
3276 if (TARGET_POWER10 && rs6000_rop_protect && info->rop_hash_size != 0)
3278 gcc_assert (DEFAULT_ABI == ABI_ELFv2);
3279 rtx stack_ptr = gen_rtx_REG (Pmode, STACK_POINTER_REGNUM);
3280 rtx addr = gen_rtx_PLUS (Pmode, stack_ptr,
3281 GEN_INT (info->rop_hash_save_offset));
3282 rtx mem = gen_rtx_MEM (Pmode, addr);
3283 rtx reg0 = gen_rtx_REG (Pmode, 0);
3284 emit_insn (gen_hashst (mem, reg0));
3287 /* If we need to save CR, put it into r12 or r11. Choose r12 except when
3288 r12 will be needed by out-of-line gpr save. */
3289 if (DEFAULT_ABI == ABI_AIX
3290 && !(strategy & (SAVE_INLINE_GPRS | SAVE_NOINLINE_GPRS_SAVES_LR)))
3291 cr_save_regno = 11;
3292 else if (DEFAULT_ABI == ABI_ELFv2)
3293 cr_save_regno = 11;
3294 else
3295 cr_save_regno = 12;
3296 if (!WORLD_SAVE_P (info)
3297 && info->cr_save_p
3298 && REGNO (frame_reg_rtx) != cr_save_regno
3299 && !(using_static_chain_p && cr_save_regno == 11)
3300 && !(using_split_stack && cr_save_regno == 12 && sp_adjust))
3302 cr_save_rtx = gen_rtx_REG (SImode, cr_save_regno);
3303 START_USE (cr_save_regno);
3304 rs6000_emit_prologue_move_from_cr (cr_save_rtx);
3307 /* Do any required saving of fpr's. If only one or two to save, do
3308 it ourselves. Otherwise, call function. */
3309 if (!WORLD_SAVE_P (info) && (strategy & SAVE_INLINE_FPRS))
3311 int offset = info->fp_save_offset + frame_off;
3312 for (int i = info->first_fp_reg_save; i < 64; i++)
3314 if (save_reg_p (i)
3315 && !cfun->machine->fpr_is_wrapped_separately[i - 32])
3316 emit_frame_save (frame_reg_rtx, fp_reg_mode, i, offset,
3317 sp_off - frame_off);
3319 offset += fp_reg_size;
3322 else if (!WORLD_SAVE_P (info) && info->first_fp_reg_save != 64)
3324 bool lr = (strategy & SAVE_NOINLINE_FPRS_SAVES_LR) != 0;
3325 int sel = SAVRES_SAVE | SAVRES_FPR | (lr ? SAVRES_LR : 0);
3326 unsigned ptr_regno = ptr_regno_for_savres (sel);
3327 rtx ptr_reg = frame_reg_rtx;
3329 if (REGNO (frame_reg_rtx) == ptr_regno)
3330 gcc_checking_assert (frame_off == 0);
3331 else
3333 ptr_reg = gen_rtx_REG (Pmode, ptr_regno);
3334 NOT_INUSE (ptr_regno);
3335 emit_insn (gen_add3_insn (ptr_reg,
3336 frame_reg_rtx, GEN_INT (frame_off)));
3338 insn = rs6000_emit_savres_rtx (info, ptr_reg,
3339 info->fp_save_offset,
3340 info->lr_save_offset,
3341 DFmode, sel);
3342 rs6000_frame_related (insn, ptr_reg, sp_off,
3343 NULL_RTX, NULL_RTX);
3344 if (lr)
3345 END_USE (0);
3348 /* Save GPRs. This is done as a PARALLEL if we are using
3349 the store-multiple instructions. */
3350 if (!WORLD_SAVE_P (info) && !(strategy & SAVE_INLINE_GPRS))
3352 bool lr = (strategy & SAVE_NOINLINE_GPRS_SAVES_LR) != 0;
3353 int sel = SAVRES_SAVE | SAVRES_GPR | (lr ? SAVRES_LR : 0);
3354 unsigned ptr_regno = ptr_regno_for_savres (sel);
3355 rtx ptr_reg = frame_reg_rtx;
3356 bool ptr_set_up = REGNO (ptr_reg) == ptr_regno;
3357 int end_save = info->gp_save_offset + info->gp_size;
3358 int ptr_off;
3360 if (ptr_regno == 12)
3361 sp_adjust = 0;
3362 if (!ptr_set_up)
3363 ptr_reg = gen_rtx_REG (Pmode, ptr_regno);
3365 /* Need to adjust r11 (r12) if we saved any FPRs. */
3366 if (end_save + frame_off != 0)
3368 rtx offset = GEN_INT (end_save + frame_off);
3370 if (ptr_set_up)
3371 frame_off = -end_save;
3372 else
3373 NOT_INUSE (ptr_regno);
3374 emit_insn (gen_add3_insn (ptr_reg, frame_reg_rtx, offset));
3376 else if (!ptr_set_up)
3378 NOT_INUSE (ptr_regno);
3379 emit_move_insn (ptr_reg, frame_reg_rtx);
3381 ptr_off = -end_save;
3382 insn = rs6000_emit_savres_rtx (info, ptr_reg,
3383 info->gp_save_offset + ptr_off,
3384 info->lr_save_offset + ptr_off,
3385 reg_mode, sel);
3386 rs6000_frame_related (insn, ptr_reg, sp_off - ptr_off,
3387 NULL_RTX, NULL_RTX);
3388 if (lr)
3389 END_USE (0);
3391 else if (!WORLD_SAVE_P (info) && (strategy & SAVE_MULTIPLE))
3393 rtvec p;
3394 int i;
3395 p = rtvec_alloc (32 - info->first_gp_reg_save);
3396 for (i = 0; i < 32 - info->first_gp_reg_save; i++)
3397 RTVEC_ELT (p, i)
3398 = gen_frame_store (gen_rtx_REG (reg_mode, info->first_gp_reg_save + i),
3399 frame_reg_rtx,
3400 info->gp_save_offset + frame_off + reg_size * i);
3401 insn = emit_insn (gen_rtx_PARALLEL (VOIDmode, p));
3402 rs6000_frame_related (insn, frame_reg_rtx, sp_off - frame_off,
3403 NULL_RTX, NULL_RTX);
3405 else if (!WORLD_SAVE_P (info))
3407 int offset = info->gp_save_offset + frame_off;
3408 for (int i = info->first_gp_reg_save; i < 32; i++)
3410 if (save_reg_p (i)
3411 && !cfun->machine->gpr_is_wrapped_separately[i])
3412 emit_frame_save (frame_reg_rtx, reg_mode, i, offset,
3413 sp_off - frame_off);
3415 offset += reg_size;
3419 if (crtl->calls_eh_return)
3421 unsigned int i;
3422 rtvec p;
3424 for (i = 0; ; ++i)
3426 unsigned int regno = EH_RETURN_DATA_REGNO (i);
3427 if (regno == INVALID_REGNUM)
3428 break;
3431 p = rtvec_alloc (i);
3433 for (i = 0; ; ++i)
3435 unsigned int regno = EH_RETURN_DATA_REGNO (i);
3436 if (regno == INVALID_REGNUM)
3437 break;
3439 rtx set
3440 = gen_frame_store (gen_rtx_REG (reg_mode, regno),
3441 sp_reg_rtx,
3442 info->ehrd_offset + sp_off + reg_size * (int) i);
3443 RTVEC_ELT (p, i) = set;
3444 RTX_FRAME_RELATED_P (set) = 1;
3447 insn = emit_insn (gen_blockage ());
3448 RTX_FRAME_RELATED_P (insn) = 1;
3449 add_reg_note (insn, REG_FRAME_RELATED_EXPR, gen_rtx_PARALLEL (VOIDmode, p));
3452 /* In AIX ABI we need to make sure r2 is really saved. */
3453 if (TARGET_AIX && crtl->calls_eh_return)
3455 rtx tmp_reg, tmp_reg_si, hi, lo, compare_result, toc_save_done, jump;
3456 rtx join_insn, note;
3457 rtx_insn *save_insn;
3458 long toc_restore_insn;
3460 tmp_reg = gen_rtx_REG (Pmode, 11);
3461 tmp_reg_si = gen_rtx_REG (SImode, 11);
3462 if (using_static_chain_p)
3464 START_USE (0);
3465 emit_move_insn (gen_rtx_REG (Pmode, 0), tmp_reg);
3467 else
3468 START_USE (11);
3469 emit_move_insn (tmp_reg, gen_rtx_REG (Pmode, LR_REGNO));
3470 /* Peek at instruction to which this function returns. If it's
3471 restoring r2, then we know we've already saved r2. We can't
3472 unconditionally save r2 because the value we have will already
3473 be updated if we arrived at this function via a plt call or
3474 toc adjusting stub. */
3475 emit_move_insn (tmp_reg_si, gen_rtx_MEM (SImode, tmp_reg));
3476 toc_restore_insn = ((TARGET_32BIT ? 0x80410000 : 0xE8410000)
3477 + RS6000_TOC_SAVE_SLOT);
3478 hi = gen_int_mode (toc_restore_insn & ~0xffff, SImode);
3479 emit_insn (gen_xorsi3 (tmp_reg_si, tmp_reg_si, hi));
3480 compare_result = gen_rtx_REG (CCUNSmode, CR0_REGNO);
3481 validate_condition_mode (EQ, CCUNSmode);
3482 lo = gen_int_mode (toc_restore_insn & 0xffff, SImode);
3483 emit_insn (gen_rtx_SET (compare_result,
3484 gen_rtx_COMPARE (CCUNSmode, tmp_reg_si, lo)));
3485 toc_save_done = gen_label_rtx ();
3486 jump = gen_rtx_IF_THEN_ELSE (VOIDmode,
3487 gen_rtx_EQ (VOIDmode, compare_result,
3488 const0_rtx),
3489 gen_rtx_LABEL_REF (VOIDmode, toc_save_done),
3490 pc_rtx);
3491 jump = emit_jump_insn (gen_rtx_SET (pc_rtx, jump));
3492 JUMP_LABEL (jump) = toc_save_done;
3493 LABEL_NUSES (toc_save_done) += 1;
3495 save_insn = emit_frame_save (frame_reg_rtx, reg_mode,
3496 TOC_REGNUM, frame_off + RS6000_TOC_SAVE_SLOT,
3497 sp_off - frame_off);
3499 emit_label (toc_save_done);
3501 /* ??? If we leave SAVE_INSN as marked as saving R2, then we'll
3502 have a CFG that has different saves along different paths.
3503 Move the note to a dummy blockage insn, which describes that
3504 R2 is unconditionally saved after the label. */
3505 /* ??? An alternate representation might be a special insn pattern
3506 containing both the branch and the store. That might let the
3507 code that minimizes the number of DW_CFA_advance opcodes better
3508 freedom in placing the annotations. */
3509 note = find_reg_note (save_insn, REG_FRAME_RELATED_EXPR, NULL);
3510 if (note)
3511 remove_note (save_insn, note);
3512 else
3513 note = alloc_reg_note (REG_FRAME_RELATED_EXPR,
3514 copy_rtx (PATTERN (save_insn)), NULL_RTX);
3515 RTX_FRAME_RELATED_P (save_insn) = 0;
3517 join_insn = emit_insn (gen_blockage ());
3518 REG_NOTES (join_insn) = note;
3519 RTX_FRAME_RELATED_P (join_insn) = 1;
3521 if (using_static_chain_p)
3523 emit_move_insn (tmp_reg, gen_rtx_REG (Pmode, 0));
3524 END_USE (0);
3526 else
3527 END_USE (11);
3530 /* Save CR if we use any that must be preserved. */
3531 if (!WORLD_SAVE_P (info) && info->cr_save_p)
3533 rtx addr = gen_rtx_PLUS (Pmode, frame_reg_rtx,
3534 GEN_INT (info->cr_save_offset + frame_off));
3535 rtx mem = gen_frame_mem (SImode, addr);
3537 /* If we didn't copy cr before, do so now using r0. */
3538 if (cr_save_rtx == NULL_RTX)
3540 START_USE (0);
3541 cr_save_rtx = gen_rtx_REG (SImode, 0);
3542 rs6000_emit_prologue_move_from_cr (cr_save_rtx);
3545 /* Saving CR requires a two-instruction sequence: one instruction
3546 to move the CR to a general-purpose register, and a second
3547 instruction that stores the GPR to memory.
3549 We do not emit any DWARF CFI records for the first of these,
3550 because we cannot properly represent the fact that CR is saved in
3551 a register. One reason is that we cannot express that multiple
3552 CR fields are saved; another reason is that on 64-bit, the size
3553 of the CR register in DWARF (4 bytes) differs from the size of
3554 a general-purpose register.
3556 This means if any intervening instruction were to clobber one of
3557 the call-saved CR fields, we'd have incorrect CFI. To prevent
3558 this from happening, we mark the store to memory as a use of
3559 those CR fields, which prevents any such instruction from being
3560 scheduled in between the two instructions. */
3561 rtx crsave_v[9];
3562 int n_crsave = 0;
3563 int i;
3565 crsave_v[n_crsave++] = gen_rtx_SET (mem, cr_save_rtx);
3566 for (i = 0; i < 8; i++)
3567 if (save_reg_p (CR0_REGNO + i))
3568 crsave_v[n_crsave++]
3569 = gen_rtx_USE (VOIDmode, gen_rtx_REG (CCmode, CR0_REGNO + i));
3571 insn = emit_insn (gen_rtx_PARALLEL (VOIDmode,
3572 gen_rtvec_v (n_crsave, crsave_v)));
3573 END_USE (REGNO (cr_save_rtx));
3575 /* Now, there's no way that dwarf2out_frame_debug_expr is going to
3576 understand '(unspec:SI [(reg:CC 68) ...] UNSPEC_MOVESI_FROM_CR)',
3577 so we need to construct a frame expression manually. */
3578 RTX_FRAME_RELATED_P (insn) = 1;
3580 /* Update address to be stack-pointer relative, like
3581 rs6000_frame_related would do. */
3582 addr = gen_rtx_PLUS (Pmode, gen_rtx_REG (Pmode, STACK_POINTER_REGNUM),
3583 GEN_INT (info->cr_save_offset + sp_off));
3584 mem = gen_frame_mem (SImode, addr);
3586 if (DEFAULT_ABI == ABI_ELFv2)
3588 /* In the ELFv2 ABI we generate separate CFI records for each
3589 CR field that was actually saved. They all point to the
3590 same 32-bit stack slot. */
3591 rtx crframe[8];
3592 int n_crframe = 0;
3594 for (i = 0; i < 8; i++)
3595 if (save_reg_p (CR0_REGNO + i))
3597 crframe[n_crframe]
3598 = gen_rtx_SET (mem, gen_rtx_REG (SImode, CR0_REGNO + i));
3600 RTX_FRAME_RELATED_P (crframe[n_crframe]) = 1;
3601 n_crframe++;
3604 add_reg_note (insn, REG_FRAME_RELATED_EXPR,
3605 gen_rtx_PARALLEL (VOIDmode,
3606 gen_rtvec_v (n_crframe, crframe)));
3608 else
3610 /* In other ABIs, by convention, we use a single CR regnum to
3611 represent the fact that all call-saved CR fields are saved.
3612 We use CR2_REGNO to be compatible with gcc-2.95 on Linux. */
3613 rtx set = gen_rtx_SET (mem, gen_rtx_REG (SImode, CR2_REGNO));
3614 add_reg_note (insn, REG_FRAME_RELATED_EXPR, set);
3618 /* In the ELFv2 ABI we need to save all call-saved CR fields into
3619 *separate* slots if the routine calls __builtin_eh_return, so
3620 that they can be independently restored by the unwinder. */
3621 if (DEFAULT_ABI == ABI_ELFv2 && crtl->calls_eh_return)
3623 int i, cr_off = info->ehcr_offset;
3624 rtx crsave;
3626 /* ??? We might get better performance by using multiple mfocrf
3627 instructions. */
3628 crsave = gen_rtx_REG (SImode, 0);
3629 emit_insn (gen_prologue_movesi_from_cr (crsave));
3631 for (i = 0; i < 8; i++)
3632 if (!call_used_or_fixed_reg_p (CR0_REGNO + i))
3634 rtvec p = rtvec_alloc (2);
3635 RTVEC_ELT (p, 0)
3636 = gen_frame_store (crsave, frame_reg_rtx, cr_off + frame_off);
3637 RTVEC_ELT (p, 1)
3638 = gen_rtx_USE (VOIDmode, gen_rtx_REG (CCmode, CR0_REGNO + i));
3640 insn = emit_insn (gen_rtx_PARALLEL (VOIDmode, p));
3642 RTX_FRAME_RELATED_P (insn) = 1;
3643 add_reg_note (insn, REG_FRAME_RELATED_EXPR,
3644 gen_frame_store (gen_rtx_REG (SImode, CR0_REGNO + i),
3645 sp_reg_rtx, cr_off + sp_off));
3647 cr_off += reg_size;
3651 /* If we are emitting stack probes, but allocate no stack, then
3652 just note that in the dump file. */
3653 if (flag_stack_clash_protection
3654 && dump_file
3655 && !info->push_p)
3656 dump_stack_clash_frame_info (NO_PROBE_NO_FRAME, false);
3658 /* Update stack and set back pointer unless this is V.4,
3659 for which it was done previously. */
3660 if (!WORLD_SAVE_P (info) && info->push_p
3661 && !(DEFAULT_ABI == ABI_V4 || crtl->calls_eh_return))
3663 rtx ptr_reg = NULL;
3664 int ptr_off = 0;
3666 /* If saving altivec regs we need to be able to address all save
3667 locations using a 16-bit offset. */
3668 if ((strategy & SAVE_INLINE_VRS) == 0
3669 || (info->altivec_size != 0
3670 && (info->altivec_save_offset + info->altivec_size - 16
3671 + info->total_size - frame_off) > 32767)
3672 || (info->vrsave_size != 0
3673 && (info->vrsave_save_offset
3674 + info->total_size - frame_off) > 32767))
3676 int sel = SAVRES_SAVE | SAVRES_VR;
3677 unsigned ptr_regno = ptr_regno_for_savres (sel);
3679 if (using_static_chain_p
3680 && ptr_regno == STATIC_CHAIN_REGNUM)
3681 ptr_regno = 12;
3682 if (REGNO (frame_reg_rtx) != ptr_regno)
3683 START_USE (ptr_regno);
3684 ptr_reg = gen_rtx_REG (Pmode, ptr_regno);
3685 frame_reg_rtx = ptr_reg;
3686 ptr_off = info->altivec_save_offset + info->altivec_size;
3687 frame_off = -ptr_off;
3689 else if (REGNO (frame_reg_rtx) == 1)
3690 frame_off = info->total_size;
3691 sp_adjust = rs6000_emit_allocate_stack (info->total_size,
3692 ptr_reg, ptr_off);
3693 if (REGNO (frame_reg_rtx) == 12)
3694 sp_adjust = 0;
3695 sp_off = info->total_size;
3696 if (frame_reg_rtx != sp_reg_rtx)
3697 rs6000_emit_stack_tie (frame_reg_rtx, false);
3700 /* Set frame pointer, if needed. */
3701 if (frame_pointer_needed_indeed)
3703 insn = emit_move_insn (gen_rtx_REG (Pmode, HARD_FRAME_POINTER_REGNUM),
3704 sp_reg_rtx);
3705 RTX_FRAME_RELATED_P (insn) = 1;
3708 /* Save AltiVec registers if needed. Save here because the red zone does
3709 not always include AltiVec registers. */
3710 if (!WORLD_SAVE_P (info)
3711 && info->altivec_size != 0 && (strategy & SAVE_INLINE_VRS) == 0)
3713 int end_save = info->altivec_save_offset + info->altivec_size;
3714 int ptr_off;
3715 /* Oddly, the vector save/restore functions point r0 at the end
3716 of the save area, then use r11 or r12 to load offsets for
3717 [reg+reg] addressing. */
3718 rtx ptr_reg = gen_rtx_REG (Pmode, 0);
3719 int scratch_regno = ptr_regno_for_savres (SAVRES_SAVE | SAVRES_VR);
3720 rtx scratch_reg = gen_rtx_REG (Pmode, scratch_regno);
3722 gcc_checking_assert (scratch_regno == 11 || scratch_regno == 12);
3723 NOT_INUSE (0);
3724 if (scratch_regno == 12)
3725 sp_adjust = 0;
3726 if (end_save + frame_off != 0)
3728 rtx offset = GEN_INT (end_save + frame_off);
3730 emit_insn (gen_add3_insn (ptr_reg, frame_reg_rtx, offset));
3732 else
3733 emit_move_insn (ptr_reg, frame_reg_rtx);
3735 ptr_off = -end_save;
3736 insn = rs6000_emit_savres_rtx (info, scratch_reg,
3737 info->altivec_save_offset + ptr_off,
3738 0, V4SImode, SAVRES_SAVE | SAVRES_VR);
3739 rs6000_frame_related (insn, scratch_reg, sp_off - ptr_off,
3740 NULL_RTX, NULL_RTX);
3741 if (REGNO (frame_reg_rtx) == REGNO (scratch_reg))
3743 /* The oddity mentioned above clobbered our frame reg. */
3744 emit_move_insn (frame_reg_rtx, ptr_reg);
3745 frame_off = ptr_off;
3748 else if (!WORLD_SAVE_P (info)
3749 && info->altivec_size != 0)
3751 int i;
3753 for (i = info->first_altivec_reg_save; i <= LAST_ALTIVEC_REGNO; ++i)
3754 if (info->vrsave_mask & ALTIVEC_REG_BIT (i))
3756 rtx areg, savereg, mem;
3757 HOST_WIDE_INT offset;
3759 offset = (info->altivec_save_offset + frame_off
3760 + 16 * (i - info->first_altivec_reg_save));
3762 savereg = gen_rtx_REG (V4SImode, i);
3764 if (TARGET_P9_VECTOR && quad_address_offset_p (offset))
3766 mem = gen_frame_mem (V4SImode,
3767 gen_rtx_PLUS (Pmode, frame_reg_rtx,
3768 GEN_INT (offset)));
3769 insn = emit_insn (gen_rtx_SET (mem, savereg));
3770 areg = NULL_RTX;
3772 else
3774 NOT_INUSE (0);
3775 areg = gen_rtx_REG (Pmode, 0);
3776 emit_move_insn (areg, GEN_INT (offset));
3778 /* AltiVec addressing mode is [reg+reg]. */
3779 mem = gen_frame_mem (V4SImode,
3780 gen_rtx_PLUS (Pmode, frame_reg_rtx, areg));
3782 /* Rather than emitting a generic move, force use of the stvx
3783 instruction, which we always want on ISA 2.07 (power8) systems.
3784 In particular we don't want xxpermdi/stxvd2x for little
3785 endian. */
3786 insn = emit_insn (gen_altivec_stvx_v4si_internal (mem, savereg));
3789 rs6000_frame_related (insn, frame_reg_rtx, sp_off - frame_off,
3790 areg, GEN_INT (offset));
3794 /* VRSAVE is a bit vector representing which AltiVec registers
3795 are used. The OS uses this to determine which vector
3796 registers to save on a context switch. We need to save
3797 VRSAVE on the stack frame, add whatever AltiVec registers we
3798 used in this function, and do the corresponding magic in the
3799 epilogue. */
3801 if (!WORLD_SAVE_P (info) && info->vrsave_size != 0)
3803 /* Get VRSAVE into a GPR. Note that ABI_V4 and ABI_DARWIN might
3804 be using r12 as frame_reg_rtx and r11 as the static chain
3805 pointer for nested functions. */
3806 int save_regno = 12;
3807 if ((DEFAULT_ABI == ABI_AIX || DEFAULT_ABI == ABI_ELFv2)
3808 && !using_static_chain_p)
3809 save_regno = 11;
3810 else if (using_split_stack || REGNO (frame_reg_rtx) == 12)
3812 save_regno = 11;
3813 if (using_static_chain_p)
3814 save_regno = 0;
3816 NOT_INUSE (save_regno);
3818 emit_vrsave_prologue (info, save_regno, frame_off, frame_reg_rtx);
3821 /* If we are using RS6000_PIC_OFFSET_TABLE_REGNUM, we need to set it up. */
3822 if (!TARGET_SINGLE_PIC_BASE
3823 && ((TARGET_TOC && TARGET_MINIMAL_TOC
3824 && !constant_pool_empty_p ())
3825 || (DEFAULT_ABI == ABI_V4
3826 && (flag_pic == 1 || (flag_pic && TARGET_SECURE_PLT))
3827 && df_regs_ever_live_p (RS6000_PIC_OFFSET_TABLE_REGNUM))))
3829 /* If emit_load_toc_table will use the link register, we need to save
3830 it. We use R12 for this purpose because emit_load_toc_table
3831 can use register 0. This allows us to use a plain 'blr' to return
3832 from the procedure more often. */
3833 int save_LR_around_toc_setup = (TARGET_ELF
3834 && DEFAULT_ABI == ABI_V4
3835 && flag_pic
3836 && ! info->lr_save_p
3837 && EDGE_COUNT (EXIT_BLOCK_PTR_FOR_FN (cfun)->preds) > 0);
3838 if (save_LR_around_toc_setup)
3840 rtx lr = gen_rtx_REG (Pmode, LR_REGNO);
3841 rtx tmp = gen_rtx_REG (Pmode, 12);
3843 sp_adjust = 0;
3844 insn = emit_move_insn (tmp, lr);
3845 RTX_FRAME_RELATED_P (insn) = 1;
3847 rs6000_emit_load_toc_table (TRUE);
3849 insn = emit_move_insn (lr, tmp);
3850 add_reg_note (insn, REG_CFA_RESTORE, lr);
3851 RTX_FRAME_RELATED_P (insn) = 1;
3853 else
3854 rs6000_emit_load_toc_table (TRUE);
3857 #if TARGET_MACHO
3858 if (!TARGET_SINGLE_PIC_BASE
3859 && DEFAULT_ABI == ABI_DARWIN
3860 && flag_pic && crtl->uses_pic_offset_table)
3862 rtx lr = gen_rtx_REG (Pmode, LR_REGNO);
3863 rtx src = gen_rtx_SYMBOL_REF (Pmode, MACHOPIC_FUNCTION_BASE_NAME);
3865 /* Save and restore LR locally around this call (in R0). */
3866 if (!info->lr_save_p)
3867 emit_move_insn (gen_rtx_REG (Pmode, 0), lr);
3869 emit_insn (gen_load_macho_picbase (Pmode, src));
3871 emit_move_insn (gen_rtx_REG (Pmode,
3872 RS6000_PIC_OFFSET_TABLE_REGNUM),
3873 lr);
3875 if (!info->lr_save_p)
3876 emit_move_insn (lr, gen_rtx_REG (Pmode, 0));
3878 #endif
3880 /* If we need to, save the TOC register after doing the stack setup.
3881 Do not emit eh frame info for this save. The unwinder wants info,
3882 conceptually attached to instructions in this function, about
3883 register values in the caller of this function. This R2 may have
3884 already been changed from the value in the caller.
3885 We don't attempt to write accurate DWARF EH frame info for R2
3886 because code emitted by gcc for a (non-pointer) function call
3887 doesn't save and restore R2. Instead, R2 is managed out-of-line
3888 by a linker generated plt call stub when the function resides in
3889 a shared library. This behavior is costly to describe in DWARF,
3890 both in terms of the size of DWARF info and the time taken in the
3891 unwinder to interpret it. R2 changes, apart from the
3892 calls_eh_return case earlier in this function, are handled by
3893 linux-unwind.h frob_update_context. */
3894 if (rs6000_save_toc_in_prologue_p ()
3895 && !cfun->machine->toc_is_wrapped_separately)
3897 rtx reg = gen_rtx_REG (reg_mode, TOC_REGNUM);
3898 emit_insn (gen_frame_store (reg, sp_reg_rtx, RS6000_TOC_SAVE_SLOT));
3901 /* Set up the arg pointer (r12) for -fsplit-stack code. */
3902 if (using_split_stack && split_stack_arg_pointer_used_p ())
3903 emit_split_stack_prologue (info, sp_adjust, frame_off, frame_reg_rtx);
3906 /* Output .extern statements for the save/restore routines we use. */
3908 static void
3909 rs6000_output_savres_externs (FILE *file)
3911 rs6000_stack_t *info = rs6000_stack_info ();
3913 if (TARGET_DEBUG_STACK)
3914 debug_stack_info (info);
3916 /* Write .extern for any function we will call to save and restore
3917 fp values. */
3918 if (info->first_fp_reg_save < 64
3919 && !TARGET_MACHO
3920 && !TARGET_ELF)
3922 char *name;
3923 int regno = info->first_fp_reg_save - 32;
3925 if ((info->savres_strategy & SAVE_INLINE_FPRS) == 0)
3927 bool lr = (info->savres_strategy & SAVE_NOINLINE_FPRS_SAVES_LR) != 0;
3928 int sel = SAVRES_SAVE | SAVRES_FPR | (lr ? SAVRES_LR : 0);
3929 name = rs6000_savres_routine_name (regno, sel);
3930 fprintf (file, "\t.extern %s\n", name);
3932 if ((info->savres_strategy & REST_INLINE_FPRS) == 0)
3934 bool lr = (info->savres_strategy
3935 & REST_NOINLINE_FPRS_DOESNT_RESTORE_LR) == 0;
3936 int sel = SAVRES_FPR | (lr ? SAVRES_LR : 0);
3937 name = rs6000_savres_routine_name (regno, sel);
3938 fprintf (file, "\t.extern %s\n", name);
3943 /* Write function prologue. */
3945 void
3946 rs6000_output_function_prologue (FILE *file)
3948 if (!cfun->is_thunk)
3950 rs6000_output_savres_externs (file);
3951 #ifdef USING_ELFOS_H
3952 const char *curr_machine = rs6000_machine_from_flags ();
3953 if (rs6000_machine != curr_machine)
3955 rs6000_machine = curr_machine;
3956 emit_asm_machine ();
3958 #endif
3961 /* ELFv2 ABI r2 setup code and local entry point. This must follow
3962 immediately after the global entry point label. */
3963 if (rs6000_global_entry_point_prologue_needed_p ())
3965 const char *name = XSTR (XEXP (DECL_RTL (current_function_decl), 0), 0);
3966 (*targetm.asm_out.internal_label) (file, "LCF", rs6000_pic_labelno);
3968 if (TARGET_CMODEL != CMODEL_LARGE)
3970 /* In the small and medium code models, we assume the TOC is less
3971 2 GB away from the text section, so it can be computed via the
3972 following two-instruction sequence. */
3973 char buf[256];
3975 ASM_GENERATE_INTERNAL_LABEL (buf, "LCF", rs6000_pic_labelno);
3976 fprintf (file, "0:\taddis 2,12,.TOC.-");
3977 assemble_name (file, buf);
3978 fprintf (file, "@ha\n");
3979 fprintf (file, "\taddi 2,2,.TOC.-");
3980 assemble_name (file, buf);
3981 fprintf (file, "@l\n");
3983 else
3985 /* In the large code model, we allow arbitrary offsets between the
3986 TOC and the text section, so we have to load the offset from
3987 memory. The data field is emitted directly before the global
3988 entry point in rs6000_elf_declare_function_name. */
3989 char buf[256];
3991 #ifdef HAVE_AS_ENTRY_MARKERS
3992 /* If supported by the linker, emit a marker relocation. If the
3993 total code size of the final executable or shared library
3994 happens to fit into 2 GB after all, the linker will replace
3995 this code sequence with the sequence for the small or medium
3996 code model. */
3997 fprintf (file, "\t.reloc .,R_PPC64_ENTRY\n");
3998 #endif
3999 fprintf (file, "\tld 2,");
4000 ASM_GENERATE_INTERNAL_LABEL (buf, "LCL", rs6000_pic_labelno);
4001 assemble_name (file, buf);
4002 fprintf (file, "-");
4003 ASM_GENERATE_INTERNAL_LABEL (buf, "LCF", rs6000_pic_labelno);
4004 assemble_name (file, buf);
4005 fprintf (file, "(12)\n");
4006 fprintf (file, "\tadd 2,2,12\n");
4009 unsigned short patch_area_size = crtl->patch_area_size;
4010 unsigned short patch_area_entry = crtl->patch_area_entry;
4011 /* Need to emit the patching area. */
4012 if (patch_area_size > 0)
4014 cfun->machine->global_entry_emitted = true;
4015 /* As ELFv2 ABI shows, the allowable bytes between the global
4016 and local entry points are 0, 4, 8, 16, 32 and 64 when
4017 there is a local entry point. Considering there are two
4018 non-prefixed instructions for global entry point prologue
4019 (8 bytes), the count for patchable nops before local entry
4020 point would be 2, 6 and 14. It's possible to support those
4021 other counts of nops by not making a local entry point, but
4022 we don't have clear use cases for them, so leave them
4023 unsupported for now. */
4024 if (patch_area_entry > 0)
4026 if (patch_area_entry != 2
4027 && patch_area_entry != 6
4028 && patch_area_entry != 14)
4029 error ("unsupported number of nops before function entry (%u)",
4030 patch_area_entry);
4031 rs6000_print_patchable_function_entry (file, patch_area_entry,
4032 true);
4033 patch_area_size -= patch_area_entry;
4037 fputs ("\t.localentry\t", file);
4038 assemble_name (file, name);
4039 fputs (",.-", file);
4040 assemble_name (file, name);
4041 fputs ("\n", file);
4042 /* Emit the nops after local entry. */
4043 if (patch_area_size > 0)
4044 rs6000_print_patchable_function_entry (file, patch_area_size,
4045 patch_area_entry == 0);
4048 else if (rs6000_pcrel_p ())
4050 const char *name = XSTR (XEXP (DECL_RTL (current_function_decl), 0), 0);
4051 /* All functions compiled to use PC-relative addressing will
4052 have a .localentry value of 0 or 1. For now we set it to
4053 1 all the time, indicating that the function may clobber
4054 the TOC register r2. Later we may optimize this by setting
4055 it to 0 if the function is a leaf and does not clobber r2. */
4056 fputs ("\t.localentry\t", file);
4057 assemble_name (file, name);
4058 fputs (",1\n", file);
4061 /* Output -mprofile-kernel code. This needs to be done here instead of
4062 in output_function_profile since it must go after the ELFv2 ABI
4063 local entry point. */
4064 if (TARGET_PROFILE_KERNEL && crtl->profile)
4066 gcc_assert (DEFAULT_ABI == ABI_AIX || DEFAULT_ABI == ABI_ELFv2);
4067 gcc_assert (!TARGET_32BIT);
4069 asm_fprintf (file, "\tmflr %s\n", reg_names[0]);
4071 /* In the ELFv2 ABI we have no compiler stack word. It must be
4072 the resposibility of _mcount to preserve the static chain
4073 register if required. */
4074 if (DEFAULT_ABI != ABI_ELFv2
4075 && cfun->static_chain_decl != NULL)
4077 asm_fprintf (file, "\tstd %s,24(%s)\n",
4078 reg_names[STATIC_CHAIN_REGNUM], reg_names[1]);
4079 fprintf (file, "\tbl %s\n", RS6000_MCOUNT);
4080 asm_fprintf (file, "\tld %s,24(%s)\n",
4081 reg_names[STATIC_CHAIN_REGNUM], reg_names[1]);
4083 else
4084 fprintf (file, "\tbl %s\n", RS6000_MCOUNT);
4087 rs6000_pic_labelno++;
4090 /* -mprofile-kernel code calls mcount before the function prolog,
4091 so a profiled leaf function should stay a leaf function. */
4092 bool
4093 rs6000_keep_leaf_when_profiled (void)
4095 return TARGET_PROFILE_KERNEL;
4098 /* Non-zero if vmx regs are restored before the frame pop, zero if
4099 we restore after the pop when possible. */
4100 #define ALWAYS_RESTORE_ALTIVEC_BEFORE_POP 0
4102 /* Restoring cr is a two step process: loading a reg from the frame
4103 save, then moving the reg to cr. For ABI_V4 we must let the
4104 unwinder know that the stack location is no longer valid at or
4105 before the stack deallocation, but we can't emit a cfa_restore for
4106 cr at the stack deallocation like we do for other registers.
4107 The trouble is that it is possible for the move to cr to be
4108 scheduled after the stack deallocation. So say exactly where cr
4109 is located on each of the two insns. */
4111 static rtx
4112 load_cr_save (int regno, rtx frame_reg_rtx, int offset, bool exit_func)
4114 rtx mem = gen_frame_mem_offset (SImode, frame_reg_rtx, offset);
4115 rtx reg = gen_rtx_REG (SImode, regno);
4116 rtx_insn *insn = emit_move_insn (reg, mem);
4118 if (!exit_func && DEFAULT_ABI == ABI_V4)
4120 rtx cr = gen_rtx_REG (SImode, CR2_REGNO);
4121 rtx set = gen_rtx_SET (reg, cr);
4123 add_reg_note (insn, REG_CFA_REGISTER, set);
4124 RTX_FRAME_RELATED_P (insn) = 1;
4126 return reg;
4129 /* Reload CR from REG. */
4131 static void
4132 restore_saved_cr (rtx reg, bool using_mfcr_multiple, bool exit_func)
4134 int count = 0;
4135 int i;
4137 if (using_mfcr_multiple)
4139 for (i = 0; i < 8; i++)
4140 if (save_reg_p (CR0_REGNO + i))
4141 count++;
4142 gcc_assert (count);
4145 if (using_mfcr_multiple && count > 1)
4147 rtx_insn *insn;
4148 rtvec p;
4149 int ndx;
4151 p = rtvec_alloc (count);
4153 ndx = 0;
4154 for (i = 0; i < 8; i++)
4155 if (save_reg_p (CR0_REGNO + i))
4157 rtvec r = rtvec_alloc (2);
4158 RTVEC_ELT (r, 0) = reg;
4159 RTVEC_ELT (r, 1) = GEN_INT (1 << (7-i));
4160 RTVEC_ELT (p, ndx) =
4161 gen_rtx_SET (gen_rtx_REG (CCmode, CR0_REGNO + i),
4162 gen_rtx_UNSPEC (CCmode, r, UNSPEC_MOVESI_TO_CR));
4163 ndx++;
4165 insn = emit_insn (gen_rtx_PARALLEL (VOIDmode, p));
4166 gcc_assert (ndx == count);
4168 /* For the ELFv2 ABI we generate a CFA_RESTORE for each
4169 CR field separately. */
4170 if (!exit_func && DEFAULT_ABI == ABI_ELFv2 && flag_shrink_wrap)
4172 for (i = 0; i < 8; i++)
4173 if (save_reg_p (CR0_REGNO + i))
4174 add_reg_note (insn, REG_CFA_RESTORE,
4175 gen_rtx_REG (SImode, CR0_REGNO + i));
4177 RTX_FRAME_RELATED_P (insn) = 1;
4180 else
4181 for (i = 0; i < 8; i++)
4182 if (save_reg_p (CR0_REGNO + i))
4184 rtx insn = emit_insn (gen_movsi_to_cr_one
4185 (gen_rtx_REG (CCmode, CR0_REGNO + i), reg));
4187 /* For the ELFv2 ABI we generate a CFA_RESTORE for each
4188 CR field separately, attached to the insn that in fact
4189 restores this particular CR field. */
4190 if (!exit_func && DEFAULT_ABI == ABI_ELFv2 && flag_shrink_wrap)
4192 add_reg_note (insn, REG_CFA_RESTORE,
4193 gen_rtx_REG (SImode, CR0_REGNO + i));
4195 RTX_FRAME_RELATED_P (insn) = 1;
4199 /* For other ABIs, we just generate a single CFA_RESTORE for CR2. */
4200 if (!exit_func && DEFAULT_ABI != ABI_ELFv2
4201 && (DEFAULT_ABI == ABI_V4 || flag_shrink_wrap))
4203 rtx_insn *insn = get_last_insn ();
4204 rtx cr = gen_rtx_REG (SImode, CR2_REGNO);
4206 add_reg_note (insn, REG_CFA_RESTORE, cr);
4207 RTX_FRAME_RELATED_P (insn) = 1;
4211 /* Like cr, the move to lr instruction can be scheduled after the
4212 stack deallocation, but unlike cr, its stack frame save is still
4213 valid. So we only need to emit the cfa_restore on the correct
4214 instruction. */
4216 static void
4217 load_lr_save (int regno, rtx frame_reg_rtx, int offset)
4219 rtx mem = gen_frame_mem_offset (Pmode, frame_reg_rtx, offset);
4220 rtx reg = gen_rtx_REG (Pmode, regno);
4222 emit_move_insn (reg, mem);
4225 static void
4226 restore_saved_lr (int regno, bool exit_func)
4228 rtx reg = gen_rtx_REG (Pmode, regno);
4229 rtx lr = gen_rtx_REG (Pmode, LR_REGNO);
4230 rtx_insn *insn = emit_move_insn (lr, reg);
4232 if (!exit_func && flag_shrink_wrap)
4234 add_reg_note (insn, REG_CFA_RESTORE, lr);
4235 RTX_FRAME_RELATED_P (insn) = 1;
4239 static rtx
4240 add_crlr_cfa_restore (const rs6000_stack_t *info, rtx cfa_restores)
4242 if (DEFAULT_ABI == ABI_ELFv2)
4244 int i;
4245 for (i = 0; i < 8; i++)
4246 if (save_reg_p (CR0_REGNO + i))
4248 rtx cr = gen_rtx_REG (SImode, CR0_REGNO + i);
4249 cfa_restores = alloc_reg_note (REG_CFA_RESTORE, cr,
4250 cfa_restores);
4253 else if (info->cr_save_p)
4254 cfa_restores = alloc_reg_note (REG_CFA_RESTORE,
4255 gen_rtx_REG (SImode, CR2_REGNO),
4256 cfa_restores);
4258 if (info->lr_save_p)
4259 cfa_restores = alloc_reg_note (REG_CFA_RESTORE,
4260 gen_rtx_REG (Pmode, LR_REGNO),
4261 cfa_restores);
4262 return cfa_restores;
4265 /* Return true if OFFSET from stack pointer can be clobbered by signals.
4266 V.4 doesn't have any stack cushion, AIX ABIs have 220 or 288 bytes
4267 below stack pointer not cloberred by signals. */
4269 static inline bool
4270 offset_below_red_zone_p (HOST_WIDE_INT offset)
4272 return offset < (DEFAULT_ABI == ABI_V4
4274 : TARGET_32BIT ? -220 : -288);
4277 /* Append CFA_RESTORES to any existing REG_NOTES on the last insn. */
4279 static void
4280 emit_cfa_restores (rtx cfa_restores)
4282 rtx_insn *insn = get_last_insn ();
4283 rtx *loc = &REG_NOTES (insn);
4285 while (*loc)
4286 loc = &XEXP (*loc, 1);
4287 *loc = cfa_restores;
4288 RTX_FRAME_RELATED_P (insn) = 1;
4291 /* Emit function epilogue as insns. */
4293 void
4294 rs6000_emit_epilogue (enum epilogue_type epilogue_type)
4296 HOST_WIDE_INT frame_off = 0;
4297 rtx sp_reg_rtx = gen_rtx_REG (Pmode, 1);
4298 rtx frame_reg_rtx = sp_reg_rtx;
4299 rtx cfa_restores = NULL_RTX;
4300 rtx insn;
4301 rtx cr_save_reg = NULL_RTX;
4302 machine_mode reg_mode = Pmode;
4303 int reg_size = TARGET_32BIT ? 4 : 8;
4304 machine_mode fp_reg_mode = TARGET_HARD_FLOAT ? DFmode : SFmode;
4305 int fp_reg_size = 8;
4306 int i;
4307 unsigned ptr_regno;
4309 rs6000_stack_t *info = rs6000_stack_info ();
4311 if (epilogue_type == EPILOGUE_TYPE_NORMAL && crtl->calls_eh_return)
4312 epilogue_type = EPILOGUE_TYPE_EH_RETURN;
4314 int strategy = info->savres_strategy;
4315 bool using_load_multiple = !!(strategy & REST_MULTIPLE);
4316 bool restoring_GPRs_inline = !!(strategy & REST_INLINE_GPRS);
4317 bool restoring_FPRs_inline = !!(strategy & REST_INLINE_FPRS);
4318 if (epilogue_type == EPILOGUE_TYPE_SIBCALL)
4320 restoring_GPRs_inline = true;
4321 restoring_FPRs_inline = true;
4324 bool using_mtcr_multiple = (rs6000_tune == PROCESSOR_PPC601
4325 || rs6000_tune == PROCESSOR_PPC603
4326 || rs6000_tune == PROCESSOR_PPC750
4327 || optimize_size);
4329 /* Restore via the backchain when we have a large frame, since this
4330 is more efficient than an addis, addi pair. The second condition
4331 here will not trigger at the moment; We don't actually need a
4332 frame pointer for alloca, but the generic parts of the compiler
4333 give us one anyway. */
4334 bool use_backchain_to_restore_sp
4335 = (info->total_size + (info->lr_save_p ? info->lr_save_offset : 0) > 32767
4336 || (cfun->calls_alloca && !frame_pointer_needed));
4338 bool restore_lr = (info->lr_save_p
4339 && (restoring_FPRs_inline
4340 || (strategy & REST_NOINLINE_FPRS_DOESNT_RESTORE_LR))
4341 && (restoring_GPRs_inline
4342 || info->first_fp_reg_save < 64)
4343 && !cfun->machine->lr_is_wrapped_separately);
4346 if (WORLD_SAVE_P (info))
4348 gcc_assert (epilogue_type != EPILOGUE_TYPE_SIBCALL);
4350 /* eh_rest_world_r10 will return to the location saved in the LR
4351 stack slot (which is not likely to be our caller.)
4352 Input: R10 -- stack adjustment. Clobbers R0, R11, R12, R7, R8.
4353 rest_world is similar, except any R10 parameter is ignored.
4354 The exception-handling stuff that was here in 2.95 is no
4355 longer necessary. */
4357 rtvec p;
4358 p = rtvec_alloc (9
4359 + 32 - info->first_gp_reg_save
4360 + LAST_ALTIVEC_REGNO + 1 - info->first_altivec_reg_save
4361 + 63 + 1 - info->first_fp_reg_save);
4363 const char *rname;
4364 switch (epilogue_type)
4366 case EPILOGUE_TYPE_NORMAL:
4367 rname = ggc_strdup ("*rest_world");
4368 break;
4370 case EPILOGUE_TYPE_EH_RETURN:
4371 rname = ggc_strdup ("*eh_rest_world_r10");
4372 break;
4374 default:
4375 gcc_unreachable ();
4378 int j = 0;
4379 RTVEC_ELT (p, j++) = ret_rtx;
4380 RTVEC_ELT (p, j++)
4381 = gen_rtx_USE (VOIDmode, gen_rtx_SYMBOL_REF (Pmode, rname));
4382 /* The instruction pattern requires a clobber here;
4383 it is shared with the restVEC helper. */
4384 RTVEC_ELT (p, j++) = gen_hard_reg_clobber (Pmode, 11);
4387 /* CR register traditionally saved as CR2. */
4388 rtx reg = gen_rtx_REG (SImode, CR2_REGNO);
4389 RTVEC_ELT (p, j++)
4390 = gen_frame_load (reg, frame_reg_rtx, info->cr_save_offset);
4391 if (flag_shrink_wrap)
4393 cfa_restores = alloc_reg_note (REG_CFA_RESTORE,
4394 gen_rtx_REG (Pmode, LR_REGNO),
4395 cfa_restores);
4396 cfa_restores = alloc_reg_note (REG_CFA_RESTORE, reg, cfa_restores);
4400 int i;
4401 for (i = 0; i < 32 - info->first_gp_reg_save; i++)
4403 rtx reg = gen_rtx_REG (reg_mode, info->first_gp_reg_save + i);
4404 RTVEC_ELT (p, j++)
4405 = gen_frame_load (reg,
4406 frame_reg_rtx, info->gp_save_offset + reg_size * i);
4407 if (flag_shrink_wrap
4408 && save_reg_p (info->first_gp_reg_save + i))
4409 cfa_restores = alloc_reg_note (REG_CFA_RESTORE, reg, cfa_restores);
4411 for (i = 0; info->first_altivec_reg_save + i <= LAST_ALTIVEC_REGNO; i++)
4413 rtx reg = gen_rtx_REG (V4SImode, info->first_altivec_reg_save + i);
4414 RTVEC_ELT (p, j++)
4415 = gen_frame_load (reg,
4416 frame_reg_rtx, info->altivec_save_offset + 16 * i);
4417 if (flag_shrink_wrap
4418 && save_reg_p (info->first_altivec_reg_save + i))
4419 cfa_restores = alloc_reg_note (REG_CFA_RESTORE, reg, cfa_restores);
4421 for (i = 0; info->first_fp_reg_save + i <= 63; i++)
4423 rtx reg = gen_rtx_REG (TARGET_HARD_FLOAT ? DFmode : SFmode,
4424 info->first_fp_reg_save + i);
4425 RTVEC_ELT (p, j++)
4426 = gen_frame_load (reg, frame_reg_rtx, info->fp_save_offset + 8 * i);
4427 if (flag_shrink_wrap
4428 && save_reg_p (info->first_fp_reg_save + i))
4429 cfa_restores = alloc_reg_note (REG_CFA_RESTORE, reg, cfa_restores);
4431 RTVEC_ELT (p, j++) = gen_hard_reg_clobber (Pmode, 0);
4432 RTVEC_ELT (p, j++) = gen_hard_reg_clobber (SImode, 12);
4433 RTVEC_ELT (p, j++) = gen_hard_reg_clobber (SImode, 7);
4434 RTVEC_ELT (p, j++) = gen_hard_reg_clobber (SImode, 8);
4435 RTVEC_ELT (p, j++)
4436 = gen_rtx_USE (VOIDmode, gen_rtx_REG (SImode, 10));
4437 insn = emit_jump_insn (gen_rtx_PARALLEL (VOIDmode, p));
4439 if (flag_shrink_wrap)
4441 REG_NOTES (insn) = cfa_restores;
4442 add_reg_note (insn, REG_CFA_DEF_CFA, sp_reg_rtx);
4443 RTX_FRAME_RELATED_P (insn) = 1;
4445 return;
4448 /* frame_reg_rtx + frame_off points to the top of this stack frame. */
4449 if (info->push_p)
4450 frame_off = info->total_size;
4452 /* Restore AltiVec registers if we must do so before adjusting the
4453 stack. */
4454 if (info->altivec_size != 0
4455 && (ALWAYS_RESTORE_ALTIVEC_BEFORE_POP
4456 || (DEFAULT_ABI != ABI_V4
4457 && offset_below_red_zone_p (info->altivec_save_offset))))
4459 int i;
4460 int scratch_regno = ptr_regno_for_savres (SAVRES_VR);
4462 gcc_checking_assert (scratch_regno == 11 || scratch_regno == 12);
4463 if (use_backchain_to_restore_sp)
4465 int frame_regno = 11;
4467 if ((strategy & REST_INLINE_VRS) == 0)
4469 /* Of r11 and r12, select the one not clobbered by an
4470 out-of-line restore function for the frame register. */
4471 frame_regno = 11 + 12 - scratch_regno;
4473 frame_reg_rtx = gen_rtx_REG (Pmode, frame_regno);
4474 emit_move_insn (frame_reg_rtx,
4475 gen_rtx_MEM (Pmode, sp_reg_rtx));
4476 frame_off = 0;
4478 else if (frame_pointer_needed)
4479 frame_reg_rtx = hard_frame_pointer_rtx;
4481 if ((strategy & REST_INLINE_VRS) == 0)
4483 int end_save = info->altivec_save_offset + info->altivec_size;
4484 int ptr_off;
4485 rtx ptr_reg = gen_rtx_REG (Pmode, 0);
4486 rtx scratch_reg = gen_rtx_REG (Pmode, scratch_regno);
4488 if (end_save + frame_off != 0)
4490 rtx offset = GEN_INT (end_save + frame_off);
4492 emit_insn (gen_add3_insn (ptr_reg, frame_reg_rtx, offset));
4494 else
4495 emit_move_insn (ptr_reg, frame_reg_rtx);
4497 ptr_off = -end_save;
4498 insn = rs6000_emit_savres_rtx (info, scratch_reg,
4499 info->altivec_save_offset + ptr_off,
4500 0, V4SImode, SAVRES_VR);
4502 else
4504 for (i = info->first_altivec_reg_save; i <= LAST_ALTIVEC_REGNO; ++i)
4505 if (info->vrsave_mask & ALTIVEC_REG_BIT (i))
4507 rtx addr, areg, mem, insn;
4508 rtx reg = gen_rtx_REG (V4SImode, i);
4509 HOST_WIDE_INT offset
4510 = (info->altivec_save_offset + frame_off
4511 + 16 * (i - info->first_altivec_reg_save));
4513 if (TARGET_P9_VECTOR && quad_address_offset_p (offset))
4515 mem = gen_frame_mem (V4SImode,
4516 gen_rtx_PLUS (Pmode, frame_reg_rtx,
4517 GEN_INT (offset)));
4518 insn = gen_rtx_SET (reg, mem);
4520 else
4522 areg = gen_rtx_REG (Pmode, 0);
4523 emit_move_insn (areg, GEN_INT (offset));
4525 /* AltiVec addressing mode is [reg+reg]. */
4526 addr = gen_rtx_PLUS (Pmode, frame_reg_rtx, areg);
4527 mem = gen_frame_mem (V4SImode, addr);
4529 /* Rather than emitting a generic move, force use of the
4530 lvx instruction, which we always want. In particular we
4531 don't want lxvd2x/xxpermdi for little endian. */
4532 insn = gen_altivec_lvx_v4si_internal (reg, mem);
4535 (void) emit_insn (insn);
4539 for (i = info->first_altivec_reg_save; i <= LAST_ALTIVEC_REGNO; ++i)
4540 if (((strategy & REST_INLINE_VRS) == 0
4541 || (info->vrsave_mask & ALTIVEC_REG_BIT (i)) != 0)
4542 && (flag_shrink_wrap
4543 || (offset_below_red_zone_p
4544 (info->altivec_save_offset
4545 + 16 * (i - info->first_altivec_reg_save))))
4546 && save_reg_p (i))
4548 rtx reg = gen_rtx_REG (V4SImode, i);
4549 cfa_restores = alloc_reg_note (REG_CFA_RESTORE, reg, cfa_restores);
4553 /* Restore VRSAVE if we must do so before adjusting the stack. */
4554 if (info->vrsave_size != 0
4555 && (ALWAYS_RESTORE_ALTIVEC_BEFORE_POP
4556 || (DEFAULT_ABI != ABI_V4
4557 && offset_below_red_zone_p (info->vrsave_save_offset))))
4559 rtx reg;
4561 if (frame_reg_rtx == sp_reg_rtx)
4563 if (use_backchain_to_restore_sp)
4565 frame_reg_rtx = gen_rtx_REG (Pmode, 11);
4566 emit_move_insn (frame_reg_rtx,
4567 gen_rtx_MEM (Pmode, sp_reg_rtx));
4568 frame_off = 0;
4570 else if (frame_pointer_needed)
4571 frame_reg_rtx = hard_frame_pointer_rtx;
4574 reg = gen_rtx_REG (SImode, 12);
4575 emit_insn (gen_frame_load (reg, frame_reg_rtx,
4576 info->vrsave_save_offset + frame_off));
4578 emit_insn (generate_set_vrsave (reg, info, 1));
4581 insn = NULL_RTX;
4582 /* If we have a large stack frame, restore the old stack pointer
4583 using the backchain. */
4584 if (use_backchain_to_restore_sp)
4586 if (frame_reg_rtx == sp_reg_rtx)
4588 /* Under V.4, don't reset the stack pointer until after we're done
4589 loading the saved registers. */
4590 if (DEFAULT_ABI == ABI_V4)
4591 frame_reg_rtx = gen_rtx_REG (Pmode, 11);
4593 insn = emit_move_insn (frame_reg_rtx,
4594 gen_rtx_MEM (Pmode, sp_reg_rtx));
4595 frame_off = 0;
4597 else if (ALWAYS_RESTORE_ALTIVEC_BEFORE_POP
4598 && DEFAULT_ABI == ABI_V4)
4599 /* frame_reg_rtx has been set up by the altivec restore. */
4601 else
4603 insn = emit_move_insn (sp_reg_rtx, frame_reg_rtx);
4604 frame_reg_rtx = sp_reg_rtx;
4607 /* If we have a frame pointer, we can restore the old stack pointer
4608 from it. */
4609 else if (frame_pointer_needed_indeed)
4611 frame_reg_rtx = sp_reg_rtx;
4612 if (DEFAULT_ABI == ABI_V4)
4613 frame_reg_rtx = gen_rtx_REG (Pmode, 11);
4614 /* Prevent reordering memory accesses against stack pointer restore. */
4615 else if (cfun->calls_alloca
4616 || offset_below_red_zone_p (-info->total_size))
4617 rs6000_emit_stack_tie (frame_reg_rtx, true);
4619 insn = emit_insn (gen_add3_insn (frame_reg_rtx, hard_frame_pointer_rtx,
4620 GEN_INT (info->total_size)));
4621 frame_off = 0;
4623 else if (info->push_p
4624 && DEFAULT_ABI != ABI_V4
4625 && epilogue_type != EPILOGUE_TYPE_EH_RETURN)
4627 /* Prevent reordering memory accesses against stack pointer restore. */
4628 if (cfun->calls_alloca
4629 || offset_below_red_zone_p (-info->total_size))
4630 rs6000_emit_stack_tie (frame_reg_rtx, false);
4631 insn = emit_insn (gen_add3_insn (sp_reg_rtx, sp_reg_rtx,
4632 GEN_INT (info->total_size)));
4633 frame_off = 0;
4635 if (insn && frame_reg_rtx == sp_reg_rtx)
4637 if (cfa_restores)
4639 REG_NOTES (insn) = cfa_restores;
4640 cfa_restores = NULL_RTX;
4642 add_reg_note (insn, REG_CFA_DEF_CFA, sp_reg_rtx);
4643 RTX_FRAME_RELATED_P (insn) = 1;
4646 /* Restore AltiVec registers if we have not done so already. */
4647 if (!ALWAYS_RESTORE_ALTIVEC_BEFORE_POP
4648 && info->altivec_size != 0
4649 && (DEFAULT_ABI == ABI_V4
4650 || !offset_below_red_zone_p (info->altivec_save_offset)))
4652 int i;
4654 if ((strategy & REST_INLINE_VRS) == 0)
4656 int end_save = info->altivec_save_offset + info->altivec_size;
4657 int ptr_off;
4658 rtx ptr_reg = gen_rtx_REG (Pmode, 0);
4659 int scratch_regno = ptr_regno_for_savres (SAVRES_VR);
4660 rtx scratch_reg = gen_rtx_REG (Pmode, scratch_regno);
4662 if (end_save + frame_off != 0)
4664 rtx offset = GEN_INT (end_save + frame_off);
4666 emit_insn (gen_add3_insn (ptr_reg, frame_reg_rtx, offset));
4668 else
4669 emit_move_insn (ptr_reg, frame_reg_rtx);
4671 ptr_off = -end_save;
4672 insn = rs6000_emit_savres_rtx (info, scratch_reg,
4673 info->altivec_save_offset + ptr_off,
4674 0, V4SImode, SAVRES_VR);
4675 if (REGNO (frame_reg_rtx) == REGNO (scratch_reg))
4677 /* Frame reg was clobbered by out-of-line save. Restore it
4678 from ptr_reg, and if we are calling out-of-line gpr or
4679 fpr restore set up the correct pointer and offset. */
4680 unsigned newptr_regno = 1;
4681 if (!restoring_GPRs_inline)
4683 bool lr = info->gp_save_offset + info->gp_size == 0;
4684 int sel = SAVRES_GPR | (lr ? SAVRES_LR : 0);
4685 newptr_regno = ptr_regno_for_savres (sel);
4686 end_save = info->gp_save_offset + info->gp_size;
4688 else if (!restoring_FPRs_inline)
4690 bool lr = !(strategy & REST_NOINLINE_FPRS_DOESNT_RESTORE_LR);
4691 int sel = SAVRES_FPR | (lr ? SAVRES_LR : 0);
4692 newptr_regno = ptr_regno_for_savres (sel);
4693 end_save = info->fp_save_offset + info->fp_size;
4696 if (newptr_regno != 1 && REGNO (frame_reg_rtx) != newptr_regno)
4697 frame_reg_rtx = gen_rtx_REG (Pmode, newptr_regno);
4699 if (end_save + ptr_off != 0)
4701 rtx offset = GEN_INT (end_save + ptr_off);
4703 frame_off = -end_save;
4704 if (TARGET_32BIT)
4705 emit_insn (gen_addsi3_carry (frame_reg_rtx,
4706 ptr_reg, offset));
4707 else
4708 emit_insn (gen_adddi3_carry (frame_reg_rtx,
4709 ptr_reg, offset));
4711 else
4713 frame_off = ptr_off;
4714 emit_move_insn (frame_reg_rtx, ptr_reg);
4718 else
4720 for (i = info->first_altivec_reg_save; i <= LAST_ALTIVEC_REGNO; ++i)
4721 if (info->vrsave_mask & ALTIVEC_REG_BIT (i))
4723 rtx addr, areg, mem, insn;
4724 rtx reg = gen_rtx_REG (V4SImode, i);
4725 HOST_WIDE_INT offset
4726 = (info->altivec_save_offset + frame_off
4727 + 16 * (i - info->first_altivec_reg_save));
4729 if (TARGET_P9_VECTOR && quad_address_offset_p (offset))
4731 mem = gen_frame_mem (V4SImode,
4732 gen_rtx_PLUS (Pmode, frame_reg_rtx,
4733 GEN_INT (offset)));
4734 insn = gen_rtx_SET (reg, mem);
4736 else
4738 areg = gen_rtx_REG (Pmode, 0);
4739 emit_move_insn (areg, GEN_INT (offset));
4741 /* AltiVec addressing mode is [reg+reg]. */
4742 addr = gen_rtx_PLUS (Pmode, frame_reg_rtx, areg);
4743 mem = gen_frame_mem (V4SImode, addr);
4745 /* Rather than emitting a generic move, force use of the
4746 lvx instruction, which we always want. In particular we
4747 don't want lxvd2x/xxpermdi for little endian. */
4748 insn = gen_altivec_lvx_v4si_internal (reg, mem);
4751 (void) emit_insn (insn);
4755 for (i = info->first_altivec_reg_save; i <= LAST_ALTIVEC_REGNO; ++i)
4756 if (((strategy & REST_INLINE_VRS) == 0
4757 || (info->vrsave_mask & ALTIVEC_REG_BIT (i)) != 0)
4758 && (DEFAULT_ABI == ABI_V4 || flag_shrink_wrap)
4759 && save_reg_p (i))
4761 rtx reg = gen_rtx_REG (V4SImode, i);
4762 cfa_restores = alloc_reg_note (REG_CFA_RESTORE, reg, cfa_restores);
4766 /* Restore VRSAVE if we have not done so already. */
4767 if (!ALWAYS_RESTORE_ALTIVEC_BEFORE_POP
4768 && info->vrsave_size != 0
4769 && (DEFAULT_ABI == ABI_V4
4770 || !offset_below_red_zone_p (info->vrsave_save_offset)))
4772 rtx reg;
4774 reg = gen_rtx_REG (SImode, 12);
4775 emit_insn (gen_frame_load (reg, frame_reg_rtx,
4776 info->vrsave_save_offset + frame_off));
4778 emit_insn (generate_set_vrsave (reg, info, 1));
4781 /* If we exit by an out-of-line restore function on ABI_V4 then that
4782 function will deallocate the stack, so we don't need to worry
4783 about the unwinder restoring cr from an invalid stack frame
4784 location. */
4785 bool exit_func = (!restoring_FPRs_inline
4786 || (!restoring_GPRs_inline
4787 && info->first_fp_reg_save == 64));
4789 /* In the ELFv2 ABI we need to restore all call-saved CR fields from
4790 *separate* slots if the routine calls __builtin_eh_return, so
4791 that they can be independently restored by the unwinder. */
4792 if (DEFAULT_ABI == ABI_ELFv2 && crtl->calls_eh_return)
4794 int i, cr_off = info->ehcr_offset;
4796 for (i = 0; i < 8; i++)
4797 if (!call_used_or_fixed_reg_p (CR0_REGNO + i))
4799 rtx reg = gen_rtx_REG (SImode, 0);
4800 emit_insn (gen_frame_load (reg, frame_reg_rtx,
4801 cr_off + frame_off));
4803 insn = emit_insn (gen_movsi_to_cr_one
4804 (gen_rtx_REG (CCmode, CR0_REGNO + i), reg));
4806 if (!exit_func && flag_shrink_wrap)
4808 add_reg_note (insn, REG_CFA_RESTORE,
4809 gen_rtx_REG (SImode, CR0_REGNO + i));
4811 RTX_FRAME_RELATED_P (insn) = 1;
4814 cr_off += reg_size;
4818 /* Get the old lr if we saved it. If we are restoring registers
4819 out-of-line, then the out-of-line routines can do this for us. */
4820 if (restore_lr && restoring_GPRs_inline)
4821 load_lr_save (0, frame_reg_rtx, info->lr_save_offset + frame_off);
4823 /* Get the old cr if we saved it. */
4824 if (info->cr_save_p)
4826 unsigned cr_save_regno = 12;
4828 if (!restoring_GPRs_inline)
4830 /* Ensure we don't use the register used by the out-of-line
4831 gpr register restore below. */
4832 bool lr = info->gp_save_offset + info->gp_size == 0;
4833 int sel = SAVRES_GPR | (lr ? SAVRES_LR : 0);
4834 int gpr_ptr_regno = ptr_regno_for_savres (sel);
4836 if (gpr_ptr_regno == 12)
4837 cr_save_regno = 11;
4838 gcc_checking_assert (REGNO (frame_reg_rtx) != cr_save_regno);
4840 else if (REGNO (frame_reg_rtx) == 12)
4841 cr_save_regno = 11;
4843 /* For ELFv2 r12 is already in use as the GEP. */
4844 if (DEFAULT_ABI == ABI_ELFv2)
4845 cr_save_regno = 11;
4847 cr_save_reg = load_cr_save (cr_save_regno, frame_reg_rtx,
4848 info->cr_save_offset + frame_off,
4849 exit_func);
4852 /* Set LR here to try to overlap restores below. */
4853 if (restore_lr && restoring_GPRs_inline)
4854 restore_saved_lr (0, exit_func);
4856 /* Load exception handler data registers, if needed. */
4857 if (epilogue_type == EPILOGUE_TYPE_EH_RETURN)
4859 unsigned int i, regno;
4861 if (TARGET_AIX)
4863 rtx reg = gen_rtx_REG (reg_mode, 2);
4864 emit_insn (gen_frame_load (reg, frame_reg_rtx,
4865 frame_off + RS6000_TOC_SAVE_SLOT));
4868 for (i = 0; ; ++i)
4870 rtx mem;
4872 regno = EH_RETURN_DATA_REGNO (i);
4873 if (regno == INVALID_REGNUM)
4874 break;
4876 mem = gen_frame_mem_offset (reg_mode, frame_reg_rtx,
4877 info->ehrd_offset + frame_off
4878 + reg_size * (int) i);
4880 emit_move_insn (gen_rtx_REG (reg_mode, regno), mem);
4884 /* Restore GPRs. This is done as a PARALLEL if we are using
4885 the load-multiple instructions. */
4886 if (!restoring_GPRs_inline)
4888 /* We are jumping to an out-of-line function. */
4889 rtx ptr_reg;
4890 int end_save = info->gp_save_offset + info->gp_size;
4891 bool can_use_exit = end_save == 0;
4892 int sel = SAVRES_GPR | (can_use_exit ? SAVRES_LR : 0);
4893 int ptr_off;
4895 /* Emit stack reset code if we need it. */
4896 ptr_regno = ptr_regno_for_savres (sel);
4897 ptr_reg = gen_rtx_REG (Pmode, ptr_regno);
4898 if (can_use_exit)
4899 rs6000_emit_stack_reset (frame_reg_rtx, frame_off, ptr_regno);
4900 else if (end_save + frame_off != 0)
4901 emit_insn (gen_add3_insn (ptr_reg, frame_reg_rtx,
4902 GEN_INT (end_save + frame_off)));
4903 else if (REGNO (frame_reg_rtx) != ptr_regno)
4904 emit_move_insn (ptr_reg, frame_reg_rtx);
4905 if (REGNO (frame_reg_rtx) == ptr_regno)
4906 frame_off = -end_save;
4908 if (can_use_exit && info->cr_save_p)
4909 restore_saved_cr (cr_save_reg, using_mtcr_multiple, true);
4911 ptr_off = -end_save;
4912 rs6000_emit_savres_rtx (info, ptr_reg,
4913 info->gp_save_offset + ptr_off,
4914 info->lr_save_offset + ptr_off,
4915 reg_mode, sel);
4917 else if (using_load_multiple)
4919 rtvec p;
4920 p = rtvec_alloc (32 - info->first_gp_reg_save);
4921 for (i = 0; i < 32 - info->first_gp_reg_save; i++)
4922 RTVEC_ELT (p, i)
4923 = gen_frame_load (gen_rtx_REG (reg_mode, info->first_gp_reg_save + i),
4924 frame_reg_rtx,
4925 info->gp_save_offset + frame_off + reg_size * i);
4926 emit_insn (gen_rtx_PARALLEL (VOIDmode, p));
4928 else
4930 int offset = info->gp_save_offset + frame_off;
4931 for (i = info->first_gp_reg_save; i < 32; i++)
4933 if (save_reg_p (i)
4934 && !cfun->machine->gpr_is_wrapped_separately[i])
4936 rtx reg = gen_rtx_REG (reg_mode, i);
4937 emit_insn (gen_frame_load (reg, frame_reg_rtx, offset));
4940 offset += reg_size;
4944 if (DEFAULT_ABI == ABI_V4 || flag_shrink_wrap)
4946 /* If the frame pointer was used then we can't delay emitting
4947 a REG_CFA_DEF_CFA note. This must happen on the insn that
4948 restores the frame pointer, r31. We may have already emitted
4949 a REG_CFA_DEF_CFA note, but that's OK; A duplicate is
4950 discarded by dwarf2cfi.cc/dwarf2out.cc, and in any case would
4951 be harmless if emitted. */
4952 if (frame_pointer_needed_indeed)
4954 insn = get_last_insn ();
4955 add_reg_note (insn, REG_CFA_DEF_CFA,
4956 plus_constant (Pmode, frame_reg_rtx, frame_off));
4957 RTX_FRAME_RELATED_P (insn) = 1;
4960 /* Set up cfa_restores. We always need these when
4961 shrink-wrapping. If not shrink-wrapping then we only need
4962 the cfa_restore when the stack location is no longer valid.
4963 The cfa_restores must be emitted on or before the insn that
4964 invalidates the stack, and of course must not be emitted
4965 before the insn that actually does the restore. The latter
4966 is why it is a bad idea to emit the cfa_restores as a group
4967 on the last instruction here that actually does a restore:
4968 That insn may be reordered with respect to others doing
4969 restores. */
4970 if (flag_shrink_wrap
4971 && !restoring_GPRs_inline
4972 && info->first_fp_reg_save == 64)
4973 cfa_restores = add_crlr_cfa_restore (info, cfa_restores);
4975 for (i = info->first_gp_reg_save; i < 32; i++)
4976 if (save_reg_p (i)
4977 && !cfun->machine->gpr_is_wrapped_separately[i])
4979 rtx reg = gen_rtx_REG (reg_mode, i);
4980 cfa_restores = alloc_reg_note (REG_CFA_RESTORE, reg, cfa_restores);
4984 if (!restoring_GPRs_inline
4985 && info->first_fp_reg_save == 64)
4987 /* We are jumping to an out-of-line function. */
4988 if (cfa_restores)
4989 emit_cfa_restores (cfa_restores);
4990 return;
4993 if (restore_lr && !restoring_GPRs_inline)
4995 load_lr_save (0, frame_reg_rtx, info->lr_save_offset + frame_off);
4996 restore_saved_lr (0, exit_func);
4999 /* Restore fpr's if we need to do it without calling a function. */
5000 if (restoring_FPRs_inline)
5002 int offset = info->fp_save_offset + frame_off;
5003 for (i = info->first_fp_reg_save; i < 64; i++)
5005 if (save_reg_p (i)
5006 && !cfun->machine->fpr_is_wrapped_separately[i - 32])
5008 rtx reg = gen_rtx_REG (fp_reg_mode, i);
5009 emit_insn (gen_frame_load (reg, frame_reg_rtx, offset));
5010 if (DEFAULT_ABI == ABI_V4 || flag_shrink_wrap)
5011 cfa_restores = alloc_reg_note (REG_CFA_RESTORE, reg,
5012 cfa_restores);
5015 offset += fp_reg_size;
5019 /* If we saved cr, restore it here. Just those that were used. */
5020 if (info->cr_save_p)
5021 restore_saved_cr (cr_save_reg, using_mtcr_multiple, exit_func);
5023 /* If this is V.4, unwind the stack pointer after all of the loads
5024 have been done, or set up r11 if we are restoring fp out of line. */
5025 ptr_regno = 1;
5026 if (!restoring_FPRs_inline)
5028 bool lr = (strategy & REST_NOINLINE_FPRS_DOESNT_RESTORE_LR) == 0;
5029 int sel = SAVRES_FPR | (lr ? SAVRES_LR : 0);
5030 ptr_regno = ptr_regno_for_savres (sel);
5033 insn = rs6000_emit_stack_reset (frame_reg_rtx, frame_off, ptr_regno);
5034 if (REGNO (frame_reg_rtx) == ptr_regno)
5035 frame_off = 0;
5037 if (insn && restoring_FPRs_inline)
5039 if (cfa_restores)
5041 REG_NOTES (insn) = cfa_restores;
5042 cfa_restores = NULL_RTX;
5044 add_reg_note (insn, REG_CFA_DEF_CFA, sp_reg_rtx);
5045 RTX_FRAME_RELATED_P (insn) = 1;
5048 if (epilogue_type == EPILOGUE_TYPE_EH_RETURN)
5050 rtx sa = EH_RETURN_STACKADJ_RTX;
5051 emit_insn (gen_add3_insn (sp_reg_rtx, sp_reg_rtx, sa));
5054 /* The ROP hash check must occur after the stack pointer is restored
5055 (since the hash involves r1), and is not performed for a sibcall. */
5056 if (TARGET_POWER10
5057 && rs6000_rop_protect
5058 && info->rop_hash_size != 0
5059 && epilogue_type != EPILOGUE_TYPE_SIBCALL)
5061 gcc_assert (DEFAULT_ABI == ABI_ELFv2);
5062 rtx stack_ptr = gen_rtx_REG (Pmode, STACK_POINTER_REGNUM);
5063 rtx addr = gen_rtx_PLUS (Pmode, stack_ptr,
5064 GEN_INT (info->rop_hash_save_offset));
5065 rtx mem = gen_rtx_MEM (Pmode, addr);
5066 rtx reg0 = gen_rtx_REG (Pmode, 0);
5067 emit_insn (gen_hashchk (reg0, mem));
5070 if (epilogue_type != EPILOGUE_TYPE_SIBCALL && restoring_FPRs_inline)
5072 if (cfa_restores)
5074 /* We can't hang the cfa_restores off a simple return,
5075 since the shrink-wrap code sometimes uses an existing
5076 return. This means there might be a path from
5077 pre-prologue code to this return, and dwarf2cfi code
5078 wants the eh_frame unwinder state to be the same on
5079 all paths to any point. So we need to emit the
5080 cfa_restores before the return. For -m64 we really
5081 don't need epilogue cfa_restores at all, except for
5082 this irritating dwarf2cfi with shrink-wrap
5083 requirement; The stack red-zone means eh_frame info
5084 from the prologue telling the unwinder to restore
5085 from the stack is perfectly good right to the end of
5086 the function. */
5087 emit_insn (gen_blockage ());
5088 emit_cfa_restores (cfa_restores);
5089 cfa_restores = NULL_RTX;
5092 emit_jump_insn (targetm.gen_simple_return ());
5095 if (epilogue_type != EPILOGUE_TYPE_SIBCALL && !restoring_FPRs_inline)
5097 bool lr = (strategy & REST_NOINLINE_FPRS_DOESNT_RESTORE_LR) == 0;
5098 rtvec p = rtvec_alloc (3 + !!lr + 64 - info->first_fp_reg_save);
5099 int elt = 0;
5100 RTVEC_ELT (p, elt++) = ret_rtx;
5101 if (lr)
5102 RTVEC_ELT (p, elt++) = gen_hard_reg_clobber (Pmode, LR_REGNO);
5104 /* We have to restore more than two FP registers, so branch to the
5105 restore function. It will return to our caller. */
5106 int i;
5107 int reg;
5108 rtx sym;
5110 if (flag_shrink_wrap)
5111 cfa_restores = add_crlr_cfa_restore (info, cfa_restores);
5113 sym = rs6000_savres_routine_sym (info, SAVRES_FPR | (lr ? SAVRES_LR : 0));
5114 RTVEC_ELT (p, elt++) = gen_rtx_USE (VOIDmode, sym);
5115 reg = (DEFAULT_ABI == ABI_AIX || DEFAULT_ABI == ABI_ELFv2)? 1 : 11;
5116 RTVEC_ELT (p, elt++) = gen_rtx_USE (VOIDmode, gen_rtx_REG (Pmode, reg));
5118 for (i = 0; i < 64 - info->first_fp_reg_save; i++)
5120 rtx reg = gen_rtx_REG (DFmode, info->first_fp_reg_save + i);
5122 RTVEC_ELT (p, elt++)
5123 = gen_frame_load (reg, sp_reg_rtx, info->fp_save_offset + 8 * i);
5124 if (flag_shrink_wrap
5125 && save_reg_p (info->first_fp_reg_save + i))
5126 cfa_restores = alloc_reg_note (REG_CFA_RESTORE, reg, cfa_restores);
5129 emit_jump_insn (gen_rtx_PARALLEL (VOIDmode, p));
5132 if (cfa_restores)
5134 if (epilogue_type == EPILOGUE_TYPE_SIBCALL)
5135 /* Ensure the cfa_restores are hung off an insn that won't
5136 be reordered above other restores. */
5137 emit_insn (gen_blockage ());
5139 emit_cfa_restores (cfa_restores);
5143 #if TARGET_MACHO
5145 /* Generate far-jump branch islands for everything recorded in
5146 branch_islands. Invoked immediately after the last instruction of
5147 the epilogue has been emitted; the branch islands must be appended
5148 to, and contiguous with, the function body. Mach-O stubs are
5149 generated in machopic_output_stub(). */
5151 static void
5152 macho_branch_islands (void)
5154 char tmp_buf[512];
5156 while (!vec_safe_is_empty (branch_islands))
5158 branch_island *bi = &branch_islands->last ();
5159 const char *label = IDENTIFIER_POINTER (bi->label_name);
5160 const char *name = IDENTIFIER_POINTER (bi->function_name);
5161 char name_buf[512];
5162 /* Cheap copy of the details from the Darwin ASM_OUTPUT_LABELREF(). */
5163 if (name[0] == '*' || name[0] == '&')
5164 strcpy (name_buf, name+1);
5165 else
5167 name_buf[0] = '_';
5168 strcpy (name_buf+1, name);
5170 strcpy (tmp_buf, "\n");
5171 strcat (tmp_buf, label);
5172 if (flag_pic)
5174 strcat (tmp_buf, ":\n\tmflr r0\n\tbcl 20,31,");
5175 strcat (tmp_buf, label);
5176 strcat (tmp_buf, "_pic\n");
5177 strcat (tmp_buf, label);
5178 strcat (tmp_buf, "_pic:\n\tmflr r11\n");
5180 strcat (tmp_buf, "\taddis r11,r11,ha16(");
5181 strcat (tmp_buf, name_buf);
5182 strcat (tmp_buf, " - ");
5183 strcat (tmp_buf, label);
5184 strcat (tmp_buf, "_pic)\n");
5186 strcat (tmp_buf, "\tmtlr r0\n");
5188 strcat (tmp_buf, "\taddi r12,r11,lo16(");
5189 strcat (tmp_buf, name_buf);
5190 strcat (tmp_buf, " - ");
5191 strcat (tmp_buf, label);
5192 strcat (tmp_buf, "_pic)\n");
5194 strcat (tmp_buf, "\tmtctr r12\n\tbctr\n");
5196 else
5198 strcat (tmp_buf, ":\n\tlis r12,hi16(");
5199 strcat (tmp_buf, name_buf);
5200 strcat (tmp_buf, ")\n\tori r12,r12,lo16(");
5201 strcat (tmp_buf, name_buf);
5202 strcat (tmp_buf, ")\n\tmtctr r12\n\tbctr");
5204 output_asm_insn (tmp_buf, 0);
5205 branch_islands->pop ();
5208 #endif
5210 /* Write function epilogue. */
5212 void
5213 rs6000_output_function_epilogue (FILE *file)
5215 #if TARGET_MACHO
5216 macho_branch_islands ();
5219 rtx_insn *insn = get_last_insn ();
5220 rtx_insn *deleted_debug_label = NULL;
5222 /* Mach-O doesn't support labels at the end of objects, so if
5223 it looks like we might want one, take special action.
5225 First, collect any sequence of deleted debug labels. */
5226 while (insn
5227 && NOTE_P (insn)
5228 && NOTE_KIND (insn) != NOTE_INSN_DELETED_LABEL)
5230 /* Don't insert a nop for NOTE_INSN_DELETED_DEBUG_LABEL
5231 notes only, instead set their CODE_LABEL_NUMBER to -1,
5232 otherwise there would be code generation differences
5233 in between -g and -g0. */
5234 if (NOTE_P (insn) && NOTE_KIND (insn) == NOTE_INSN_DELETED_DEBUG_LABEL)
5235 deleted_debug_label = insn;
5236 insn = PREV_INSN (insn);
5239 /* Second, if we have:
5240 label:
5241 barrier
5242 then this needs to be detected, so skip past the barrier. */
5244 if (insn && BARRIER_P (insn))
5245 insn = PREV_INSN (insn);
5247 /* Up to now we've only seen notes or barriers. */
5248 if (insn)
5250 if (LABEL_P (insn)
5251 || (NOTE_P (insn)
5252 && NOTE_KIND (insn) == NOTE_INSN_DELETED_LABEL))
5253 /* Trailing label: <barrier>. */
5254 fputs ("\tnop\n", file);
5255 else
5257 /* Lastly, see if we have a completely empty function body. */
5258 while (insn && ! INSN_P (insn))
5259 insn = PREV_INSN (insn);
5260 /* If we don't find any insns, we've got an empty function body;
5261 I.e. completely empty - without a return or branch. This is
5262 taken as the case where a function body has been removed
5263 because it contains an inline __builtin_unreachable(). GCC
5264 states that reaching __builtin_unreachable() means UB so we're
5265 not obliged to do anything special; however, we want
5266 non-zero-sized function bodies. To meet this, and help the
5267 user out, let's trap the case. */
5268 if (insn == NULL)
5269 fputs ("\ttrap\n", file);
5272 else if (deleted_debug_label)
5273 for (insn = deleted_debug_label; insn; insn = NEXT_INSN (insn))
5274 if (NOTE_KIND (insn) == NOTE_INSN_DELETED_DEBUG_LABEL)
5275 CODE_LABEL_NUMBER (insn) = -1;
5277 #endif
5279 /* Output a traceback table here. See /usr/include/sys/debug.h for info
5280 on its format.
5282 We don't output a traceback table if -finhibit-size-directive was
5283 used. The documentation for -finhibit-size-directive reads
5284 ``don't output a @code{.size} assembler directive, or anything
5285 else that would cause trouble if the function is split in the
5286 middle, and the two halves are placed at locations far apart in
5287 memory.'' The traceback table has this property, since it
5288 includes the offset from the start of the function to the
5289 traceback table itself.
5291 System V.4 Powerpc's (and the embedded ABI derived from it) use a
5292 different traceback table. */
5293 if ((DEFAULT_ABI == ABI_AIX || DEFAULT_ABI == ABI_ELFv2)
5294 && ! flag_inhibit_size_directive
5295 && rs6000_traceback != traceback_none && !cfun->is_thunk)
5297 const char *fname = NULL;
5298 const char *language_string = lang_hooks.name;
5299 int fixed_parms = 0, float_parms = 0, parm_info = 0;
5300 int i;
5301 int optional_tbtab;
5302 rs6000_stack_t *info = rs6000_stack_info ();
5304 if (rs6000_traceback == traceback_full)
5305 optional_tbtab = 1;
5306 else if (rs6000_traceback == traceback_part)
5307 optional_tbtab = 0;
5308 else
5309 optional_tbtab = !optimize_size && !TARGET_ELF;
5311 if (optional_tbtab)
5313 fname = XSTR (XEXP (DECL_RTL (current_function_decl), 0), 0);
5314 while (*fname == '.') /* V.4 encodes . in the name */
5315 fname++;
5317 /* Need label immediately before tbtab, so we can compute
5318 its offset from the function start. */
5319 ASM_OUTPUT_INTERNAL_LABEL_PREFIX (file, "LT");
5320 ASM_OUTPUT_LABEL (file, fname);
5323 /* The .tbtab pseudo-op can only be used for the first eight
5324 expressions, since it can't handle the possibly variable
5325 length fields that follow. However, if you omit the optional
5326 fields, the assembler outputs zeros for all optional fields
5327 anyways, giving each variable length field is minimum length
5328 (as defined in sys/debug.h). Thus we cannot use the .tbtab
5329 pseudo-op at all. */
5331 /* An all-zero word flags the start of the tbtab, for debuggers
5332 that have to find it by searching forward from the entry
5333 point or from the current pc. */
5334 fputs ("\t.long 0\n", file);
5336 /* Tbtab format type. Use format type 0. */
5337 fputs ("\t.byte 0,", file);
5339 /* Language type. Unfortunately, there does not seem to be any
5340 official way to discover the language being compiled, so we
5341 use language_string.
5342 C is 0. Fortran is 1. Ada is 3. Modula-2 is 8. C++ is 9.
5343 Java is 13. Objective-C is 14. Objective-C++ isn't assigned
5344 a number, so for now use 9. LTO, Go, D, and JIT aren't assigned
5345 numbers either, so for now use 0. */
5346 if (lang_GNU_C ()
5347 || ! strcmp (language_string, "GNU GIMPLE")
5348 || ! strcmp (language_string, "GNU Go")
5349 || ! strcmp (language_string, "GNU D")
5350 || ! strcmp (language_string, "GNU Rust")
5351 || ! strcmp (language_string, "libgccjit"))
5352 i = 0;
5353 else if (! strcmp (language_string, "GNU F77")
5354 || lang_GNU_Fortran ())
5355 i = 1;
5356 else if (! strcmp (language_string, "GNU Ada"))
5357 i = 3;
5358 else if (! strcmp (language_string, "GNU Modula-2"))
5359 i = 8;
5360 else if (lang_GNU_CXX ()
5361 || ! strcmp (language_string, "GNU Objective-C++"))
5362 i = 9;
5363 else if (! strcmp (language_string, "GNU Java"))
5364 i = 13;
5365 else if (! strcmp (language_string, "GNU Objective-C"))
5366 i = 14;
5367 else
5368 gcc_unreachable ();
5369 fprintf (file, "%d,", i);
5371 /* 8 single bit fields: global linkage (not set for C extern linkage,
5372 apparently a PL/I convention?), out-of-line epilogue/prologue, offset
5373 from start of procedure stored in tbtab, internal function, function
5374 has controlled storage, function has no toc, function uses fp,
5375 function logs/aborts fp operations. */
5376 /* Assume that fp operations are used if any fp reg must be saved. */
5377 fprintf (file, "%d,",
5378 (optional_tbtab << 5) | ((info->first_fp_reg_save != 64) << 1));
5380 /* 6 bitfields: function is interrupt handler, name present in
5381 proc table, function calls alloca, on condition directives
5382 (controls stack walks, 3 bits), saves condition reg, saves
5383 link reg. */
5384 /* The `function calls alloca' bit seems to be set whenever reg 31 is
5385 set up as a frame pointer, even when there is no alloca call. */
5386 fprintf (file, "%d,",
5387 ((optional_tbtab << 6)
5388 | ((optional_tbtab & frame_pointer_needed) << 5)
5389 | (info->cr_save_p << 1)
5390 | (info->lr_save_p)));
5392 /* 3 bitfields: saves backchain, fixup code, number of fpr saved
5393 (6 bits). */
5394 fprintf (file, "%d,",
5395 (info->push_p << 7) | (64 - info->first_fp_reg_save));
5397 /* 2 bitfields: spare bits (2 bits), number of gpr saved (6 bits). */
5398 fprintf (file, "%d,", (32 - first_reg_to_save ()));
5400 if (optional_tbtab)
5402 /* Compute the parameter info from the function decl argument
5403 list. */
5404 tree decl;
5405 int next_parm_info_bit = 31;
5407 for (decl = DECL_ARGUMENTS (current_function_decl);
5408 decl; decl = DECL_CHAIN (decl))
5410 rtx parameter = DECL_INCOMING_RTL (decl);
5411 machine_mode mode = GET_MODE (parameter);
5413 if (REG_P (parameter))
5415 if (SCALAR_FLOAT_MODE_P (mode))
5417 int bits;
5419 float_parms++;
5421 switch (mode)
5423 case E_SFmode:
5424 case E_SDmode:
5425 bits = 0x2;
5426 break;
5428 case E_DFmode:
5429 case E_DDmode:
5430 case E_TFmode:
5431 case E_TDmode:
5432 case E_IFmode:
5433 case E_KFmode:
5434 bits = 0x3;
5435 break;
5437 default:
5438 gcc_unreachable ();
5441 /* If only one bit will fit, don't or in this entry. */
5442 if (next_parm_info_bit > 0)
5443 parm_info |= (bits << (next_parm_info_bit - 1));
5444 next_parm_info_bit -= 2;
5446 else
5448 fixed_parms += ((GET_MODE_SIZE (mode)
5449 + (UNITS_PER_WORD - 1))
5450 / UNITS_PER_WORD);
5451 next_parm_info_bit -= 1;
5457 /* Number of fixed point parameters. */
5458 /* This is actually the number of words of fixed point parameters; thus
5459 an 8 byte struct counts as 2; and thus the maximum value is 8. */
5460 fprintf (file, "%d,", fixed_parms);
5462 /* 2 bitfields: number of floating point parameters (7 bits), parameters
5463 all on stack. */
5464 /* This is actually the number of fp registers that hold parameters;
5465 and thus the maximum value is 13. */
5466 /* Set parameters on stack bit if parameters are not in their original
5467 registers, regardless of whether they are on the stack? Xlc
5468 seems to set the bit when not optimizing. */
5469 fprintf (file, "%d\n", ((float_parms << 1) | (! optimize)));
5471 if (optional_tbtab)
5473 /* Optional fields follow. Some are variable length. */
5475 /* Parameter types, left adjusted bit fields: 0 fixed, 10 single
5476 float, 11 double float. */
5477 /* There is an entry for each parameter in a register, in the order
5478 that they occur in the parameter list. Any intervening arguments
5479 on the stack are ignored. If the list overflows a long (max
5480 possible length 34 bits) then completely leave off all elements
5481 that don't fit. */
5482 /* Only emit this long if there was at least one parameter. */
5483 if (fixed_parms || float_parms)
5484 fprintf (file, "\t.long %d\n", parm_info);
5486 /* Offset from start of code to tb table. */
5487 fputs ("\t.long ", file);
5488 ASM_OUTPUT_INTERNAL_LABEL_PREFIX (file, "LT");
5489 RS6000_OUTPUT_BASENAME (file, fname);
5490 putc ('-', file);
5491 rs6000_output_function_entry (file, fname);
5492 putc ('\n', file);
5494 /* Interrupt handler mask. */
5495 /* Omit this long, since we never set the interrupt handler bit
5496 above. */
5498 /* Number of CTL (controlled storage) anchors. */
5499 /* Omit this long, since the has_ctl bit is never set above. */
5501 /* Displacement into stack of each CTL anchor. */
5502 /* Omit this list of longs, because there are no CTL anchors. */
5504 /* Length of function name. */
5505 if (*fname == '*')
5506 ++fname;
5507 fprintf (file, "\t.short %d\n", (int) strlen (fname));
5509 /* Function name. */
5510 assemble_string (fname, strlen (fname));
5512 /* Register for alloca automatic storage; this is always reg 31.
5513 Only emit this if the alloca bit was set above. */
5514 if (frame_pointer_needed)
5515 fputs ("\t.byte 31\n", file);
5517 fputs ("\t.align 2\n", file);
5521 /* Arrange to define .LCTOC1 label, if not already done. */
5522 if (need_toc_init)
5524 need_toc_init = 0;
5525 if (!toc_initialized)
5527 switch_to_section (toc_section);
5528 switch_to_section (current_function_section ());
5533 /* -fsplit-stack support. */
5535 /* A SYMBOL_REF for __morestack. */
5536 static GTY(()) rtx morestack_ref;
5538 static rtx
5539 gen_add3_const (rtx rt, rtx ra, long c)
5541 if (TARGET_64BIT)
5542 return gen_adddi3 (rt, ra, GEN_INT (c));
5543 else
5544 return gen_addsi3 (rt, ra, GEN_INT (c));
5547 /* Emit -fsplit-stack prologue, which goes before the regular function
5548 prologue (at local entry point in the case of ELFv2). */
5550 void
5551 rs6000_expand_split_stack_prologue (void)
5553 rs6000_stack_t *info = rs6000_stack_info ();
5554 unsigned HOST_WIDE_INT allocate;
5555 long alloc_hi, alloc_lo;
5556 rtx r0, r1, r12, lr, ok_label, compare, jump, call_fusage;
5557 rtx_insn *insn;
5559 gcc_assert (flag_split_stack && reload_completed);
5561 if (!info->push_p)
5563 /* We need the -fsplit-stack prologue for functions that make
5564 tail calls. Tail calls don't count against crtl->is_leaf.
5565 Note that we are called inside a sequence. get_insns will
5566 just return that (as yet empty) sequence, so instead we
5567 access the function rtl with get_topmost_sequence. */
5568 for (insn = get_topmost_sequence ()->first; insn; insn = NEXT_INSN (insn))
5569 if (CALL_P (insn))
5570 break;
5571 if (!insn)
5572 return;
5575 if (global_regs[29])
5577 error ("%qs uses register r29", "%<-fsplit-stack%>");
5578 inform (DECL_SOURCE_LOCATION (global_regs_decl[29]),
5579 "conflicts with %qD", global_regs_decl[29]);
5582 allocate = info->total_size;
5583 if (allocate > (unsigned HOST_WIDE_INT) 1 << 31)
5585 sorry ("Stack frame larger than 2G is not supported for "
5586 "%<-fsplit-stack%>");
5587 return;
5589 if (morestack_ref == NULL_RTX)
5591 morestack_ref = gen_rtx_SYMBOL_REF (Pmode, "__morestack");
5592 SYMBOL_REF_FLAGS (morestack_ref) |= (SYMBOL_FLAG_LOCAL
5593 | SYMBOL_FLAG_FUNCTION);
5596 r0 = gen_rtx_REG (Pmode, 0);
5597 r1 = gen_rtx_REG (Pmode, STACK_POINTER_REGNUM);
5598 r12 = gen_rtx_REG (Pmode, 12);
5599 emit_insn (gen_load_split_stack_limit (r0));
5600 /* Always emit two insns here to calculate the requested stack,
5601 so that the linker can edit them when adjusting size for calling
5602 non-split-stack code. */
5603 alloc_hi = (-allocate + 0x8000) & ~0xffffL;
5604 alloc_lo = -allocate - alloc_hi;
5605 if (alloc_hi != 0)
5607 emit_insn (gen_add3_const (r12, r1, alloc_hi));
5608 if (alloc_lo != 0)
5609 emit_insn (gen_add3_const (r12, r12, alloc_lo));
5610 else
5611 emit_insn (gen_nop ());
5613 else
5615 emit_insn (gen_add3_const (r12, r1, alloc_lo));
5616 emit_insn (gen_nop ());
5619 compare = gen_rtx_REG (CCUNSmode, CR7_REGNO);
5620 emit_insn (gen_rtx_SET (compare, gen_rtx_COMPARE (CCUNSmode, r12, r0)));
5621 ok_label = gen_label_rtx ();
5622 jump = gen_rtx_IF_THEN_ELSE (VOIDmode,
5623 gen_rtx_GEU (VOIDmode, compare, const0_rtx),
5624 gen_rtx_LABEL_REF (VOIDmode, ok_label),
5625 pc_rtx);
5626 insn = emit_jump_insn (gen_rtx_SET (pc_rtx, jump));
5627 JUMP_LABEL (insn) = ok_label;
5628 /* Mark the jump as very likely to be taken. */
5629 add_reg_br_prob_note (insn, profile_probability::very_likely ());
5631 lr = gen_rtx_REG (Pmode, LR_REGNO);
5632 insn = emit_move_insn (r0, lr);
5633 RTX_FRAME_RELATED_P (insn) = 1;
5634 insn = emit_insn (gen_frame_store (r0, r1, info->lr_save_offset));
5635 RTX_FRAME_RELATED_P (insn) = 1;
5637 insn = emit_call_insn (gen_call (gen_rtx_MEM (SImode, morestack_ref),
5638 const0_rtx, const0_rtx));
5639 call_fusage = NULL_RTX;
5640 use_reg (&call_fusage, r12);
5641 /* Say the call uses r0, even though it doesn't, to stop regrename
5642 from twiddling with the insns saving lr, trashing args for cfun.
5643 The insns restoring lr are similarly protected by making
5644 split_stack_return use r0. */
5645 use_reg (&call_fusage, r0);
5646 add_function_usage_to (insn, call_fusage);
5647 /* Indicate that this function can't jump to non-local gotos. */
5648 make_reg_eh_region_note_nothrow_nononlocal (insn);
5649 emit_insn (gen_frame_load (r0, r1, info->lr_save_offset));
5650 insn = emit_move_insn (lr, r0);
5651 add_reg_note (insn, REG_CFA_RESTORE, lr);
5652 RTX_FRAME_RELATED_P (insn) = 1;
5653 emit_insn (gen_split_stack_return ());
5655 emit_label (ok_label);
5656 LABEL_NUSES (ok_label) = 1;
5659 /* We may have to tell the dataflow pass that the split stack prologue
5660 is initializing a register. */
5662 void
5663 rs6000_live_on_entry (bitmap regs)
5665 if (flag_split_stack)
5666 bitmap_set_bit (regs, 12);
5669 /* Emit -fsplit-stack dynamic stack allocation space check. */
5671 void
5672 rs6000_split_stack_space_check (rtx size, rtx label)
5674 rtx sp = gen_rtx_REG (Pmode, STACK_POINTER_REGNUM);
5675 rtx limit = gen_reg_rtx (Pmode);
5676 rtx requested = gen_reg_rtx (Pmode);
5677 rtx cmp = gen_reg_rtx (CCUNSmode);
5678 rtx jump;
5680 emit_insn (gen_load_split_stack_limit (limit));
5681 if (CONST_INT_P (size))
5682 emit_insn (gen_add3_insn (requested, sp, GEN_INT (-INTVAL (size))));
5683 else
5685 size = force_reg (Pmode, size);
5686 emit_move_insn (requested, gen_rtx_MINUS (Pmode, sp, size));
5688 emit_insn (gen_rtx_SET (cmp, gen_rtx_COMPARE (CCUNSmode, requested, limit)));
5689 jump = gen_rtx_IF_THEN_ELSE (VOIDmode,
5690 gen_rtx_GEU (VOIDmode, cmp, const0_rtx),
5691 gen_rtx_LABEL_REF (VOIDmode, label),
5692 pc_rtx);
5693 jump = emit_jump_insn (gen_rtx_SET (pc_rtx, jump));
5694 JUMP_LABEL (jump) = label;
5698 /* Return whether we need to always update the saved TOC pointer when we update
5699 the stack pointer. */
5701 static bool
5702 rs6000_save_toc_in_prologue_p (void)
5704 return (cfun && cfun->machine && cfun->machine->save_toc_in_prologue);
5707 #include "gt-rs6000-logue.h"