Debian Codegraph @

gcc/config/rs6000/rs6000-logue.cc

raw
1 /* Subroutines used to generate function prologues and epilogues
2 on IBM RS/6000.
3 Copyright (C) 1991-2025 Free Software Foundation, Inc.
4
5 This file is part of GCC.
6
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.
11
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.
16
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/>. */
20
21 #define IN_TARGET_CODE 1
22
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"
50
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);
54
55 static rs6000_stack_t stack_info;
56
57 /* Set if HARD_FRAM_POINTER_REGNUM is really needed. */
58 static bool frame_pointer_needed_indeed = false;
59
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;
63
64
65 #ifndef TARGET_PROFILE_KERNEL
66 #define TARGET_PROFILE_KERNEL 0
67 #endif
68
69
70 /* Function to init struct machine_function.
71 This will be called, via a pointer variable,
72 from push_function_context. */
73
74 struct machine_function *
75 rs6000_init_machine_status (void)
76 {
77 stack_info.reload_completed = 0;
78 return ggc_cleared_alloc<machine_function> ();
79 }
80
81 /* This page contains routines that are used to determine what the
82 function prologue and epilogue code will do and write them out. */
83
84 /* Determine whether the REG is really used. */
85
86 bool
87 save_reg_p (int reg)
88 {
89 if (reg == RS6000_PIC_OFFSET_TABLE_REGNUM && !TARGET_SINGLE_PIC_BASE)
90 {
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;
99
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;
106
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;
111
112 if (DEFAULT_ABI == ABI_DARWIN
113 && flag_pic && crtl->uses_pic_offset_table)
114 return true;
115 }
116
117 return !call_used_or_fixed_reg_p (reg) && df_regs_ever_live_p (reg);
118 }
119
120 /* Return the first fixed-point register that is required to be
121 saved. 32 if none. */
122
123 int
124 first_reg_to_save (void)
125 {
126 int first_reg;
127
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;
132
133 return first_reg;
134 }
135
136 /* Similar, for FP regs. */
137
138 int
139 first_fp_reg_to_save (void)
140 {
141 int first_reg;
142
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;
147
148 return first_reg;
149 }
150
151 /* Similar, for AltiVec regs. */
152
153 static int
154 first_altivec_reg_to_save (void)
155 {
156 int i;
157
158 /* Stack frame remains as is unless we are in AltiVec ABI. */
159 if (! TARGET_ALTIVEC_ABI)
160 return LAST_ALTIVEC_REGNO + 1;
161
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;
168
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;
173
174 return i;
175 }
176
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. */
180
181 static unsigned int
182 compute_vrsave_mask (void)
183 {
184 unsigned int i, mask = 0;
185
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;
192
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);
197
198 if (mask == 0)
199 return mask;
200
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);
208
209 /* Similarly, remove the return value from the set. */
210 {
211 bool yes = false;
212 diddle_return_value (is_altivec_return_reg, &yes);
213 if (yes)
214 mask &= ~ALTIVEC_REG_BIT (ALTIVEC_ARG_RETURN);
215 }
216
217 return mask;
218 }
219
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. */
223
224 static void
225 compute_save_world_info (rs6000_stack_t *info)
226 {
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);
236
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))
240 {
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))
244 {
245 info->world_save_p = 0;
246 break;
247 }
248 }
249
250 if (WORLD_SAVE_P (info))
251 {
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;
256
257 /* If we are going to save the world, we need to save the link register too. */
258 info->lr_save_p = 1;
259
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 ();
263
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));
270 }
271
272 return;
273 }
274
275
276 static void
277 is_altivec_return_reg (rtx reg, void *xyes)
278 {
279 bool *yes = (bool *) xyes;
280 if (REGNO (reg) == ALTIVEC_ARG_RETURN)
281 *yes = true;
282 }
283
284
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. */
290
291 static bool
292 fixed_reg_p (int reg)
293 {
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;
301
302 return fixed_regs[reg];
303 }
304
305 /* Determine the strategy for savings/restoring registers. */
306
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
319 };
320
321 static int
322 rs6000_savres_strategy (rs6000_stack_t *info,
323 bool using_static_chain_p)
324 {
325 int strategy = 0;
326
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);
334
335 if (info->first_gp_reg_save == 32)
336 strategy |= SAVE_INLINE_GPRS | REST_INLINE_GPRS;
337
338 if (info->first_fp_reg_save == 64)
339 strategy |= SAVE_INLINE_FPRS | REST_INLINE_FPRS;
340
341 if (info->first_altivec_reg_save == LAST_ALTIVEC_REGNO + 1)
342 strategy |= SAVE_INLINE_VRS | REST_INLINE_VRS;
343
344 /* Define cutoff for using out-of-line functions to save registers. */
345 if (DEFAULT_ABI == ABI_V4 || TARGET_ELF)
346 {
347 if (!optimize_size)
348 {
349 strategy |= SAVE_INLINE_FPRS | REST_INLINE_FPRS;
350 strategy |= SAVE_INLINE_GPRS | REST_INLINE_GPRS;
351 strategy |= SAVE_INLINE_VRS | REST_INLINE_VRS;
352 }
353 else
354 {
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)
359 {
360 if (info->first_fp_reg_save == 64)
361 strategy |= SAVE_INLINE_GPRS;
362 else
363 strategy |= SAVE_INLINE_GPRS | REST_INLINE_GPRS;
364 }
365 if (info->first_altivec_reg_save == LAST_ALTIVEC_REGNO)
366 strategy |= SAVE_INLINE_VRS | REST_INLINE_VRS;
367 }
368 }
369 else if (DEFAULT_ABI == ABI_DARWIN)
370 {
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;
376 }
377 else
378 {
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;
385 }
386
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);
396
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])
403 {
404 strategy |= REST_INLINE_FPRS;
405 break;
406 }
407
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))
416 {
417 strategy |= REST_INLINE_FPRS;
418 break;
419 }
420
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])
425 {
426 strategy |= REST_INLINE_VRS;
427 break;
428 }
429
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))
434 {
435 strategy |= REST_INLINE_VRS;
436 break;
437 }
438
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));
447
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)))
454 {
455 int count = 0;
456 for (int i = info->first_gp_reg_save; i < 32; i++)
457 if (save_reg_p (i))
458 count++;
459
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
467 {
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;
472
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;
484 }
485 }
486
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;
492
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))
497 {
498 strategy |= REST_INLINE_GPRS;
499 strategy &= ~REST_MULTIPLE;
500 break;
501 }
502
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))
512 {
513 strategy |= REST_INLINE_GPRS;
514 strategy &= ~REST_MULTIPLE;
515 break;
516 }
517
518 if (TARGET_ELF && TARGET_64BIT)
519 {
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;
525 }
526 else if (TARGET_AIX && !(strategy & REST_INLINE_FPRS))
527 strategy |= REST_NOINLINE_FPRS_DOESNT_RESTORE_LR;
528
529 if (TARGET_MACHO && !(strategy & SAVE_INLINE_FPRS))
530 strategy |= SAVE_NOINLINE_FPRS_SAVES_LR;
531
532 return strategy;
533 }
534
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.
538
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 +---------------------------------------+
574
575 * If the alloca area is present, the parameter save area is
576 padded so that the former starts 16-byte aligned.
577
578 The required alignment for AIX configurations is two words (i.e., 8
579 or 16 bytes).
580
581 The ELFv2 ABI is a variant of the AIX ABI. Stack frames look like:
582
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 | Alloca space (A) | 32+P
595 +---------------------------------------+
596 | Local variable space (L) | 32+P+A
597 +---------------------------------------+
598 | Optional ROP hash slot (R) | 32+P+A+L
599 +---------------------------------------+
600 | Save area for AltiVec registers (W) | 32+P+A+L+R
601 +---------------------------------------+
602 | AltiVec alignment padding (Y) | 32+P+A+L+R+W
603 +---------------------------------------+
604 | Save area for GP registers (G) | 32+P+A+L+R+W+Y
605 +---------------------------------------+
606 | Save area for FP registers (F) | 32+P+A+L+R+W+Y+G
607 +---------------------------------------+
608 old SP->| back chain to caller's caller | 32+P+A+L+R+W+Y+G+F
609 +---------------------------------------+
610
611 * If the alloca area is present, the parameter save area is
612 padded so that the former starts 16-byte aligned.
613
614 V.4 stack frames look like:
615
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 +---------------------------------------+
645
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.
649
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.)
656
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.) */
662
663 #ifndef ABI_STACK_BOUNDARY
664 #define ABI_STACK_BOUNDARY STACK_BOUNDARY
665 #endif
666
667 rs6000_stack_t *
668 rs6000_stack_info (void)
669 {
670 /* We should never be called for thunks, we are not set up for that. */
671 gcc_assert (!cfun->is_thunk);
672
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;
681
682 if (reload_completed && info->reload_completed)
683 return info;
684
685 memset (info, 0, sizeof (*info));
686 info->reload_completed = reload_completed;
687
688 /* Select which calling sequence. */
689 info->abi = DEFAULT_ABI;
690
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;
705
706 info->gp_size = reg_size * (32 - first_gp);
707
708 info->first_fp_reg_save = first_fp_reg_to_save ();
709 info->fp_size = 8 * (64 - info->first_fp_reg_save);
710
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);
714
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;
718
719 /* If we want ROP protection and this function makes a call, indicate
720 we need to create a stack slot to save the hashed return address in. */
721 if (rs6000_rop_protect
722 && info->calls_p)
723 info->rop_hash_size = 8;
724
725 /* Determine if we need to save the condition code registers. */
726 if (save_reg_p (CR2_REGNO)
727 || save_reg_p (CR3_REGNO)
728 || save_reg_p (CR4_REGNO))
729 {
730 info->cr_save_p = 1;
731 if (DEFAULT_ABI == ABI_V4)
732 info->cr_size = reg_size;
733 }
734
735 /* If the current function calls __builtin_eh_return, then we need
736 to allocate stack space for registers that will hold data for
737 the exception handler. */
738 if (crtl->calls_eh_return)
739 {
740 unsigned int i;
741 for (i = 0; EH_RETURN_DATA_REGNO (i) != INVALID_REGNUM; ++i)
742 continue;
743
744 ehrd_size = i * UNITS_PER_WORD;
745 }
746 else
747 ehrd_size = 0;
748
749 /* In the ELFv2 ABI, we also need to allocate space for separate
750 CR field save areas if the function calls __builtin_eh_return. */
751 if (DEFAULT_ABI == ABI_ELFv2 && crtl->calls_eh_return)
752 {
753 /* This hard-codes that we have three call-saved CR fields. */
754 ehcr_size = 3 * reg_size;
755 /* We do *not* use the regular CR save mechanism. */
756 info->cr_save_p = 0;
757 }
758 else
759 ehcr_size = 0;
760
761 /* Determine various sizes. */
762 info->reg_size = reg_size;
763 info->fixed_size = RS6000_SAVE_AREA;
764 info->vars_size = RS6000_ALIGN (get_frame_size (), 8);
765 if (cfun->calls_alloca)
766 info->parm_size =
767 RS6000_ALIGN (crtl->outgoing_args_size + info->fixed_size,
768 STACK_BOUNDARY / BITS_PER_UNIT) - info->fixed_size;
769 else
770 info->parm_size = RS6000_ALIGN (crtl->outgoing_args_size,
771 TARGET_ALTIVEC ? 16 : 8);
772 if (FRAME_GROWS_DOWNWARD)
773 info->vars_size
774 += RS6000_ALIGN (info->fixed_size + info->vars_size + info->parm_size,
775 ABI_STACK_BOUNDARY / BITS_PER_UNIT)
776 - (info->fixed_size + info->vars_size + info->parm_size);
777
778 if (TARGET_ALTIVEC_ABI)
779 info->vrsave_mask = compute_vrsave_mask ();
780
781 if (TARGET_ALTIVEC_VRSAVE && info->vrsave_mask)
782 info->vrsave_size = 4;
783
784 compute_save_world_info (info);
785
786 /* Calculate the offsets. */
787 switch (DEFAULT_ABI)
788 {
789 case ABI_NONE:
790 default:
791 gcc_unreachable ();
792
793 case ABI_AIX:
794 case ABI_ELFv2:
795 case ABI_DARWIN:
796 info->fp_save_offset = -info->fp_size;
797 info->gp_save_offset = info->fp_save_offset - info->gp_size;
798
799 if (TARGET_ALTIVEC_ABI)
800 {
801 info->vrsave_save_offset = info->gp_save_offset - info->vrsave_size;
802
803 /* Align stack so vector save area is on a quadword boundary.
804 The padding goes above the vectors. */
805 if (info->altivec_size != 0)
806 info->altivec_padding_size = info->vrsave_save_offset & 0xF;
807
808 info->altivec_save_offset = info->vrsave_save_offset
809 - info->altivec_padding_size
810 - info->altivec_size;
811 gcc_assert (info->altivec_size == 0
812 || info->altivec_save_offset % 16 == 0);
813
814 /* Adjust for ROP protection. */
815 info->rop_hash_save_offset
816 = info->altivec_save_offset - info->rop_hash_size;
817 }
818 else
819 /* Adjust for ROP protection. */
820 info->rop_hash_save_offset
821 = info->gp_save_offset - info->rop_hash_size;
822
823 info->ehrd_offset = info->rop_hash_save_offset - ehrd_size;
824 info->ehcr_offset = info->ehrd_offset - ehcr_size;
825 info->cr_save_offset = reg_size; /* first word when 64-bit. */
826 info->lr_save_offset = 2*reg_size;
827 break;
828
829 case ABI_V4:
830 info->fp_save_offset = -info->fp_size;
831 info->gp_save_offset = info->fp_save_offset - info->gp_size;
832 info->cr_save_offset = info->gp_save_offset - info->cr_size;
833
834 if (TARGET_ALTIVEC_ABI)
835 {
836 info->vrsave_save_offset = info->cr_save_offset - info->vrsave_size;
837
838 /* Align stack so vector save area is on a quadword boundary. */
839 if (info->altivec_size != 0)
840 info->altivec_padding_size = 16 - (-info->vrsave_save_offset % 16);
841
842 info->altivec_save_offset = info->vrsave_save_offset
843 - info->altivec_padding_size
844 - info->altivec_size;
845
846 /* Adjust for AltiVec case. */
847 info->ehrd_offset = info->altivec_save_offset;
848 }
849 else
850 info->ehrd_offset = info->cr_save_offset;
851
852 info->ehrd_offset -= ehrd_size;
853 info->lr_save_offset = reg_size;
854 }
855
856 save_align = (TARGET_ALTIVEC_ABI || DEFAULT_ABI == ABI_DARWIN) ? 16 : 8;
857 info->save_size = RS6000_ALIGN (info->fp_size
858 + info->gp_size
859 + info->altivec_size
860 + info->altivec_padding_size
861 + info->rop_hash_size
862 + ehrd_size
863 + ehcr_size
864 + info->cr_size
865 + info->vrsave_size,
866 save_align);
867
868 non_fixed_size = info->vars_size + info->parm_size + info->save_size;
869
870 info->total_size = RS6000_ALIGN (non_fixed_size + info->fixed_size,
871 ABI_STACK_BOUNDARY / BITS_PER_UNIT);
872
873 /* Determine if we need to save the link register. */
874 if (info->calls_p
875 || ((DEFAULT_ABI == ABI_AIX || DEFAULT_ABI == ABI_ELFv2)
876 && crtl->profile
877 && !TARGET_PROFILE_KERNEL)
878 || (DEFAULT_ABI == ABI_V4 && cfun->calls_alloca)
879 #ifdef TARGET_RELOCATABLE
880 || (DEFAULT_ABI == ABI_V4
881 && (TARGET_RELOCATABLE || flag_pic > 1)
882 && !constant_pool_empty_p ())
883 #endif
884 || rs6000_ra_ever_killed ())
885 info->lr_save_p = 1;
886
887 using_static_chain_p = (cfun->static_chain_decl != NULL_TREE
888 && df_regs_ever_live_p (STATIC_CHAIN_REGNUM)
889 && call_used_or_fixed_reg_p (STATIC_CHAIN_REGNUM));
890 info->savres_strategy = rs6000_savres_strategy (info, using_static_chain_p);
891
892 if (!(info->savres_strategy & SAVE_INLINE_GPRS)
893 || !(info->savres_strategy & SAVE_INLINE_FPRS)
894 || !(info->savres_strategy & SAVE_INLINE_VRS)
895 || !(info->savres_strategy & REST_INLINE_GPRS)
896 || !(info->savres_strategy & REST_INLINE_FPRS)
897 || !(info->savres_strategy & REST_INLINE_VRS))
898 info->lr_save_p = 1;
899
900 if (info->lr_save_p)
901 df_set_regs_ever_live (LR_REGNO, true);
902
903 /* Determine if we need to allocate any stack frame:
904
905 For AIX we need to push the stack if a frame pointer is needed
906 (because the stack might be dynamically adjusted), if we are
907 debugging, if we make calls, or if the sum of fp_save, gp_save,
908 and local variables are more than the space needed to save all
909 non-volatile registers: 32-bit: 18*8 + 19*4 = 220 or 64-bit: 18*8
910 + 18*8 = 288 (GPR13 reserved).
911
912 For V.4 we don't have the stack cushion that AIX uses, but assume
913 that the debugger can handle stackless frames. */
914
915 if (info->calls_p)
916 info->push_p = 1;
917
918 else if (DEFAULT_ABI == ABI_V4)
919 info->push_p = non_fixed_size != 0;
920
921 else if (frame_pointer_needed || cfun->machine->asm_redzone_clobber_seen)
922 info->push_p = 1;
923
924 else
925 info->push_p = non_fixed_size > (TARGET_32BIT ? 220 : 288);
926
927 return info;
928 }
929
930 static void
931 debug_stack_info (rs6000_stack_t *info)
932 {
933 const char *abi_string;
934
935 if (! info)
936 info = rs6000_stack_info ();
937
938 fprintf (stderr, "\nStack information for function %s:\n",
939 ((current_function_decl && DECL_NAME (current_function_decl))
940 ? IDENTIFIER_POINTER (DECL_NAME (current_function_decl))
941 : "<unknown>"));
942
943 switch (info->abi)
944 {
945 default: abi_string = "Unknown"; break;
946 case ABI_NONE: abi_string = "NONE"; break;
947 case ABI_AIX: abi_string = "AIX"; break;
948 case ABI_ELFv2: abi_string = "ELFv2"; break;
949 case ABI_DARWIN: abi_string = "Darwin"; break;
950 case ABI_V4: abi_string = "V.4"; break;
951 }
952
953 fprintf (stderr, "\tABI = %5s\n", abi_string);
954
955 if (TARGET_ALTIVEC_ABI)
956 fprintf (stderr, "\tALTIVEC ABI extensions enabled.\n");
957
958 if (info->first_gp_reg_save != 32)
959 fprintf (stderr, "\tfirst_gp_reg_save = %5d\n", info->first_gp_reg_save);
960
961 if (info->first_fp_reg_save != 64)
962 fprintf (stderr, "\tfirst_fp_reg_save = %5d\n", info->first_fp_reg_save);
963
964 if (info->first_altivec_reg_save <= LAST_ALTIVEC_REGNO)
965 fprintf (stderr, "\tfirst_altivec_reg_save = %5d\n",
966 info->first_altivec_reg_save);
967
968 if (info->lr_save_p)
969 fprintf (stderr, "\tlr_save_p = %5d\n", info->lr_save_p);
970
971 if (info->cr_save_p)
972 fprintf (stderr, "\tcr_save_p = %5d\n", info->cr_save_p);
973
974 if (info->vrsave_mask)
975 fprintf (stderr, "\tvrsave_mask = 0x%x\n", info->vrsave_mask);
976
977 if (info->push_p)
978 fprintf (stderr, "\tpush_p = %5d\n", info->push_p);
979
980 if (info->calls_p)
981 fprintf (stderr, "\tcalls_p = %5d\n", info->calls_p);
982
983 if (info->gp_size)
984 fprintf (stderr, "\tgp_save_offset = %5d\n", info->gp_save_offset);
985
986 if (info->fp_size)
987 fprintf (stderr, "\tfp_save_offset = %5d\n", info->fp_save_offset);
988
989 if (info->altivec_size)
990 fprintf (stderr, "\taltivec_save_offset = %5d\n",
991 info->altivec_save_offset);
992
993 if (info->vrsave_size)
994 fprintf (stderr, "\tvrsave_save_offset = %5d\n",
995 info->vrsave_save_offset);
996
997 if (info->rop_hash_size)
998 fprintf (stderr, "\trop_hash_save_offset = %5d\n",
999 info->rop_hash_save_offset);
1000
1001 if (info->lr_save_p)
1002 fprintf (stderr, "\tlr_save_offset = %5d\n", info->lr_save_offset);
1003
1004 if (info->cr_save_p)
1005 fprintf (stderr, "\tcr_save_offset = %5d\n", info->cr_save_offset);
1006
1007 if (info->varargs_save_offset)
1008 fprintf (stderr, "\tvarargs_save_offset = %5d\n", info->varargs_save_offset);
1009
1010 if (info->total_size)
1011 fprintf (stderr, "\ttotal_size = " HOST_WIDE_INT_PRINT_DEC"\n",
1012 info->total_size);
1013
1014 if (info->vars_size)
1015 fprintf (stderr, "\tvars_size = " HOST_WIDE_INT_PRINT_DEC"\n",
1016 info->vars_size);
1017
1018 if (info->parm_size)
1019 fprintf (stderr, "\tparm_size = %5d\n", info->parm_size);
1020
1021 if (info->fixed_size)
1022 fprintf (stderr, "\tfixed_size = %5d\n", info->fixed_size);
1023
1024 if (info->gp_size)
1025 fprintf (stderr, "\tgp_size = %5d\n", info->gp_size);
1026
1027 if (info->fp_size)
1028 fprintf (stderr, "\tfp_size = %5d\n", info->fp_size);
1029
1030 if (info->altivec_size)
1031 fprintf (stderr, "\taltivec_size = %5d\n", info->altivec_size);
1032
1033 if (info->vrsave_size)
1034 fprintf (stderr, "\tvrsave_size = %5d\n", info->vrsave_size);
1035
1036 if (info->altivec_padding_size)
1037 fprintf (stderr, "\taltivec_padding_size= %5d\n",
1038 info->altivec_padding_size);
1039
1040 if (info->rop_hash_size)
1041 fprintf (stderr, "\trop_hash_size = %5d\n", info->rop_hash_size);
1042
1043 if (info->cr_size)
1044 fprintf (stderr, "\tcr_size = %5d\n", info->cr_size);
1045
1046 if (info->save_size)
1047 fprintf (stderr, "\tsave_size = %5d\n", info->save_size);
1048
1049 if (info->reg_size != 4)
1050 fprintf (stderr, "\treg_size = %5d\n", info->reg_size);
1051
1052 fprintf (stderr, "\tsave-strategy = %04x\n", info->savres_strategy);
1053
1054 if (info->abi == ABI_DARWIN)
1055 fprintf (stderr, "\tWORLD_SAVE_P = %5d\n", WORLD_SAVE_P(info));
1056
1057 fprintf (stderr, "\n");
1058 }
1059
1060 rtx
1061 rs6000_return_addr (int count, rtx frame)
1062 {
1063 /* We can't use get_hard_reg_initial_val for LR when count == 0 if LR
1064 is trashed by the prologue, as it is for PIC on ABI_V4 and Darwin. */
1065 if (count != 0
1066 || ((DEFAULT_ABI == ABI_V4 || DEFAULT_ABI == ABI_DARWIN) && flag_pic))
1067 {
1068 cfun->machine->ra_needs_full_frame = 1;
1069
1070 if (count == 0)
1071 /* FRAME is set to frame_pointer_rtx by the generic code, but that
1072 is good for loading 0(r1) only when !FRAME_GROWS_DOWNWARD. */
1073 frame = stack_pointer_rtx;
1074 rtx prev_frame_addr = memory_address (Pmode, frame);
1075 rtx prev_frame = copy_to_reg (gen_rtx_MEM (Pmode, prev_frame_addr));
1076 rtx lr_save_off = plus_constant (Pmode,
1077 prev_frame, RETURN_ADDRESS_OFFSET);
1078 rtx lr_save_addr = memory_address (Pmode, lr_save_off);
1079 return gen_rtx_MEM (Pmode, lr_save_addr);
1080 }
1081
1082 cfun->machine->ra_need_lr = 1;
1083 return get_hard_reg_initial_val (Pmode, LR_REGNO);
1084 }
1085
1086 /* Helper function for rs6000_function_ok_for_sibcall. */
1087
1088 bool
1089 rs6000_decl_ok_for_sibcall (tree decl)
1090 {
1091 /* Sibcalls are always fine for the Darwin ABI. */
1092 if (DEFAULT_ABI == ABI_DARWIN)
1093 return true;
1094
1095 if (DEFAULT_ABI == ABI_AIX || DEFAULT_ABI == ABI_ELFv2)
1096 {
1097 /* A function compiled using the PC-relative addressing model does not
1098 use a TOC pointer; nor is it guaranteed to preserve the value of
1099 r2 for its caller's TOC. Such a function may make sibcalls to any
1100 function, whether local or external, without restriction based on
1101 TOC-save/restore rules. */
1102 if (rs6000_pcrel_p ())
1103 return true;
1104
1105 /* Otherwise, under the AIX or ELFv2 ABIs we can't allow sibcalls
1106 to non-local functions, because the callee may not preserve the
1107 TOC pointer, and there's no way to ensure we restore the TOC when
1108 we return. */
1109 if (!decl || DECL_EXTERNAL (decl) || DECL_WEAK (decl)
1110 || !(*targetm.binds_local_p) (decl))
1111 return false;
1112
1113 /* A local sibcall from a function that preserves the TOC pointer
1114 to a function that does not is invalid for the same reason. */
1115 if (rs6000_fndecl_pcrel_p (decl))
1116 return false;
1117
1118 return true;
1119 }
1120
1121 /* With the secure-plt SYSV ABI we can't make non-local calls when
1122 -fpic/PIC because the plt call stubs use r30. */
1123 if (DEFAULT_ABI != ABI_V4
1124 || (TARGET_SECURE_PLT
1125 && flag_pic
1126 && (!decl || !((*targetm.binds_local_p) (decl)))))
1127 return false;
1128
1129 return true;
1130 }
1131
1132 /* Say whether a function is a candidate for sibcall handling or not. */
1133
1134 bool
1135 rs6000_function_ok_for_sibcall (tree decl, tree exp)
1136 {
1137 tree fntype;
1138
1139 /* The sibcall epilogue may clobber the static chain register.
1140 ??? We could work harder and avoid that, but it's probably
1141 not worth the hassle in practice. */
1142 if (CALL_EXPR_STATIC_CHAIN (exp))
1143 return false;
1144
1145 if (decl)
1146 fntype = TREE_TYPE (decl);
1147 else
1148 fntype = TREE_TYPE (TREE_TYPE (CALL_EXPR_FN (exp)));
1149
1150 /* We can't do it if the called function has more vector parameters
1151 than the current function; there's nowhere to put the VRsave code. */
1152 if (TARGET_ALTIVEC_ABI
1153 && TARGET_ALTIVEC_VRSAVE
1154 && !(decl && decl == current_function_decl))
1155 {
1156 function_args_iterator args_iter;
1157 tree type;
1158 int nvreg = 0;
1159
1160 /* Functions with vector parameters are required to have a
1161 prototype, so the argument type info must be available
1162 here. */
1163 FOREACH_FUNCTION_ARGS(fntype, type, args_iter)
1164 if (VECTOR_TYPE_P (type)
1165 && ALTIVEC_OR_VSX_VECTOR_MODE (TYPE_MODE (type)))
1166 nvreg++;
1167
1168 FOREACH_FUNCTION_ARGS(TREE_TYPE (current_function_decl), type, args_iter)
1169 if (VECTOR_TYPE_P (type)
1170 && ALTIVEC_OR_VSX_VECTOR_MODE (TYPE_MODE (type)))
1171 nvreg--;
1172
1173 if (nvreg > 0)
1174 return false;
1175 }
1176
1177 if (rs6000_decl_ok_for_sibcall (decl))
1178 {
1179 tree attr_list = TYPE_ATTRIBUTES (fntype);
1180
1181 if (!lookup_attribute ("longcall", attr_list)
1182 || lookup_attribute ("shortcall", attr_list))
1183 return true;
1184 }
1185
1186 return false;
1187 }
1188
1189 static int
1190 rs6000_ra_ever_killed (void)
1191 {
1192 rtx_insn *top;
1193 rtx reg;
1194 rtx_insn *insn;
1195
1196 if (cfun->is_thunk)
1197 return 0;
1198
1199 if (cfun->machine->lr_save_state)
1200 return cfun->machine->lr_save_state - 1;
1201
1202 /* regs_ever_live has LR marked as used if any sibcalls are present,
1203 but this should not force saving and restoring in the
1204 pro/epilogue. Likewise, reg_set_between_p thinks a sibcall
1205 clobbers LR, so that is inappropriate. */
1206
1207 /* Also, the prologue can generate a store into LR that
1208 doesn't really count, like this:
1209
1210 move LR->R0
1211 bcl to set PIC register
1212 move LR->R31
1213 move R0->LR
1214
1215 When we're called from the epilogue, we need to avoid counting
1216 this as a store. */
1217
1218 push_topmost_sequence ();
1219 top = get_insns ();
1220 pop_topmost_sequence ();
1221 reg = gen_rtx_REG (Pmode, LR_REGNO);
1222
1223 for (insn = NEXT_INSN (top); insn != NULL_RTX; insn = NEXT_INSN (insn))
1224 {
1225 if (INSN_P (insn))
1226 {
1227 if (CALL_P (insn))
1228 {
1229 if (!SIBLING_CALL_P (insn))
1230 return 1;
1231 }
1232 else if (find_regno_note (insn, REG_INC, LR_REGNO))
1233 return 1;
1234 else if (set_of (reg, insn) != NULL_RTX
1235 && !prologue_epilogue_contains (insn))
1236 return 1;
1237 }
1238 }
1239 return 0;
1240 }
1241
1242 /* Emit instructions needed to load the TOC register.
1243 This is only needed when TARGET_TOC, TARGET_MINIMAL_TOC, and there is
1244 a constant pool; or for SVR4 -fpic. */
1245
1246 void
1247 rs6000_emit_load_toc_table (int fromprolog)
1248 {
1249 rtx dest;
1250 dest = gen_rtx_REG (Pmode, RS6000_PIC_OFFSET_TABLE_REGNUM);
1251
1252 if (TARGET_ELF && TARGET_SECURE_PLT && DEFAULT_ABI == ABI_V4 && flag_pic)
1253 {
1254 char buf[30];
1255 rtx lab, tmp1, tmp2, got;
1256
1257 lab = gen_label_rtx ();
1258 ASM_GENERATE_INTERNAL_LABEL (buf, "L", CODE_LABEL_NUMBER (lab));
1259 lab = gen_rtx_SYMBOL_REF (Pmode, ggc_strdup (buf));
1260 if (flag_pic == 2)
1261 {
1262 got = gen_rtx_SYMBOL_REF (Pmode, ggc_strdup (toc_label_name));
1263 need_toc_init = 1;
1264 }
1265 else
1266 got = rs6000_got_sym ();
1267 tmp1 = tmp2 = dest;
1268 if (!fromprolog)
1269 {
1270 tmp1 = gen_reg_rtx (Pmode);
1271 tmp2 = gen_reg_rtx (Pmode);
1272 }
1273 emit_insn (gen_load_toc_v4_PIC_1 (lab));
1274 emit_move_insn (tmp1, gen_rtx_REG (Pmode, LR_REGNO));
1275 emit_insn (gen_load_toc_v4_PIC_3b (tmp2, tmp1, got, lab));
1276 emit_insn (gen_load_toc_v4_PIC_3c (dest, tmp2, got, lab));
1277 }
1278 else if (TARGET_ELF && DEFAULT_ABI == ABI_V4 && flag_pic == 1)
1279 {
1280 emit_insn (gen_load_toc_v4_pic_si ());
1281 emit_move_insn (dest, gen_rtx_REG (Pmode, LR_REGNO));
1282 }
1283 else if (TARGET_ELF && DEFAULT_ABI == ABI_V4 && flag_pic == 2)
1284 {
1285 char buf[30];
1286 rtx temp0 = (fromprolog
1287 ? gen_rtx_REG (Pmode, 0)
1288 : gen_reg_rtx (Pmode));
1289
1290 if (fromprolog)
1291 {
1292 rtx symF, symL;
1293
1294 ASM_GENERATE_INTERNAL_LABEL (buf, "LCF", rs6000_pic_labelno);
1295 symF = gen_rtx_SYMBOL_REF (Pmode, ggc_strdup (buf));
1296
1297 ASM_GENERATE_INTERNAL_LABEL (buf, "LCL", rs6000_pic_labelno);
1298 symL = gen_rtx_SYMBOL_REF (Pmode, ggc_strdup (buf));
1299
1300 emit_insn (gen_load_toc_v4_PIC_1 (symF));
1301 emit_move_insn (dest, gen_rtx_REG (Pmode, LR_REGNO));
1302 emit_insn (gen_load_toc_v4_PIC_2 (temp0, dest, symL, symF));
1303 }
1304 else
1305 {
1306 rtx tocsym, lab;
1307
1308 tocsym = gen_rtx_SYMBOL_REF (Pmode, ggc_strdup (toc_label_name));
1309 need_toc_init = 1;
1310 lab = gen_label_rtx ();
1311 emit_insn (gen_load_toc_v4_PIC_1b (tocsym, lab));
1312 emit_move_insn (dest, gen_rtx_REG (Pmode, LR_REGNO));
1313 if (TARGET_LINK_STACK)
1314 emit_insn (gen_addsi3 (dest, dest, GEN_INT (4)));
1315 emit_move_insn (temp0, gen_rtx_MEM (Pmode, dest));
1316 }
1317 emit_insn (gen_addsi3 (dest, temp0, dest));
1318 }
1319 else if (TARGET_ELF && !TARGET_AIX && flag_pic == 0 && TARGET_MINIMAL_TOC)
1320 {
1321 /* This is for AIX code running in non-PIC ELF32. */
1322 rtx realsym = gen_rtx_SYMBOL_REF (Pmode, ggc_strdup (toc_label_name));
1323
1324 need_toc_init = 1;
1325 emit_insn (gen_elf_high (dest, realsym));
1326 emit_insn (gen_elf_low (dest, dest, realsym));
1327 }
1328 else
1329 {
1330 gcc_assert (DEFAULT_ABI == ABI_AIX || DEFAULT_ABI == ABI_ELFv2);
1331
1332 if (TARGET_32BIT)
1333 emit_insn (gen_load_toc_aix_si (dest));
1334 else
1335 emit_insn (gen_load_toc_aix_di (dest));
1336 }
1337 }
1338
1339 /* Emit instructions to restore the link register after determining where
1340 its value has been stored. */
1341
1342 void
1343 rs6000_emit_eh_reg_restore (rtx source, rtx scratch)
1344 {
1345 rs6000_stack_t *info = rs6000_stack_info ();
1346 rtx operands[2];
1347
1348 operands[0] = source;
1349 operands[1] = scratch;
1350
1351 if (info->lr_save_p)
1352 {
1353 rtx frame_rtx = stack_pointer_rtx;
1354 HOST_WIDE_INT sp_offset = 0;
1355 rtx tmp;
1356
1357 if (frame_pointer_needed
1358 || cfun->calls_alloca
1359 || info->total_size > 32767)
1360 {
1361 tmp = gen_frame_mem (Pmode, frame_rtx);
1362 emit_move_insn (operands[1], tmp);
1363 frame_rtx = operands[1];
1364 }
1365 else if (info->push_p)
1366 sp_offset = info->total_size;
1367
1368 tmp = plus_constant (Pmode, frame_rtx,
1369 info->lr_save_offset + sp_offset);
1370 tmp = gen_frame_mem (Pmode, tmp);
1371 emit_move_insn (tmp, operands[0]);
1372 }
1373 else
1374 emit_move_insn (gen_rtx_REG (Pmode, LR_REGNO), operands[0]);
1375
1376 /* Freeze lr_save_p. We've just emitted rtl that depends on the
1377 state of lr_save_p so any change from here on would be a bug. In
1378 particular, stop rs6000_ra_ever_killed from considering the SET
1379 of lr we may have added just above. */
1380 cfun->machine->lr_save_state = info->lr_save_p + 1;
1381 }
1382
1383 /* This returns nonzero if the current function uses the TOC. This is
1384 determined by the presence of (use (unspec ... UNSPEC_TOC)), which
1385 is generated by the ABI_V4 load_toc_* patterns.
1386 Return 2 instead of 1 if the load_toc_* pattern is in the function
1387 partition that doesn't start the function. */
1388 #if TARGET_ELF
1389 int
1390 uses_TOC (void)
1391 {
1392 rtx_insn *insn;
1393 int ret = 1;
1394
1395 for (insn = get_insns (); insn; insn = NEXT_INSN (insn))
1396 {
1397 if (INSN_P (insn))
1398 {
1399 rtx pat = PATTERN (insn);
1400 int i;
1401
1402 if (GET_CODE (pat) == PARALLEL)
1403 for (i = 0; i < XVECLEN (pat, 0); i++)
1404 {
1405 rtx sub = XVECEXP (pat, 0, i);
1406 if (GET_CODE (sub) == USE)
1407 {
1408 sub = XEXP (sub, 0);
1409 if (GET_CODE (sub) == UNSPEC
1410 && XINT (sub, 1) == UNSPEC_TOC)
1411 return ret;
1412 }
1413 }
1414 }
1415 else if (crtl->has_bb_partition
1416 && NOTE_P (insn)
1417 && NOTE_KIND (insn) == NOTE_INSN_SWITCH_TEXT_SECTIONS)
1418 ret = 2;
1419 }
1420 return 0;
1421 }
1422 #endif
1423
1424 /* Issue assembly directives that create a reference to the given DWARF
1425 FRAME_TABLE_LABEL from the current function section. */
1426 void
1427 rs6000_aix_asm_output_dwarf_table_ref (char * frame_table_label)
1428 {
1429 fprintf (asm_out_file, "\t.ref %s\n",
1430 (* targetm.strip_name_encoding) (frame_table_label));
1431 }
1432
1433 /* This ties together stack memory (MEM with an alias set of frame_alias_set)
1434 and the change to the stack pointer. */
1435
1436 static void
1437 rs6000_emit_stack_tie (rtx fp, bool hard_frame_needed)
1438 {
1439 rtvec p;
1440 int i;
1441 rtx regs[3];
1442
1443 i = 0;
1444 regs[i++] = gen_rtx_REG (Pmode, STACK_POINTER_REGNUM);
1445 if (hard_frame_needed)
1446 regs[i++] = gen_rtx_REG (Pmode, HARD_FRAME_POINTER_REGNUM);
1447 if (!(REGNO (fp) == STACK_POINTER_REGNUM
1448 || (hard_frame_needed
1449 && REGNO (fp) == HARD_FRAME_POINTER_REGNUM)))
1450 regs[i++] = fp;
1451
1452 p = rtvec_alloc (i);
1453 while (--i >= 0)
1454 {
1455 rtx mem = gen_frame_mem (BLKmode, regs[i]);
1456 RTVEC_ELT (p, i) = gen_rtx_SET (mem, const0_rtx);
1457 }
1458
1459 emit_insn (gen_stack_tie (gen_rtx_PARALLEL (VOIDmode, p)));
1460 }
1461
1462 /* Allocate SIZE_INT bytes on the stack using a store with update style insn
1463 and set the appropriate attributes for the generated insn. Return the
1464 first insn which adjusts the stack pointer or the last insn before
1465 the stack adjustment loop.
1466
1467 SIZE_INT is used to create the CFI note for the allocation.
1468
1469 SIZE_RTX is an rtx containing the size of the adjustment. Note that
1470 since stacks grow to lower addresses its runtime value is -SIZE_INT.
1471
1472 ORIG_SP contains the backchain value that must be stored at *sp. */
1473
1474 static rtx_insn *
1475 rs6000_emit_allocate_stack_1 (HOST_WIDE_INT size_int, rtx orig_sp)
1476 {
1477 rtx_insn *insn;
1478
1479 rtx size_rtx = GEN_INT (-size_int);
1480 if (size_int > 32767)
1481 {
1482 rtx tmp_reg = gen_rtx_REG (Pmode, 0);
1483 /* Need a note here so that try_split doesn't get confused. */
1484 if (get_last_insn () == NULL_RTX)
1485 emit_note (NOTE_INSN_DELETED);
1486 insn = emit_move_insn (tmp_reg, size_rtx);
1487 try_split (PATTERN (insn), insn, 0);
1488 size_rtx = tmp_reg;
1489 }
1490
1491 if (TARGET_32BIT)
1492 insn = emit_insn (gen_movsi_update_stack (stack_pointer_rtx,
1493 stack_pointer_rtx,
1494 size_rtx,
1495 orig_sp));
1496 else
1497 insn = emit_insn (gen_movdi_update_stack (stack_pointer_rtx,
1498 stack_pointer_rtx,
1499 size_rtx,
1500 orig_sp));
1501 rtx par = PATTERN (insn);
1502 gcc_assert (GET_CODE (par) == PARALLEL);
1503 rtx set = XVECEXP (par, 0, 0);
1504 gcc_assert (GET_CODE (set) == SET);
1505 rtx mem = SET_DEST (set);
1506 gcc_assert (MEM_P (mem));
1507 MEM_NOTRAP_P (mem) = 1;
1508 set_mem_alias_set (mem, get_frame_alias_set ());
1509
1510 RTX_FRAME_RELATED_P (insn) = 1;
1511 add_reg_note (insn, REG_FRAME_RELATED_EXPR,
1512 gen_rtx_SET (stack_pointer_rtx,
1513 gen_rtx_PLUS (Pmode,
1514 stack_pointer_rtx,
1515 GEN_INT (-size_int))));
1516
1517 /* Emit a blockage to ensure the allocation/probing insns are
1518 not optimized, combined, removed, etc. Add REG_STACK_CHECK
1519 note for similar reasons. */
1520 if (flag_stack_clash_protection)
1521 {
1522 add_reg_note (insn, REG_STACK_CHECK, const0_rtx);
1523 emit_insn (gen_blockage ());
1524 }
1525
1526 return insn;
1527 }
1528
1529 static HOST_WIDE_INT
1530 get_stack_clash_protection_probe_interval (void)
1531 {
1532 return (HOST_WIDE_INT_1U
1533 << param_stack_clash_protection_probe_interval);
1534 }
1535
1536 static HOST_WIDE_INT
1537 get_stack_clash_protection_guard_size (void)
1538 {
1539 return (HOST_WIDE_INT_1U
1540 << param_stack_clash_protection_guard_size);
1541 }
1542
1543 /* Allocate ORIG_SIZE bytes on the stack and probe the newly
1544 allocated space every STACK_CLASH_PROTECTION_PROBE_INTERVAL bytes.
1545
1546 COPY_REG, if non-null, should contain a copy of the original
1547 stack pointer at exit from this function.
1548
1549 This is subtly different than the Ada probing in that it tries hard to
1550 prevent attacks that jump the stack guard. Thus it is never allowed to
1551 allocate more than STACK_CLASH_PROTECTION_PROBE_INTERVAL bytes of stack
1552 space without a suitable probe. */
1553 static rtx_insn *
1554 rs6000_emit_probe_stack_range_stack_clash (HOST_WIDE_INT orig_size,
1555 rtx copy_reg)
1556 {
1557 rtx orig_sp = copy_reg;
1558
1559 HOST_WIDE_INT probe_interval = get_stack_clash_protection_probe_interval ();
1560
1561 /* Round the size down to a multiple of PROBE_INTERVAL. */
1562 HOST_WIDE_INT rounded_size = ROUND_DOWN (orig_size, probe_interval);
1563
1564 /* If explicitly requested,
1565 or the rounded size is not the same as the original size
1566 or the rounded size is greater than a page,
1567 then we will need a copy of the original stack pointer. */
1568 if (rounded_size != orig_size
1569 || rounded_size > probe_interval
1570 || copy_reg)
1571 {
1572 /* If the caller did not request a copy of the incoming stack
1573 pointer, then we use r0 to hold the copy. */
1574 if (!copy_reg)
1575 orig_sp = gen_rtx_REG (Pmode, 0);
1576 emit_move_insn (orig_sp, stack_pointer_rtx);
1577 }
1578
1579 /* There's three cases here.
1580
1581 One is a single probe which is the most common and most efficiently
1582 implemented as it does not have to have a copy of the original
1583 stack pointer if there are no residuals.
1584
1585 Second is unrolled allocation/probes which we use if there's just
1586 a few of them. It needs to save the original stack pointer into a
1587 temporary for use as a source register in the allocation/probe.
1588
1589 Last is a loop. This is the most uncommon case and least efficient. */
1590 rtx_insn *retval = NULL;
1591 if (rounded_size == probe_interval)
1592 {
1593 retval = rs6000_emit_allocate_stack_1 (probe_interval, stack_pointer_rtx);
1594
1595 dump_stack_clash_frame_info (PROBE_INLINE, rounded_size != orig_size);
1596 }
1597 else if (rounded_size <= 8 * probe_interval)
1598 {
1599 /* The ABI requires using the store with update insns to allocate
1600 space and store the backchain into the stack
1601
1602 So we save the current stack pointer into a temporary, then
1603 emit the store-with-update insns to store the saved stack pointer
1604 into the right location in each new page. */
1605 for (int i = 0; i < rounded_size; i += probe_interval)
1606 {
1607 rtx_insn *insn
1608 = rs6000_emit_allocate_stack_1 (probe_interval, orig_sp);
1609
1610 /* Save the first stack adjustment in RETVAL. */
1611 if (i == 0)
1612 retval = insn;
1613 }
1614
1615 dump_stack_clash_frame_info (PROBE_INLINE, rounded_size != orig_size);
1616 }
1617 else
1618 {
1619 /* Compute the ending address. */
1620 rtx end_addr
1621 = copy_reg ? gen_rtx_REG (Pmode, 0) : gen_rtx_REG (Pmode, 12);
1622 rtx rs = GEN_INT (-rounded_size);
1623 rtx_insn *insn = gen_add3_insn (end_addr, stack_pointer_rtx, rs);
1624 if (insn == NULL)
1625 {
1626 emit_move_insn (end_addr, rs);
1627 insn = gen_add3_insn (end_addr, end_addr, stack_pointer_rtx);
1628 gcc_assert (insn);
1629 }
1630 bool add_note = false;
1631 if (!NONJUMP_INSN_P (insn) || NEXT_INSN (insn))
1632 add_note = true;
1633 else
1634 {
1635 rtx set = single_set (insn);
1636 if (set == NULL_RTX
1637 || SET_DEST (set) != end_addr
1638 || GET_CODE (SET_SRC (set)) != PLUS
1639 || XEXP (SET_SRC (set), 0) != stack_pointer_rtx
1640 || XEXP (SET_SRC (set), 1) != rs)
1641 add_note = true;
1642 }
1643 insn = emit_insn (insn);
1644 /* Describe the effect of INSN to the CFI engine, unless it
1645 is a single insn that describes it itself. */
1646 if (add_note)
1647 add_reg_note (insn, REG_FRAME_RELATED_EXPR,
1648 gen_rtx_SET (end_addr,
1649 gen_rtx_PLUS (Pmode, stack_pointer_rtx,
1650 rs)));
1651 RTX_FRAME_RELATED_P (insn) = 1;
1652
1653 /* Emit the loop. */
1654 if (TARGET_64BIT)
1655 retval = emit_insn (gen_probe_stack_rangedi (stack_pointer_rtx,
1656 stack_pointer_rtx, orig_sp,
1657 end_addr));
1658 else
1659 retval = emit_insn (gen_probe_stack_rangesi (stack_pointer_rtx,
1660 stack_pointer_rtx, orig_sp,
1661 end_addr));
1662 RTX_FRAME_RELATED_P (retval) = 1;
1663 /* Describe the effect of INSN to the CFI engine. */
1664 add_reg_note (retval, REG_FRAME_RELATED_EXPR,
1665 gen_rtx_SET (stack_pointer_rtx, end_addr));
1666
1667 /* Emit a blockage to ensure the allocation/probing insns are
1668 not optimized, combined, removed, etc. Other cases handle this
1669 within their call to rs6000_emit_allocate_stack_1. */
1670 emit_insn (gen_blockage ());
1671
1672 dump_stack_clash_frame_info (PROBE_LOOP, rounded_size != orig_size);
1673 }
1674
1675 if (orig_size != rounded_size)
1676 {
1677 /* Allocate (and implicitly probe) any residual space. */
1678 HOST_WIDE_INT residual = orig_size - rounded_size;
1679
1680 rtx_insn *insn = rs6000_emit_allocate_stack_1 (residual, orig_sp);
1681
1682 /* If the residual was the only allocation, then we can return the
1683 allocating insn. */
1684 if (!retval)
1685 retval = insn;
1686 }
1687
1688 return retval;
1689 }
1690
1691 /* Emit the correct code for allocating stack space, as insns.
1692 If COPY_REG, make sure a copy of the old frame is left there.
1693 The generated code may use hard register 0 as a temporary. */
1694
1695 static rtx_insn *
1696 rs6000_emit_allocate_stack (HOST_WIDE_INT size, rtx copy_reg, int copy_off)
1697 {
1698 rtx_insn *insn;
1699 rtx stack_reg = gen_rtx_REG (Pmode, STACK_POINTER_REGNUM);
1700 rtx tmp_reg = gen_rtx_REG (Pmode, 0);
1701 rtx todec = gen_int_mode (-size, Pmode);
1702
1703 if (INTVAL (todec) != -size)
1704 {
1705 warning (0, "stack frame too large");
1706 emit_insn (gen_trap ());
1707 return 0;
1708 }
1709
1710 if (crtl->limit_stack)
1711 {
1712 if (REG_P (stack_limit_rtx)
1713 && REGNO (stack_limit_rtx) > 1
1714 && REGNO (stack_limit_rtx) <= 31)
1715 {
1716 rtx_insn *insn
1717 = gen_add3_insn (tmp_reg, stack_limit_rtx, GEN_INT (size));
1718 gcc_assert (insn);
1719 emit_insn (insn);
1720 emit_insn (gen_cond_trap (LTU, stack_reg, tmp_reg, const0_rtx));
1721 }
1722 else if (SYMBOL_REF_P (stack_limit_rtx)
1723 && TARGET_32BIT
1724 && DEFAULT_ABI == ABI_V4
1725 && !flag_pic)
1726 {
1727 rtx toload = gen_rtx_CONST (VOIDmode,
1728 gen_rtx_PLUS (Pmode,
1729 stack_limit_rtx,
1730 GEN_INT (size)));
1731
1732 /* We cannot use r0 with elf_low. Lamely solve this problem by
1733 moving registers around. */
1734 rtx r11_reg = gen_rtx_REG (Pmode, 11);
1735 emit_move_insn (tmp_reg, r11_reg);
1736 emit_insn (gen_elf_high (r11_reg, toload));
1737 emit_insn (gen_elf_low (r11_reg, r11_reg, toload));
1738 emit_insn (gen_cond_trap (LTU, stack_reg, r11_reg, const0_rtx));
1739 emit_move_insn (r11_reg, tmp_reg);
1740 }
1741 else
1742 warning (0, "stack limit expression is not supported");
1743 }
1744
1745 if (flag_stack_clash_protection)
1746 {
1747 if (size < get_stack_clash_protection_guard_size ())
1748 dump_stack_clash_frame_info (NO_PROBE_SMALL_FRAME, true);
1749 else
1750 {
1751 rtx_insn *insn = rs6000_emit_probe_stack_range_stack_clash (size,
1752 copy_reg);
1753
1754 /* If we asked for a copy with an offset, then we still need add in
1755 the offset. */
1756 if (copy_reg && copy_off)
1757 emit_insn (gen_add3_insn (copy_reg, copy_reg, GEN_INT (copy_off)));
1758 return insn;
1759 }
1760 }
1761
1762 if (copy_reg)
1763 {
1764 if (copy_off != 0)
1765 emit_insn (gen_add3_insn (copy_reg, stack_reg, GEN_INT (copy_off)));
1766 else
1767 emit_move_insn (copy_reg, stack_reg);
1768 }
1769
1770 /* Since we didn't use gen_frame_mem to generate the MEM, grab
1771 it now and set the alias set/attributes. The above gen_*_update
1772 calls will generate a PARALLEL with the MEM set being the first
1773 operation. */
1774 insn = rs6000_emit_allocate_stack_1 (size, stack_reg);
1775 return insn;
1776 }
1777
1778 #define PROBE_INTERVAL (1 << STACK_CHECK_PROBE_INTERVAL_EXP)
1779
1780 #if PROBE_INTERVAL > 32768
1781 #error Cannot use indexed addressing mode for stack probing
1782 #endif
1783
1784 /* Emit code to probe a range of stack addresses from FIRST to FIRST+SIZE,
1785 inclusive. These are offsets from the current stack pointer. */
1786
1787 static void
1788 rs6000_emit_probe_stack_range (HOST_WIDE_INT first, HOST_WIDE_INT size)
1789 {
1790 /* See if we have a constant small number of probes to generate. If so,
1791 that's the easy case. */
1792 if (first + size <= 32768)
1793 {
1794 HOST_WIDE_INT i;
1795
1796 /* Probe at FIRST + N * PROBE_INTERVAL for values of N from 1 until
1797 it exceeds SIZE. If only one probe is needed, this will not
1798 generate any code. Then probe at FIRST + SIZE. */
1799 for (i = PROBE_INTERVAL; i < size; i += PROBE_INTERVAL)
1800 emit_stack_probe (plus_constant (Pmode, stack_pointer_rtx,
1801 -(first + i)));
1802
1803 emit_stack_probe (plus_constant (Pmode, stack_pointer_rtx,
1804 -(first + size)));
1805 }
1806
1807 /* Otherwise, do the same as above, but in a loop. Note that we must be
1808 extra careful with variables wrapping around because we might be at
1809 the very top (or the very bottom) of the address space and we have
1810 to be able to handle this case properly; in particular, we use an
1811 equality test for the loop condition. */
1812 else
1813 {
1814 HOST_WIDE_INT rounded_size;
1815 rtx r12 = gen_rtx_REG (Pmode, 12);
1816 rtx r0 = gen_rtx_REG (Pmode, 0);
1817
1818 /* Sanity check for the addressing mode we're going to use. */
1819 gcc_assert (first <= 32768);
1820
1821 /* Step 1: round SIZE to the previous multiple of the interval. */
1822
1823 rounded_size = ROUND_DOWN (size, PROBE_INTERVAL);
1824
1825
1826 /* Step 2: compute initial and final value of the loop counter. */
1827
1828 /* TEST_ADDR = SP + FIRST. */
1829 emit_insn (gen_rtx_SET (r12, plus_constant (Pmode, stack_pointer_rtx,
1830 -first)));
1831
1832 /* LAST_ADDR = SP + FIRST + ROUNDED_SIZE. */
1833 if (rounded_size > 32768)
1834 {
1835 emit_move_insn (r0, GEN_INT (-rounded_size));
1836 emit_insn (gen_rtx_SET (r0, gen_rtx_PLUS (Pmode, r12, r0)));
1837 }
1838 else
1839 emit_insn (gen_rtx_SET (r0, plus_constant (Pmode, r12,
1840 -rounded_size)));
1841
1842
1843 /* Step 3: the loop
1844
1845 do
1846 {
1847 TEST_ADDR = TEST_ADDR + PROBE_INTERVAL
1848 probe at TEST_ADDR
1849 }
1850 while (TEST_ADDR != LAST_ADDR)
1851
1852 probes at FIRST + N * PROBE_INTERVAL for values of N from 1
1853 until it is equal to ROUNDED_SIZE. */
1854
1855 if (TARGET_64BIT)
1856 emit_insn (gen_probe_stack_rangedi (r12, r12, stack_pointer_rtx, r0));
1857 else
1858 emit_insn (gen_probe_stack_rangesi (r12, r12, stack_pointer_rtx, r0));
1859
1860
1861 /* Step 4: probe at FIRST + SIZE if we cannot assert at compile-time
1862 that SIZE is equal to ROUNDED_SIZE. */
1863
1864 if (size != rounded_size)
1865 emit_stack_probe (plus_constant (Pmode, r12, rounded_size - size));
1866 }
1867 }
1868
1869 /* Probe a range of stack addresses from REG1 to REG2 inclusive. These are
1870 addresses, not offsets. */
1871
1872 static const char *
1873 output_probe_stack_range_1 (rtx reg1, rtx reg2)
1874 {
1875 static int labelno = 0;
1876 char loop_lab[32];
1877 rtx xops[2];
1878
1879 ASM_GENERATE_INTERNAL_LABEL (loop_lab, "LPSRL", labelno++);
1880
1881 /* Loop. */
1882 ASM_OUTPUT_INTERNAL_LABEL (asm_out_file, loop_lab);
1883
1884 /* TEST_ADDR = TEST_ADDR + PROBE_INTERVAL. */
1885 xops[0] = reg1;
1886 xops[1] = GEN_INT (-PROBE_INTERVAL);
1887 output_asm_insn ("addi %0,%0,%1", xops);
1888
1889 /* Probe at TEST_ADDR. */
1890 xops[1] = gen_rtx_REG (Pmode, 0);
1891 output_asm_insn ("stw %1,0(%0)", xops);
1892
1893 /* Test if TEST_ADDR == LAST_ADDR. */
1894 xops[1] = reg2;
1895 if (TARGET_64BIT)
1896 output_asm_insn ("cmpd 0,%0,%1", xops);
1897 else
1898 output_asm_insn ("cmpw 0,%0,%1", xops);
1899
1900 /* Branch. */
1901 fputs ("\tbne 0,", asm_out_file);
1902 assemble_name_raw (asm_out_file, loop_lab);
1903 fputc ('\n', asm_out_file);
1904
1905 return "";
1906 }
1907
1908 /* This function is called when rs6000_frame_related is processing
1909 SETs within a PARALLEL, and returns whether the REGNO save ought to
1910 be marked RTX_FRAME_RELATED_P. The PARALLELs involved are those
1911 for out-of-line register save functions, store multiple, and the
1912 Darwin world_save. They may contain registers that don't really
1913 need saving. */
1914
1915 static bool
1916 interesting_frame_related_regno (unsigned int regno)
1917 {
1918 /* Saves apparently of r0 are actually saving LR. It doesn't make
1919 sense to substitute the regno here to test save_reg_p (LR_REGNO).
1920 We *know* LR needs saving, and dwarf2cfi.cc is able to deduce that
1921 (set (mem) (r0)) is saving LR from a prior (set (r0) (lr)) marked
1922 as frame related. */
1923 if (regno == 0)
1924 return true;
1925 /* If we see CR2 then we are here on a Darwin world save. Saves of
1926 CR2 signify the whole CR is being saved. This is a long-standing
1927 ABI wart fixed by ELFv2. As for r0/lr there is no need to check
1928 that CR needs to be saved. */
1929 if (regno == CR2_REGNO)
1930 return true;
1931 /* Omit frame info for any user-defined global regs. If frame info
1932 is supplied for them, frame unwinding will restore a user reg.
1933 Also omit frame info for any reg we don't need to save, as that
1934 bloats frame info and can cause problems with shrink wrapping.
1935 Since global regs won't be seen as needing to be saved, both of
1936 these conditions are covered by save_reg_p. */
1937 return save_reg_p (regno);
1938 }
1939
1940 /* Probe a range of stack addresses from REG1 to REG3 inclusive. These are
1941 addresses, not offsets.
1942
1943 REG2 contains the backchain that must be stored into *sp at each allocation.
1944
1945 This is subtly different than the Ada probing above in that it tries hard
1946 to prevent attacks that jump the stack guard. Thus, it is never allowed
1947 to allocate more than PROBE_INTERVAL bytes of stack space without a
1948 suitable probe. */
1949
1950 static const char *
1951 output_probe_stack_range_stack_clash (rtx reg1, rtx reg2, rtx reg3)
1952 {
1953 static int labelno = 0;
1954 char loop_lab[32];
1955 rtx xops[3];
1956
1957 HOST_WIDE_INT probe_interval = get_stack_clash_protection_probe_interval ();
1958
1959 ASM_GENERATE_INTERNAL_LABEL (loop_lab, "LPSRL", labelno++);
1960
1961 ASM_OUTPUT_INTERNAL_LABEL (asm_out_file, loop_lab);
1962
1963 /* This allocates and probes. */
1964 xops[0] = reg1;
1965 xops[1] = reg2;
1966 xops[2] = GEN_INT (-probe_interval);
1967 if (TARGET_64BIT)
1968 output_asm_insn ("stdu %1,%2(%0)", xops);
1969 else
1970 output_asm_insn ("stwu %1,%2(%0)", xops);
1971
1972 /* Jump to LOOP_LAB if TEST_ADDR != LAST_ADDR. */
1973 xops[0] = reg1;
1974 xops[1] = reg3;
1975 if (TARGET_64BIT)
1976 output_asm_insn ("cmpd 0,%0,%1", xops);
1977 else
1978 output_asm_insn ("cmpw 0,%0,%1", xops);
1979
1980 fputs ("\tbne 0,", asm_out_file);
1981 assemble_name_raw (asm_out_file, loop_lab);
1982 fputc ('\n', asm_out_file);
1983
1984 return "";
1985 }
1986
1987 /* Wrapper around the output_probe_stack_range routines. */
1988 const char *
1989 output_probe_stack_range (rtx reg1, rtx reg2, rtx reg3)
1990 {
1991 if (flag_stack_clash_protection)
1992 return output_probe_stack_range_stack_clash (reg1, reg2, reg3);
1993 else
1994 return output_probe_stack_range_1 (reg1, reg3);
1995 }
1996
1997 /* Add to 'insn' a note which is PATTERN (INSN) but with REG replaced
1998 with (plus:P (reg 1) VAL), and with REG2 replaced with REPL2 if REG2
1999 is not NULL. It would be nice if dwarf2out_frame_debug_expr could
2000 deduce these equivalences by itself so it wasn't necessary to hold
2001 its hand so much. Don't be tempted to always supply d2_f_d_e with
2002 the actual cfa register, ie. r31 when we are using a hard frame
2003 pointer. That fails when saving regs off r1, and sched moves the
2004 r31 setup past the reg saves. */
2005
2006 static rtx_insn *
2007 rs6000_frame_related (rtx_insn *insn, rtx reg, HOST_WIDE_INT val,
2008 rtx reg2, rtx repl2)
2009 {
2010 rtx repl;
2011
2012 if (REGNO (reg) == STACK_POINTER_REGNUM)
2013 {
2014 gcc_checking_assert (val == 0);
2015 repl = NULL_RTX;
2016 }
2017 else
2018 repl = gen_rtx_PLUS (Pmode, gen_rtx_REG (Pmode, STACK_POINTER_REGNUM),
2019 GEN_INT (val));
2020
2021 rtx pat = PATTERN (insn);
2022 if (!repl && !reg2)
2023 {
2024 /* No need for any replacement. Just set RTX_FRAME_RELATED_P. */
2025 if (GET_CODE (pat) == PARALLEL)
2026 for (int i = 0; i < XVECLEN (pat, 0); i++)
2027 if (GET_CODE (XVECEXP (pat, 0, i)) == SET)
2028 {
2029 rtx set = XVECEXP (pat, 0, i);
2030
2031 if (!REG_P (SET_SRC (set))
2032 || interesting_frame_related_regno (REGNO (SET_SRC (set))))
2033 RTX_FRAME_RELATED_P (set) = 1;
2034 }
2035 RTX_FRAME_RELATED_P (insn) = 1;
2036 return insn;
2037 }
2038
2039 /* We expect that 'pat' is either a SET or a PARALLEL containing
2040 SETs (and possibly other stuff). In a PARALLEL, all the SETs
2041 are important so they all have to be marked RTX_FRAME_RELATED_P.
2042 Call simplify_replace_rtx on the SETs rather than the whole insn
2043 so as to leave the other stuff alone (for example USE of r12). */
2044
2045 set_used_flags (pat);
2046 if (GET_CODE (pat) == SET)
2047 {
2048 if (repl)
2049 pat = simplify_replace_rtx (pat, reg, repl);
2050 if (reg2)
2051 pat = simplify_replace_rtx (pat, reg2, repl2);
2052 }
2053 else if (GET_CODE (pat) == PARALLEL)
2054 {
2055 pat = shallow_copy_rtx (pat);
2056 XVEC (pat, 0) = shallow_copy_rtvec (XVEC (pat, 0));
2057
2058 for (int i = 0; i < XVECLEN (pat, 0); i++)
2059 if (GET_CODE (XVECEXP (pat, 0, i)) == SET)
2060 {
2061 rtx set = XVECEXP (pat, 0, i);
2062
2063 if (repl)
2064 set = simplify_replace_rtx (set, reg, repl);
2065 if (reg2)
2066 set = simplify_replace_rtx (set, reg2, repl2);
2067 XVECEXP (pat, 0, i) = set;
2068
2069 if (!REG_P (SET_SRC (set))
2070 || interesting_frame_related_regno (REGNO (SET_SRC (set))))
2071 RTX_FRAME_RELATED_P (set) = 1;
2072 }
2073 }
2074 else
2075 gcc_unreachable ();
2076
2077 RTX_FRAME_RELATED_P (insn) = 1;
2078 add_reg_note (insn, REG_FRAME_RELATED_EXPR, copy_rtx_if_shared (pat));
2079
2080 return insn;
2081 }
2082
2083 /* Returns an insn that has a vrsave set operation with the
2084 appropriate CLOBBERs. */
2085
2086 static rtx
2087 generate_set_vrsave (rtx reg, rs6000_stack_t *info, int epiloguep)
2088 {
2089 int nclobs, i;
2090 rtx insn, clobs[TOTAL_ALTIVEC_REGS + 1];
2091 rtx vrsave = gen_rtx_REG (SImode, VRSAVE_REGNO);
2092
2093 clobs[0]
2094 = gen_rtx_SET (vrsave,
2095 gen_rtx_UNSPEC_VOLATILE (SImode,
2096 gen_rtvec (2, reg, vrsave),
2097 UNSPECV_SET_VRSAVE));
2098
2099 nclobs = 1;
2100
2101 /* We need to clobber the registers in the mask so the scheduler
2102 does not move sets to VRSAVE before sets of AltiVec registers.
2103
2104 However, if the function receives nonlocal gotos, reload will set
2105 all call saved registers live. We will end up with:
2106
2107 (set (reg 999) (mem))
2108 (parallel [ (set (reg vrsave) (unspec blah))
2109 (clobber (reg 999))])
2110
2111 The clobber will cause the store into reg 999 to be dead, and
2112 flow will attempt to delete an epilogue insn. In this case, we
2113 need an unspec use/set of the register. */
2114
2115 for (i = FIRST_ALTIVEC_REGNO; i <= LAST_ALTIVEC_REGNO; ++i)
2116 if (info->vrsave_mask & ALTIVEC_REG_BIT (i))
2117 {
2118 if (!epiloguep || call_used_or_fixed_reg_p (i))
2119 clobs[nclobs++] = gen_hard_reg_clobber (V4SImode, i);
2120 else
2121 {
2122 rtx reg = gen_rtx_REG (V4SImode, i);
2123
2124 clobs[nclobs++]
2125 = gen_rtx_SET (reg,
2126 gen_rtx_UNSPEC (V4SImode,
2127 gen_rtvec (1, reg), 27));
2128 }
2129 }
2130
2131 insn = gen_rtx_PARALLEL (VOIDmode, rtvec_alloc (nclobs));
2132
2133 for (i = 0; i < nclobs; ++i)
2134 XVECEXP (insn, 0, i) = clobs[i];
2135
2136 return insn;
2137 }
2138
2139 static rtx
2140 gen_frame_set (rtx reg, rtx frame_reg, int offset, bool store)
2141 {
2142 rtx addr, mem;
2143
2144 addr = gen_rtx_PLUS (Pmode, frame_reg, GEN_INT (offset));
2145 mem = gen_frame_mem (GET_MODE (reg), addr);
2146 return gen_rtx_SET (store ? mem : reg, store ? reg : mem);
2147 }
2148
2149 static rtx
2150 gen_frame_load (rtx reg, rtx frame_reg, int offset)
2151 {
2152 return gen_frame_set (reg, frame_reg, offset, false);
2153 }
2154
2155 static rtx
2156 gen_frame_store (rtx reg, rtx frame_reg, int offset)
2157 {
2158 return gen_frame_set (reg, frame_reg, offset, true);
2159 }
2160
2161 /* Save a register into the frame, and emit RTX_FRAME_RELATED_P notes.
2162 Save REGNO into [FRAME_REG + OFFSET] in mode MODE. */
2163
2164 static rtx_insn *
2165 emit_frame_save (rtx frame_reg, machine_mode mode,
2166 unsigned int regno, int offset, HOST_WIDE_INT frame_reg_to_sp)
2167 {
2168 rtx reg;
2169
2170 /* Some cases that need register indexed addressing. */
2171 gcc_checking_assert (!(TARGET_ALTIVEC_ABI && ALTIVEC_VECTOR_MODE (mode))
2172 || (TARGET_VSX && ALTIVEC_OR_VSX_VECTOR_MODE (mode)));
2173
2174 reg = gen_rtx_REG (mode, regno);
2175 rtx_insn *insn = emit_insn (gen_frame_store (reg, frame_reg, offset));
2176 return rs6000_frame_related (insn, frame_reg, frame_reg_to_sp,
2177 NULL_RTX, NULL_RTX);
2178 }
2179
2180 /* Emit an offset memory reference suitable for a frame store, while
2181 converting to a valid addressing mode. */
2182
2183 static rtx
2184 gen_frame_mem_offset (machine_mode mode, rtx reg, int offset)
2185 {
2186 return gen_frame_mem (mode, gen_rtx_PLUS (Pmode, reg, GEN_INT (offset)));
2187 }
2188
2189 #ifndef TARGET_FIX_AND_CONTINUE
2190 #define TARGET_FIX_AND_CONTINUE 0
2191 #endif
2192
2193 /* It's really GPR 13 or 14, FPR 14 and VR 20. We need the smallest. */
2194 #define FIRST_SAVRES_REGISTER FIRST_SAVED_GP_REGNO
2195 #define LAST_SAVRES_REGISTER 31
2196 #define N_SAVRES_REGISTERS (LAST_SAVRES_REGISTER - FIRST_SAVRES_REGISTER + 1)
2197
2198 enum {
2199 SAVRES_LR = 0x1,
2200 SAVRES_SAVE = 0x2,
2201 SAVRES_REG = 0x0c,
2202 SAVRES_GPR = 0,
2203 SAVRES_FPR = 4,
2204 SAVRES_VR = 8
2205 };
2206
2207 static GTY(()) rtx savres_routine_syms[N_SAVRES_REGISTERS][12];
2208
2209 /* Temporary holding space for an out-of-line register save/restore
2210 routine name. */
2211 static char savres_routine_name[30];
2212
2213 /* Return the name for an out-of-line register save/restore routine.
2214 We are saving/restoring GPRs if GPR is true. */
2215
2216 static char *
2217 rs6000_savres_routine_name (int regno, int sel)
2218 {
2219 const char *prefix = "";
2220 const char *suffix = "";
2221
2222 /* Different targets are supposed to define
2223 {SAVE,RESTORE}_FP_{PREFIX,SUFFIX} with the idea that the needed
2224 routine name could be defined with:
2225
2226 sprintf (name, "%s%d%s", SAVE_FP_PREFIX, regno, SAVE_FP_SUFFIX)
2227
2228 This is a nice idea in practice, but in reality, things are
2229 complicated in several ways:
2230
2231 - ELF targets have save/restore routines for GPRs.
2232
2233 - PPC64 ELF targets have routines for save/restore of GPRs that
2234 differ in what they do with the link register, so having a set
2235 prefix doesn't work. (We only use one of the save routines at
2236 the moment, though.)
2237
2238 - PPC32 elf targets have "exit" versions of the restore routines
2239 that restore the link register and can save some extra space.
2240 These require an extra suffix. (There are also "tail" versions
2241 of the restore routines and "GOT" versions of the save routines,
2242 but we don't generate those at present. Same problems apply,
2243 though.)
2244
2245 We deal with all this by synthesizing our own prefix/suffix and
2246 using that for the simple sprintf call shown above. */
2247 if (DEFAULT_ABI == ABI_V4)
2248 {
2249 if (TARGET_64BIT)
2250 goto aix_names;
2251
2252 if ((sel & SAVRES_REG) == SAVRES_GPR)
2253 prefix = (sel & SAVRES_SAVE) ? "_savegpr_" : "_restgpr_";
2254 else if ((sel & SAVRES_REG) == SAVRES_FPR)
2255 prefix = (sel & SAVRES_SAVE) ? "_savefpr_" : "_restfpr_";
2256 else if ((sel & SAVRES_REG) == SAVRES_VR)
2257 prefix = (sel & SAVRES_SAVE) ? "_savevr_" : "_restvr_";
2258 else
2259 abort ();
2260
2261 if ((sel & SAVRES_LR))
2262 suffix = "_x";
2263 }
2264 else if (DEFAULT_ABI == ABI_AIX || DEFAULT_ABI == ABI_ELFv2)
2265 {
2266 #if !defined (POWERPC_LINUX) && !defined (POWERPC_FREEBSD)
2267 /* No out-of-line save/restore routines for GPRs on AIX. */
2268 gcc_assert (!TARGET_AIX || (sel & SAVRES_REG) != SAVRES_GPR);
2269 #endif
2270
2271 aix_names:
2272 if ((sel & SAVRES_REG) == SAVRES_GPR)
2273 prefix = ((sel & SAVRES_SAVE)
2274 ? ((sel & SAVRES_LR) ? "_savegpr0_" : "_savegpr1_")
2275 : ((sel & SAVRES_LR) ? "_restgpr0_" : "_restgpr1_"));
2276 else if ((sel & SAVRES_REG) == SAVRES_FPR)
2277 {
2278 #if defined (POWERPC_LINUX) || defined (POWERPC_FREEBSD)
2279 if ((sel & SAVRES_LR))
2280 prefix = ((sel & SAVRES_SAVE) ? "_savefpr_" : "_restfpr_");
2281 else
2282 #endif
2283 {
2284 prefix = (sel & SAVRES_SAVE) ? SAVE_FP_PREFIX : RESTORE_FP_PREFIX;
2285 suffix = (sel & SAVRES_SAVE) ? SAVE_FP_SUFFIX : RESTORE_FP_SUFFIX;
2286 }
2287 }
2288 else if ((sel & SAVRES_REG) == SAVRES_VR)
2289 prefix = (sel & SAVRES_SAVE) ? "_savevr_" : "_restvr_";
2290 else
2291 abort ();
2292 }
2293
2294 if (DEFAULT_ABI == ABI_DARWIN)
2295 {
2296 /* The Darwin approach is (slightly) different, in order to be
2297 compatible with code generated by the system toolchain. There is a
2298 single symbol for the start of save sequence, and the code here
2299 embeds an offset into that code on the basis of the first register
2300 to be saved. */
2301 prefix = (sel & SAVRES_SAVE) ? "save" : "rest" ;
2302 if ((sel & SAVRES_REG) == SAVRES_GPR)
2303 sprintf (savres_routine_name, "*%sGPR%s%s%.0d ; %s r%d-r31", prefix,
2304 ((sel & SAVRES_LR) ? "x" : ""), (regno == 13 ? "" : "+"),
2305 (regno - 13) * 4, prefix, regno);
2306 else if ((sel & SAVRES_REG) == SAVRES_FPR)
2307 sprintf (savres_routine_name, "*%sFP%s%.0d ; %s f%d-f31", prefix,
2308 (regno == 14 ? "" : "+"), (regno - 14) * 4, prefix, regno);
2309 else if ((sel & SAVRES_REG) == SAVRES_VR)
2310 sprintf (savres_routine_name, "*%sVEC%s%.0d ; %s v%d-v31", prefix,
2311 (regno == 20 ? "" : "+"), (regno - 20) * 8, prefix, regno);
2312 else
2313 abort ();
2314 }
2315 else
2316 sprintf (savres_routine_name, "%s%d%s", prefix, regno, suffix);
2317
2318 return savres_routine_name;
2319 }
2320
2321 /* Return an RTL SYMBOL_REF for an out-of-line register save/restore routine.
2322 We are saving/restoring GPRs if GPR is true. */
2323
2324 static rtx
2325 rs6000_savres_routine_sym (rs6000_stack_t *info, int sel)
2326 {
2327 int regno = ((sel & SAVRES_REG) == SAVRES_GPR
2328 ? info->first_gp_reg_save
2329 : (sel & SAVRES_REG) == SAVRES_FPR
2330 ? info->first_fp_reg_save - 32
2331 : (sel & SAVRES_REG) == SAVRES_VR
2332 ? info->first_altivec_reg_save - FIRST_ALTIVEC_REGNO
2333 : -1);
2334 rtx sym;
2335 int select = sel;
2336
2337 /* Don't generate bogus routine names. */
2338 gcc_assert (FIRST_SAVRES_REGISTER <= regno
2339 && regno <= LAST_SAVRES_REGISTER
2340 && select >= 0 && select <= 12);
2341
2342 sym = savres_routine_syms[regno-FIRST_SAVRES_REGISTER][select];
2343
2344 if (sym == NULL)
2345 {
2346 char *name;
2347
2348 name = rs6000_savres_routine_name (regno, sel);
2349
2350 sym = savres_routine_syms[regno-FIRST_SAVRES_REGISTER][select]
2351 = gen_rtx_SYMBOL_REF (Pmode, ggc_strdup (name));
2352 SYMBOL_REF_FLAGS (sym) |= SYMBOL_FLAG_FUNCTION;
2353 }
2354
2355 return sym;
2356 }
2357
2358 /* Emit a sequence of insns, including a stack tie if needed, for
2359 resetting the stack pointer. If UPDT_REGNO is not 1, then don't
2360 reset the stack pointer, but move the base of the frame into
2361 reg UPDT_REGNO for use by out-of-line register restore routines. */
2362
2363 static rtx
2364 rs6000_emit_stack_reset (rtx frame_reg_rtx, HOST_WIDE_INT frame_off,
2365 unsigned updt_regno)
2366 {
2367 /* If there is nothing to do, don't do anything. */
2368 if (frame_off == 0 && REGNO (frame_reg_rtx) == updt_regno)
2369 return NULL_RTX;
2370
2371 rtx updt_reg_rtx = gen_rtx_REG (Pmode, updt_regno);
2372
2373 /* This blockage is needed so that sched doesn't decide to move
2374 the sp change before the register restores. */
2375 if (DEFAULT_ABI == ABI_V4)
2376 return emit_insn (gen_stack_restore_tie (updt_reg_rtx, frame_reg_rtx,
2377 GEN_INT (frame_off)));
2378
2379 /* If we are restoring registers out-of-line, we will be using the
2380 "exit" variants of the restore routines, which will reset the
2381 stack for us. But we do need to point updt_reg into the
2382 right place for those routines. */
2383 if (frame_off != 0)
2384 return emit_insn (gen_add3_insn (updt_reg_rtx,
2385 frame_reg_rtx, GEN_INT (frame_off)));
2386 else
2387 return emit_move_insn (updt_reg_rtx, frame_reg_rtx);
2388
2389 return NULL_RTX;
2390 }
2391
2392 /* Return the register number used as a pointer by out-of-line
2393 save/restore functions. */
2394
2395 static inline unsigned
2396 ptr_regno_for_savres (int sel)
2397 {
2398 if (DEFAULT_ABI == ABI_AIX || DEFAULT_ABI == ABI_ELFv2)
2399 return (sel & SAVRES_REG) == SAVRES_FPR || (sel & SAVRES_LR) ? 1 : 12;
2400 return DEFAULT_ABI == ABI_DARWIN && (sel & SAVRES_REG) == SAVRES_FPR ? 1 : 11;
2401 }
2402
2403 /* Construct a parallel rtx describing the effect of a call to an
2404 out-of-line register save/restore routine, and emit the insn
2405 or jump_insn as appropriate. */
2406
2407 static rtx_insn *
2408 rs6000_emit_savres_rtx (rs6000_stack_t *info,
2409 rtx frame_reg_rtx, int save_area_offset, int lr_offset,
2410 machine_mode reg_mode, int sel)
2411 {
2412 int i;
2413 int offset, start_reg, end_reg, n_regs, use_reg;
2414 int reg_size = GET_MODE_SIZE (reg_mode);
2415 rtx sym;
2416 rtvec p;
2417 rtx par;
2418 rtx_insn *insn;
2419
2420 offset = 0;
2421 start_reg = ((sel & SAVRES_REG) == SAVRES_GPR
2422 ? info->first_gp_reg_save
2423 : (sel & SAVRES_REG) == SAVRES_FPR
2424 ? info->first_fp_reg_save
2425 : (sel & SAVRES_REG) == SAVRES_VR
2426 ? info->first_altivec_reg_save
2427 : -1);
2428 end_reg = ((sel & SAVRES_REG) == SAVRES_GPR
2429 ? 32
2430 : (sel & SAVRES_REG) == SAVRES_FPR
2431 ? 64
2432 : (sel & SAVRES_REG) == SAVRES_VR
2433 ? LAST_ALTIVEC_REGNO + 1
2434 : -1);
2435 n_regs = end_reg - start_reg;
2436 p = rtvec_alloc (3 + ((sel & SAVRES_LR) ? 1 : 0)
2437 + ((sel & SAVRES_REG) == SAVRES_VR ? 1 : 0)
2438 + n_regs);
2439
2440 if (!(sel & SAVRES_SAVE) && (sel & SAVRES_LR))
2441 RTVEC_ELT (p, offset++) = ret_rtx;
2442
2443 RTVEC_ELT (p, offset++) = gen_hard_reg_clobber (Pmode, LR_REGNO);
2444
2445 sym = rs6000_savres_routine_sym (info, sel);
2446 RTVEC_ELT (p, offset++) = gen_rtx_USE (VOIDmode, sym);
2447
2448 use_reg = ptr_regno_for_savres (sel);
2449 if ((sel & SAVRES_REG) == SAVRES_VR)
2450 {
2451 /* Vector regs are saved/restored using [reg+reg] addressing. */
2452 RTVEC_ELT (p, offset++) = gen_hard_reg_clobber (Pmode, use_reg);
2453 RTVEC_ELT (p, offset++)
2454 = gen_rtx_USE (VOIDmode, gen_rtx_REG (Pmode, 0));
2455 }
2456 else
2457 RTVEC_ELT (p, offset++)
2458 = gen_rtx_USE (VOIDmode, gen_rtx_REG (Pmode, use_reg));
2459
2460 for (i = 0; i < end_reg - start_reg; i++)
2461 RTVEC_ELT (p, i + offset)
2462 = gen_frame_set (gen_rtx_REG (reg_mode, start_reg + i),
2463 frame_reg_rtx, save_area_offset + reg_size * i,
2464 (sel & SAVRES_SAVE) != 0);
2465
2466 if ((sel & SAVRES_SAVE) && (sel & SAVRES_LR))
2467 RTVEC_ELT (p, i + offset)
2468 = gen_frame_store (gen_rtx_REG (Pmode, 0), frame_reg_rtx, lr_offset);
2469
2470 par = gen_rtx_PARALLEL (VOIDmode, p);
2471
2472 if (!(sel & SAVRES_SAVE) && (sel & SAVRES_LR))
2473 {
2474 insn = emit_jump_insn (par);
2475 JUMP_LABEL (insn) = ret_rtx;
2476 }
2477 else
2478 insn = emit_insn (par);
2479 return insn;
2480 }
2481
2482 /* Emit prologue code to store CR fields that need to be saved into REG. This
2483 function should only be called when moving the non-volatile CRs to REG, it
2484 is not a general purpose routine to move the entire set of CRs to REG.
2485 Specifically, gen_prologue_movesi_from_cr() does not contain uses of the
2486 volatile CRs. */
2487
2488 static void
2489 rs6000_emit_prologue_move_from_cr (rtx reg)
2490 {
2491 /* Only the ELFv2 ABI allows storing only selected fields. */
2492 if (DEFAULT_ABI == ABI_ELFv2 && TARGET_MFCRF)
2493 {
2494 int i, cr_reg[8], count = 0;
2495
2496 /* Collect CR fields that must be saved. */
2497 for (i = 0; i < 8; i++)
2498 if (save_reg_p (CR0_REGNO + i))
2499 cr_reg[count++] = i;
2500
2501 /* If it's just a single one, use mfcrf. */
2502 if (count == 1)
2503 {
2504 rtvec p = rtvec_alloc (1);
2505 rtvec r = rtvec_alloc (2);
2506 RTVEC_ELT (r, 0) = gen_rtx_REG (CCmode, CR0_REGNO + cr_reg[0]);
2507 RTVEC_ELT (r, 1) = GEN_INT (1 << (7 - cr_reg[0]));
2508 RTVEC_ELT (p, 0)
2509 = gen_rtx_SET (reg,
2510 gen_rtx_UNSPEC (SImode, r, UNSPEC_MOVESI_FROM_CR));
2511
2512 emit_insn (gen_rtx_PARALLEL (VOIDmode, p));
2513 return;
2514 }
2515
2516 /* ??? It might be better to handle count == 2 / 3 cases here
2517 as well, using logical operations to combine the values. */
2518 }
2519
2520 emit_insn (gen_prologue_movesi_from_cr (reg));
2521 }
2522
2523 /* Return whether the split-stack arg pointer (r12) is used. */
2524
2525 static bool
2526 split_stack_arg_pointer_used_p (void)
2527 {
2528 /* If the pseudo holding the arg pointer is no longer a pseudo,
2529 then the arg pointer is used. */
2530 if (cfun->machine->split_stack_arg_pointer != NULL_RTX
2531 && (!REG_P (cfun->machine->split_stack_arg_pointer)
2532 || HARD_REGISTER_P (cfun->machine->split_stack_arg_pointer)))
2533 return true;
2534
2535 /* Unfortunately we also need to do some code scanning, since
2536 r12 may have been substituted for the pseudo. */
2537 rtx_insn *insn;
2538 basic_block bb = ENTRY_BLOCK_PTR_FOR_FN (cfun)->next_bb;
2539 FOR_BB_INSNS (bb, insn)
2540 if (NONDEBUG_INSN_P (insn))
2541 {
2542 /* A call destroys r12. */
2543 if (CALL_P (insn))
2544 return false;
2545
2546 df_ref use;
2547 FOR_EACH_INSN_USE (use, insn)
2548 {
2549 rtx x = DF_REF_REG (use);
2550 if (REG_P (x) && REGNO (x) == 12)
2551 return true;
2552 }
2553 df_ref def;
2554 FOR_EACH_INSN_DEF (def, insn)
2555 {
2556 rtx x = DF_REF_REG (def);
2557 if (REG_P (x) && REGNO (x) == 12)
2558 return false;
2559 }
2560 }
2561 return bitmap_bit_p (DF_LR_OUT (bb), 12);
2562 }
2563
2564 /* Return whether we need to emit an ELFv2 global entry point prologue. */
2565
2566 bool
2567 rs6000_global_entry_point_prologue_needed_p (void)
2568 {
2569 /* Only needed for the ELFv2 ABI. */
2570 if (DEFAULT_ABI != ABI_ELFv2)
2571 return false;
2572
2573 /* With -msingle-pic-base, we assume the whole program shares the same
2574 TOC, so no global entry point prologues are needed anywhere. */
2575 if (TARGET_SINGLE_PIC_BASE)
2576 return false;
2577
2578 /* PC-relative functions never generate a global entry point prologue. */
2579 if (rs6000_pcrel_p ())
2580 return false;
2581
2582 /* Ensure we have a global entry point for thunks. ??? We could
2583 avoid that if the target routine doesn't need a global entry point,
2584 but we do not know whether this is the case at this point. */
2585 if (cfun->is_thunk)
2586 return true;
2587
2588 /* For regular functions, rs6000_emit_prologue sets this flag if the
2589 routine ever uses the TOC pointer. */
2590 return cfun->machine->r2_setup_needed;
2591 }
2592
2593 /* Implement TARGET_SHRINK_WRAP_GET_SEPARATE_COMPONENTS. */
2594 sbitmap
2595 rs6000_get_separate_components (void)
2596 {
2597 rs6000_stack_t *info = rs6000_stack_info ();
2598
2599 if (WORLD_SAVE_P (info))
2600 return NULL;
2601
2602 gcc_assert (!(info->savres_strategy & SAVE_MULTIPLE)
2603 && !(info->savres_strategy & REST_MULTIPLE));
2604
2605 /* Component 0 is the save/restore of LR (done via GPR0).
2606 Component 2 is the save of the TOC (GPR2).
2607 Components 13..31 are the save/restore of GPR13..GPR31.
2608 Components 46..63 are the save/restore of FPR14..FPR31. */
2609
2610 cfun->machine->n_components = 64;
2611
2612 sbitmap components = sbitmap_alloc (cfun->machine->n_components);
2613 bitmap_clear (components);
2614
2615 int reg_size = TARGET_32BIT ? 4 : 8;
2616 int fp_reg_size = 8;
2617
2618 /* The GPRs we need saved to the frame. */
2619 if ((info->savres_strategy & SAVE_INLINE_GPRS)
2620 && (info->savres_strategy & REST_INLINE_GPRS))
2621 {
2622 int offset = info->gp_save_offset;
2623 if (info->push_p)
2624 offset += info->total_size;
2625
2626 for (unsigned regno = info->first_gp_reg_save; regno < 32; regno++)
2627 {
2628 if (IN_RANGE (offset, -0x8000, 0x7fff)
2629 && save_reg_p (regno))
2630 bitmap_set_bit (components, regno);
2631
2632 offset += reg_size;
2633 }
2634 }
2635
2636 /* Don't mess with the hard frame pointer. */
2637 if (frame_pointer_needed)
2638 bitmap_clear_bit (components, HARD_FRAME_POINTER_REGNUM);
2639
2640 /* Don't mess with the fixed TOC register. */
2641 if ((TARGET_TOC && TARGET_MINIMAL_TOC)
2642 || (flag_pic == 1 && DEFAULT_ABI == ABI_V4)
2643 || (flag_pic && DEFAULT_ABI == ABI_DARWIN))
2644 bitmap_clear_bit (components, RS6000_PIC_OFFSET_TABLE_REGNUM);
2645
2646 /* The FPRs we need saved to the frame. */
2647 if ((info->savres_strategy & SAVE_INLINE_FPRS)
2648 && (info->savres_strategy & REST_INLINE_FPRS))
2649 {
2650 int offset = info->fp_save_offset;
2651 if (info->push_p)
2652 offset += info->total_size;
2653
2654 for (unsigned regno = info->first_fp_reg_save; regno < 64; regno++)
2655 {
2656 if (IN_RANGE (offset, -0x8000, 0x7fff) && save_reg_p (regno))
2657 bitmap_set_bit (components, regno);
2658
2659 offset += fp_reg_size;
2660 }
2661 }
2662
2663 /* Optimize LR save and restore if we can. This is component 0. Any
2664 out-of-line register save/restore routines need LR. */
2665 if (info->lr_save_p
2666 && !(flag_pic && (DEFAULT_ABI == ABI_V4 || DEFAULT_ABI == ABI_DARWIN))
2667 && (info->savres_strategy & SAVE_INLINE_GPRS)
2668 && (info->savres_strategy & REST_INLINE_GPRS)
2669 && (info->savres_strategy & SAVE_INLINE_FPRS)
2670 && (info->savres_strategy & REST_INLINE_FPRS)
2671 && (info->savres_strategy & SAVE_INLINE_VRS)
2672 && (info->savres_strategy & REST_INLINE_VRS))
2673 {
2674 int offset = info->lr_save_offset;
2675 if (info->push_p)
2676 offset += info->total_size;
2677 if (IN_RANGE (offset, -0x8000, 0x7fff))
2678 bitmap_set_bit (components, 0);
2679 }
2680
2681 /* Optimize saving the TOC. This is component 2. */
2682 if (cfun->machine->save_toc_in_prologue)
2683 bitmap_set_bit (components, 2);
2684
2685 return components;
2686 }
2687
2688 /* Implement TARGET_SHRINK_WRAP_COMPONENTS_FOR_BB. */
2689 sbitmap
2690 rs6000_components_for_bb (basic_block bb)
2691 {
2692 rs6000_stack_t *info = rs6000_stack_info ();
2693
2694 bitmap in = DF_LIVE_IN (bb);
2695 bitmap gen = &DF_LIVE_BB_INFO (bb)->gen;
2696 bitmap kill = &DF_LIVE_BB_INFO (bb)->kill;
2697
2698 sbitmap components = sbitmap_alloc (cfun->machine->n_components);
2699 bitmap_clear (components);
2700
2701 /* A register is used in a bb if it is in the IN, GEN, or KILL sets. */
2702
2703 /* GPRs. */
2704 for (unsigned regno = info->first_gp_reg_save; regno < 32; regno++)
2705 if (bitmap_bit_p (in, regno)
2706 || bitmap_bit_p (gen, regno)
2707 || bitmap_bit_p (kill, regno))
2708 bitmap_set_bit (components, regno);
2709
2710 /* FPRs. */
2711 for (unsigned regno = info->first_fp_reg_save; regno < 64; 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);
2716
2717 /* The link register. */
2718 if (bitmap_bit_p (in, LR_REGNO)
2719 || bitmap_bit_p (gen, LR_REGNO)
2720 || bitmap_bit_p (kill, LR_REGNO))
2721 bitmap_set_bit (components, 0);
2722
2723 /* The TOC save. */
2724 if (bitmap_bit_p (in, TOC_REGNUM)
2725 || bitmap_bit_p (gen, TOC_REGNUM)
2726 || bitmap_bit_p (kill, TOC_REGNUM))
2727 bitmap_set_bit (components, 2);
2728
2729 return components;
2730 }
2731
2732 /* Implement TARGET_SHRINK_WRAP_DISQUALIFY_COMPONENTS. */
2733 void
2734 rs6000_disqualify_components (sbitmap components, edge e,
2735 sbitmap edge_components, bool /*is_prologue*/)
2736 {
2737 /* Our LR pro/epilogue code moves LR via R0, so R0 had better not be
2738 live where we want to place that code. */
2739 if (bitmap_bit_p (edge_components, 0)
2740 && bitmap_bit_p (DF_LIVE_IN (e->dest), 0))
2741 {
2742 if (dump_file)
2743 fprintf (dump_file, "Disqualifying LR because GPR0 is live "
2744 "on entry to bb %d\n", e->dest->index);
2745 bitmap_clear_bit (components, 0);
2746 }
2747 }
2748
2749 /* Implement TARGET_SHRINK_WRAP_EMIT_PROLOGUE_COMPONENTS. */
2750 void
2751 rs6000_emit_prologue_components (sbitmap components)
2752 {
2753 rs6000_stack_t *info = rs6000_stack_info ();
2754 rtx ptr_reg = gen_rtx_REG (Pmode, frame_pointer_needed_indeed
2755 ? HARD_FRAME_POINTER_REGNUM
2756 : STACK_POINTER_REGNUM);
2757
2758 machine_mode reg_mode = Pmode;
2759 int reg_size = TARGET_32BIT ? 4 : 8;
2760 machine_mode fp_reg_mode = TARGET_HARD_FLOAT ? DFmode : SFmode;
2761 int fp_reg_size = 8;
2762
2763 /* Prologue for LR. */
2764 if (bitmap_bit_p (components, 0))
2765 {
2766 rtx lr = gen_rtx_REG (reg_mode, LR_REGNO);
2767 rtx reg = gen_rtx_REG (reg_mode, 0);
2768 rtx_insn *insn = emit_move_insn (reg, lr);
2769 RTX_FRAME_RELATED_P (insn) = 1;
2770 add_reg_note (insn, REG_CFA_REGISTER, gen_rtx_SET (reg, lr));
2771
2772 int offset = info->lr_save_offset;
2773 if (info->push_p)
2774 offset += info->total_size;
2775
2776 insn = emit_insn (gen_frame_store (reg, ptr_reg, offset));
2777 RTX_FRAME_RELATED_P (insn) = 1;
2778 rtx mem = copy_rtx (SET_DEST (single_set (insn)));
2779 add_reg_note (insn, REG_CFA_OFFSET, gen_rtx_SET (mem, lr));
2780 }
2781
2782 /* Prologue for TOC. */
2783 if (bitmap_bit_p (components, 2))
2784 {
2785 rtx reg = gen_rtx_REG (reg_mode, TOC_REGNUM);
2786 rtx sp_reg = gen_rtx_REG (Pmode, STACK_POINTER_REGNUM);
2787 emit_insn (gen_frame_store (reg, sp_reg, RS6000_TOC_SAVE_SLOT));
2788 }
2789
2790 /* Prologue for the GPRs. */
2791 int offset = info->gp_save_offset;
2792 if (info->push_p)
2793 offset += info->total_size;
2794
2795 for (int i = info->first_gp_reg_save; i < 32; i++)
2796 {
2797 if (bitmap_bit_p (components, i))
2798 {
2799 rtx reg = gen_rtx_REG (reg_mode, i);
2800 rtx_insn *insn = emit_insn (gen_frame_store (reg, ptr_reg, offset));
2801 RTX_FRAME_RELATED_P (insn) = 1;
2802 rtx set = copy_rtx (single_set (insn));
2803 add_reg_note (insn, REG_CFA_OFFSET, set);
2804 }
2805
2806 offset += reg_size;
2807 }
2808
2809 /* Prologue for the FPRs. */
2810 offset = info->fp_save_offset;
2811 if (info->push_p)
2812 offset += info->total_size;
2813
2814 for (int i = info->first_fp_reg_save; i < 64; i++)
2815 {
2816 if (bitmap_bit_p (components, i))
2817 {
2818 rtx reg = gen_rtx_REG (fp_reg_mode, i);
2819 rtx_insn *insn = emit_insn (gen_frame_store (reg, ptr_reg, offset));
2820 RTX_FRAME_RELATED_P (insn) = 1;
2821 rtx set = copy_rtx (single_set (insn));
2822 add_reg_note (insn, REG_CFA_OFFSET, set);
2823 }
2824
2825 offset += fp_reg_size;
2826 }
2827 }
2828
2829 /* Implement TARGET_SHRINK_WRAP_EMIT_EPILOGUE_COMPONENTS. */
2830 void
2831 rs6000_emit_epilogue_components (sbitmap components)
2832 {
2833 rs6000_stack_t *info = rs6000_stack_info ();
2834 rtx ptr_reg = gen_rtx_REG (Pmode, frame_pointer_needed_indeed
2835 ? HARD_FRAME_POINTER_REGNUM
2836 : STACK_POINTER_REGNUM);
2837
2838 machine_mode reg_mode = Pmode;
2839 int reg_size = TARGET_32BIT ? 4 : 8;
2840
2841 machine_mode fp_reg_mode = TARGET_HARD_FLOAT ? DFmode : SFmode;
2842 int fp_reg_size = 8;
2843
2844 /* Epilogue for the FPRs. */
2845 int offset = info->fp_save_offset;
2846 if (info->push_p)
2847 offset += info->total_size;
2848
2849 for (int i = info->first_fp_reg_save; i < 64; i++)
2850 {
2851 if (bitmap_bit_p (components, i))
2852 {
2853 rtx reg = gen_rtx_REG (fp_reg_mode, i);
2854 rtx_insn *insn = emit_insn (gen_frame_load (reg, ptr_reg, offset));
2855 RTX_FRAME_RELATED_P (insn) = 1;
2856 add_reg_note (insn, REG_CFA_RESTORE, reg);
2857 }
2858
2859 offset += fp_reg_size;
2860 }
2861
2862 /* Epilogue for the GPRs. */
2863 offset = info->gp_save_offset;
2864 if (info->push_p)
2865 offset += info->total_size;
2866
2867 for (int i = info->first_gp_reg_save; i < 32; i++)
2868 {
2869 if (bitmap_bit_p (components, i))
2870 {
2871 rtx reg = gen_rtx_REG (reg_mode, i);
2872 rtx_insn *insn = emit_insn (gen_frame_load (reg, ptr_reg, offset));
2873 RTX_FRAME_RELATED_P (insn) = 1;
2874 add_reg_note (insn, REG_CFA_RESTORE, reg);
2875 }
2876
2877 offset += reg_size;
2878 }
2879
2880 /* Epilogue for LR. */
2881 if (bitmap_bit_p (components, 0))
2882 {
2883 int offset = info->lr_save_offset;
2884 if (info->push_p)
2885 offset += info->total_size;
2886
2887 rtx reg = gen_rtx_REG (reg_mode, 0);
2888 rtx_insn *insn = emit_insn (gen_frame_load (reg, ptr_reg, offset));
2889
2890 rtx lr = gen_rtx_REG (Pmode, LR_REGNO);
2891 insn = emit_move_insn (lr, reg);
2892 RTX_FRAME_RELATED_P (insn) = 1;
2893 add_reg_note (insn, REG_CFA_RESTORE, lr);
2894 }
2895 }
2896
2897 /* Implement TARGET_SHRINK_WRAP_SET_HANDLED_COMPONENTS. */
2898 void
2899 rs6000_set_handled_components (sbitmap components)
2900 {
2901 rs6000_stack_t *info = rs6000_stack_info ();
2902
2903 for (int i = info->first_gp_reg_save; i < 32; i++)
2904 if (bitmap_bit_p (components, i))
2905 cfun->machine->gpr_is_wrapped_separately[i] = true;
2906
2907 for (int i = info->first_fp_reg_save; i < 64; i++)
2908 if (bitmap_bit_p (components, i))
2909 cfun->machine->fpr_is_wrapped_separately[i - 32] = true;
2910
2911 if (bitmap_bit_p (components, 0))
2912 cfun->machine->lr_is_wrapped_separately = true;
2913
2914 if (bitmap_bit_p (components, 2))
2915 cfun->machine->toc_is_wrapped_separately = true;
2916 }
2917
2918 /* VRSAVE is a bit vector representing which AltiVec registers
2919 are used. The OS uses this to determine which vector
2920 registers to save on a context switch. We need to save
2921 VRSAVE on the stack frame, add whatever AltiVec registers we
2922 used in this function, and do the corresponding magic in the
2923 epilogue. */
2924 static void
2925 emit_vrsave_prologue (rs6000_stack_t *info, int save_regno,
2926 HOST_WIDE_INT frame_off, rtx frame_reg_rtx)
2927 {
2928 /* Get VRSAVE into a GPR. */
2929 rtx reg = gen_rtx_REG (SImode, save_regno);
2930 rtx vrsave = gen_rtx_REG (SImode, VRSAVE_REGNO);
2931 if (TARGET_MACHO)
2932 emit_insn (gen_get_vrsave_internal (reg));
2933 else
2934 emit_insn (gen_rtx_SET (reg, vrsave));
2935
2936 /* Save VRSAVE. */
2937 int offset = info->vrsave_save_offset + frame_off;
2938 emit_insn (gen_frame_store (reg, frame_reg_rtx, offset));
2939
2940 /* Include the registers in the mask. */
2941 emit_insn (gen_iorsi3 (reg, reg, GEN_INT (info->vrsave_mask)));
2942
2943 emit_insn (generate_set_vrsave (reg, info, 0));
2944 }
2945
2946 /* Set up the arg pointer (r12) for -fsplit-stack code. If __morestack was
2947 called, it left the arg pointer to the old stack in r29. Otherwise, the
2948 arg pointer is the top of the current frame. */
2949 static void
2950 emit_split_stack_prologue (rs6000_stack_t *info, rtx_insn *sp_adjust,
2951 HOST_WIDE_INT frame_off, rtx frame_reg_rtx)
2952 {
2953 cfun->machine->split_stack_argp_used = true;
2954
2955 if (sp_adjust)
2956 {
2957 rtx r12 = gen_rtx_REG (Pmode, 12);
2958 rtx sp_reg_rtx = gen_rtx_REG (Pmode, STACK_POINTER_REGNUM);
2959 rtx set_r12 = gen_rtx_SET (r12, sp_reg_rtx);
2960 emit_insn_before (set_r12, sp_adjust);
2961 }
2962 else if (frame_off != 0 || REGNO (frame_reg_rtx) != 12)
2963 {
2964 rtx r12 = gen_rtx_REG (Pmode, 12);
2965 if (frame_off == 0)
2966 emit_move_insn (r12, frame_reg_rtx);
2967 else
2968 emit_insn (gen_add3_insn (r12, frame_reg_rtx, GEN_INT (frame_off)));
2969 }
2970
2971 if (info->push_p)
2972 {
2973 rtx r12 = gen_rtx_REG (Pmode, 12);
2974 rtx r29 = gen_rtx_REG (Pmode, 29);
2975 rtx cr7 = gen_rtx_REG (CCUNSmode, CR7_REGNO);
2976 rtx not_more = gen_label_rtx ();
2977 rtx jump;
2978
2979 jump = gen_rtx_IF_THEN_ELSE (VOIDmode,
2980 gen_rtx_GEU (VOIDmode, cr7, const0_rtx),
2981 gen_rtx_LABEL_REF (VOIDmode, not_more),
2982 pc_rtx);
2983 jump = emit_jump_insn (gen_rtx_SET (pc_rtx, jump));
2984 JUMP_LABEL (jump) = not_more;
2985 LABEL_NUSES (not_more) += 1;
2986 emit_move_insn (r12, r29);
2987 emit_label (not_more);
2988 }
2989 }
2990
2991 /* Emit function prologue as insns. */
2992
2993 void
2994 rs6000_emit_prologue (void)
2995 {
2996 rs6000_stack_t *info = rs6000_stack_info ();
2997 machine_mode reg_mode = Pmode;
2998 int reg_size = TARGET_32BIT ? 4 : 8;
2999 machine_mode fp_reg_mode = TARGET_HARD_FLOAT ? DFmode : SFmode;
3000 int fp_reg_size = 8;
3001 rtx sp_reg_rtx = gen_rtx_REG (Pmode, STACK_POINTER_REGNUM);
3002 rtx frame_reg_rtx = sp_reg_rtx;
3003 unsigned int cr_save_regno;
3004 rtx cr_save_rtx = NULL_RTX;
3005 rtx_insn *insn;
3006 int strategy;
3007 int using_static_chain_p
3008 = (cfun->static_chain_decl != NULL_TREE
3009 && df_regs_ever_live_p (STATIC_CHAIN_REGNUM)
3010 && call_used_or_fixed_reg_p (STATIC_CHAIN_REGNUM));
3011 int using_split_stack = (flag_split_stack
3012 && (lookup_attribute ("no_split_stack",
3013 DECL_ATTRIBUTES (cfun->decl))
3014 == NULL));
3015 /* If we are inserting ROP-protect hash instructions, disable shrink-wrap
3016 until the bug where the hashst insn is emitted in the wrong location
3017 is fixed. See PR101324 for details. */
3018 if (info->rop_hash_size)
3019 flag_shrink_wrap = 0;
3020
3021 frame_pointer_needed_indeed
3022 = frame_pointer_needed && df_regs_ever_live_p (HARD_FRAME_POINTER_REGNUM);
3023
3024 /* Offset to top of frame for frame_reg and sp respectively. */
3025 HOST_WIDE_INT frame_off = 0;
3026 HOST_WIDE_INT sp_off = 0;
3027 /* sp_adjust is the stack adjusting instruction, tracked so that the
3028 insn setting up the split-stack arg pointer can be emitted just
3029 prior to it, when r12 is not used here for other purposes. */
3030 rtx_insn *sp_adjust = 0;
3031
3032 #if CHECKING_P
3033 /* Track and check usage of r0, r11, r12. */
3034 int reg_inuse = using_static_chain_p ? 1 << 11 : 0;
3035 #define START_USE(R) do \
3036 { \
3037 gcc_assert ((reg_inuse & (1 << (R))) == 0); \
3038 reg_inuse |= 1 << (R); \
3039 } while (0)
3040 #define END_USE(R) do \
3041 { \
3042 gcc_assert ((reg_inuse & (1 << (R))) != 0); \
3043 reg_inuse &= ~(1 << (R)); \
3044 } while (0)
3045 #define NOT_INUSE(R) do \
3046 { \
3047 gcc_assert ((reg_inuse & (1 << (R))) == 0); \
3048 } while (0)
3049 #else
3050 #define START_USE(R) do {} while (0)
3051 #define END_USE(R) do {} while (0)
3052 #define NOT_INUSE(R) do {} while (0)
3053 #endif
3054
3055 if (DEFAULT_ABI == ABI_ELFv2
3056 && !TARGET_SINGLE_PIC_BASE)
3057 {
3058 cfun->machine->r2_setup_needed = df_regs_ever_live_p (TOC_REGNUM);
3059
3060 /* With -mminimal-toc we may generate an extra use of r2 below. */
3061 if (TARGET_TOC && TARGET_MINIMAL_TOC
3062 && !constant_pool_empty_p ())
3063 cfun->machine->r2_setup_needed = true;
3064 }
3065
3066
3067 if (flag_stack_usage_info)
3068 current_function_static_stack_size = info->total_size;
3069
3070 if (flag_stack_check == STATIC_BUILTIN_STACK_CHECK)
3071 {
3072 HOST_WIDE_INT size = info->total_size;
3073
3074 if (crtl->is_leaf && !cfun->calls_alloca)
3075 {
3076 if (size > PROBE_INTERVAL && size > get_stack_check_protect ())
3077 rs6000_emit_probe_stack_range (get_stack_check_protect (),
3078 size - get_stack_check_protect ());
3079 }
3080 else if (size > 0)
3081 rs6000_emit_probe_stack_range (get_stack_check_protect (), size);
3082 }
3083
3084 if (TARGET_FIX_AND_CONTINUE)
3085 {
3086 /* gdb on darwin arranges to forward a function from the old
3087 address by modifying the first 5 instructions of the function
3088 to branch to the overriding function. This is necessary to
3089 permit function pointers that point to the old function to
3090 actually forward to the new function. */
3091 emit_insn (gen_nop ());
3092 emit_insn (gen_nop ());
3093 emit_insn (gen_nop ());
3094 emit_insn (gen_nop ());
3095 emit_insn (gen_nop ());
3096 }
3097
3098 /* Handle world saves specially here. */
3099 if (WORLD_SAVE_P (info))
3100 {
3101 int i, j, sz;
3102 rtx treg;
3103 rtvec p;
3104 rtx reg0;
3105
3106 /* save_world expects lr in r0. */
3107 reg0 = gen_rtx_REG (Pmode, 0);
3108 if (info->lr_save_p)
3109 {
3110 insn = emit_move_insn (reg0,
3111 gen_rtx_REG (Pmode, LR_REGNO));
3112 RTX_FRAME_RELATED_P (insn) = 1;
3113 }
3114
3115 /* The SAVE_WORLD and RESTORE_WORLD routines make a number of
3116 assumptions about the offsets of various bits of the stack
3117 frame. */
3118 gcc_assert (info->gp_save_offset == -220
3119 && info->fp_save_offset == -144
3120 && info->lr_save_offset == 8
3121 && info->cr_save_offset == 4
3122 && info->push_p
3123 && info->lr_save_p
3124 && (!crtl->calls_eh_return
3125 || info->ehrd_offset == -432)
3126 && info->vrsave_save_offset == -224
3127 && info->altivec_save_offset == -416);
3128
3129 treg = gen_rtx_REG (SImode, 11);
3130 emit_move_insn (treg, GEN_INT (-info->total_size));
3131
3132 /* SAVE_WORLD takes the caller's LR in R0 and the frame size
3133 in R11. It also clobbers R12, so beware! */
3134
3135 /* Preserve CR2 for save_world prologues */
3136 sz = 5;
3137 sz += 32 - info->first_gp_reg_save;
3138 sz += 64 - info->first_fp_reg_save;
3139 sz += LAST_ALTIVEC_REGNO - info->first_altivec_reg_save + 1;
3140 p = rtvec_alloc (sz);
3141 j = 0;
3142 RTVEC_ELT (p, j++) = gen_hard_reg_clobber (SImode, LR_REGNO);
3143 RTVEC_ELT (p, j++) = gen_rtx_USE (VOIDmode,
3144 gen_rtx_SYMBOL_REF (Pmode,
3145 "*save_world"));
3146 /* We do floats first so that the instruction pattern matches
3147 properly. */
3148 for (i = 0; i < 64 - info->first_fp_reg_save; i++)
3149 RTVEC_ELT (p, j++)
3150 = gen_frame_store (gen_rtx_REG (TARGET_HARD_FLOAT ? DFmode : SFmode,
3151 info->first_fp_reg_save + i),
3152 frame_reg_rtx,
3153 info->fp_save_offset + frame_off + 8 * i);
3154 for (i = 0; info->first_altivec_reg_save + i <= LAST_ALTIVEC_REGNO; i++)
3155 RTVEC_ELT (p, j++)
3156 = gen_frame_store (gen_rtx_REG (V4SImode,
3157 info->first_altivec_reg_save + i),
3158 frame_reg_rtx,
3159 info->altivec_save_offset + frame_off + 16 * i);
3160 for (i = 0; i < 32 - info->first_gp_reg_save; i++)
3161 RTVEC_ELT (p, j++)
3162 = gen_frame_store (gen_rtx_REG (reg_mode, info->first_gp_reg_save + i),
3163 frame_reg_rtx,
3164 info->gp_save_offset + frame_off + reg_size * i);
3165
3166 /* CR register traditionally saved as CR2. */
3167 RTVEC_ELT (p, j++)
3168 = gen_frame_store (gen_rtx_REG (SImode, CR2_REGNO),
3169 frame_reg_rtx, info->cr_save_offset + frame_off);
3170 /* Explain about use of R0. */
3171 if (info->lr_save_p)
3172 RTVEC_ELT (p, j++)
3173 = gen_frame_store (reg0,
3174 frame_reg_rtx, info->lr_save_offset + frame_off);
3175 /* Explain what happens to the stack pointer. */
3176 {
3177 rtx newval = gen_rtx_PLUS (Pmode, sp_reg_rtx, treg);
3178 RTVEC_ELT (p, j++) = gen_rtx_SET (sp_reg_rtx, newval);
3179 }
3180
3181 insn = emit_insn (gen_rtx_PARALLEL (VOIDmode, p));
3182 rs6000_frame_related (insn, frame_reg_rtx, sp_off - frame_off,
3183 treg, GEN_INT (-info->total_size));
3184 sp_off = frame_off = info->total_size;
3185 }
3186
3187 strategy = info->savres_strategy;
3188
3189 /* For V.4, update stack before we do any saving and set back pointer. */
3190 if (! WORLD_SAVE_P (info)
3191 && info->push_p
3192 && (DEFAULT_ABI == ABI_V4
3193 || crtl->calls_eh_return))
3194 {
3195 bool need_r11 = (!(strategy & SAVE_INLINE_FPRS)
3196 || !(strategy & SAVE_INLINE_GPRS)
3197 || !(strategy & SAVE_INLINE_VRS));
3198 int ptr_regno = -1;
3199 rtx ptr_reg = NULL_RTX;
3200 int ptr_off = 0;
3201
3202 if (info->total_size < 32767)
3203 frame_off = info->total_size;
3204 else if (need_r11)
3205 ptr_regno = 11;
3206 else if (info->cr_save_p
3207 || info->lr_save_p
3208 || info->first_fp_reg_save < 64
3209 || info->first_gp_reg_save < 32
3210 || info->altivec_size != 0
3211 || info->vrsave_size != 0
3212 || crtl->calls_eh_return)
3213 ptr_regno = 12;
3214 else
3215 {
3216 /* The prologue won't be saving any regs so there is no need
3217 to set up a frame register to access any frame save area.
3218 We also won't be using frame_off anywhere below, but set
3219 the correct value anyway to protect against future
3220 changes to this function. */
3221 frame_off = info->total_size;
3222 }
3223 if (ptr_regno != -1)
3224 {
3225 /* Set up the frame offset to that needed by the first
3226 out-of-line save function. */
3227 START_USE (ptr_regno);
3228 ptr_reg = gen_rtx_REG (Pmode, ptr_regno);
3229 frame_reg_rtx = ptr_reg;
3230 if (!(strategy & SAVE_INLINE_FPRS) && info->fp_size != 0)
3231 gcc_checking_assert (info->fp_save_offset + info->fp_size == 0);
3232 else if (!(strategy & SAVE_INLINE_GPRS) && info->first_gp_reg_save < 32)
3233 ptr_off = info->gp_save_offset + info->gp_size;
3234 else if (!(strategy & SAVE_INLINE_VRS) && info->altivec_size != 0)
3235 ptr_off = info->altivec_save_offset + info->altivec_size;
3236 frame_off = -ptr_off;
3237 }
3238 sp_adjust = rs6000_emit_allocate_stack (info->total_size,
3239 ptr_reg, ptr_off);
3240 if (REGNO (frame_reg_rtx) == 12)
3241 sp_adjust = 0;
3242 sp_off = info->total_size;
3243 if (frame_reg_rtx != sp_reg_rtx)
3244 rs6000_emit_stack_tie (frame_reg_rtx, false);
3245 }
3246
3247 /* If we use the link register, get it into r0. */
3248 if (!WORLD_SAVE_P (info) && info->lr_save_p
3249 && !cfun->machine->lr_is_wrapped_separately)
3250 {
3251 rtx reg;
3252
3253 reg = gen_rtx_REG (Pmode, 0);
3254 START_USE (0);
3255 insn = emit_move_insn (reg, gen_rtx_REG (Pmode, LR_REGNO));
3256 RTX_FRAME_RELATED_P (insn) = 1;
3257
3258 if (!(strategy & (SAVE_NOINLINE_GPRS_SAVES_LR
3259 | SAVE_NOINLINE_FPRS_SAVES_LR)))
3260 {
3261 insn = emit_insn (gen_frame_store (reg, frame_reg_rtx,
3262 info->lr_save_offset + frame_off));
3263 rs6000_frame_related (insn, frame_reg_rtx, sp_off - frame_off,
3264 NULL_RTX, NULL_RTX);
3265 END_USE (0);
3266 }
3267 }
3268
3269 /* The ROP hash store must occur before a stack frame is created,
3270 since the hash operates on r1. */
3271 /* NOTE: The hashst isn't needed if we're going to do a sibcall,
3272 but there's no way to know that here. Harmless except for
3273 performance, of course. */
3274 if (info->rop_hash_size)
3275 {
3276 rtx stack_ptr = gen_rtx_REG (Pmode, STACK_POINTER_REGNUM);
3277 rtx addr = gen_rtx_PLUS (Pmode, stack_ptr,
3278 GEN_INT (info->rop_hash_save_offset));
3279 rtx mem = gen_rtx_MEM (Pmode, addr);
3280 rtx reg0 = gen_rtx_REG (Pmode, 0);
3281 emit_insn (gen_hashst (mem, reg0));
3282 }
3283
3284 /* If we need to save CR, put it into r12 or r11. Choose r12 except when
3285 r12 will be needed by out-of-line gpr save. */
3286 if (DEFAULT_ABI == ABI_AIX
3287 && !(strategy & (SAVE_INLINE_GPRS | SAVE_NOINLINE_GPRS_SAVES_LR)))
3288 cr_save_regno = 11;
3289 else if (DEFAULT_ABI == ABI_ELFv2)
3290 cr_save_regno = 11;
3291 else
3292 cr_save_regno = 12;
3293 if (!WORLD_SAVE_P (info)
3294 && info->cr_save_p
3295 && REGNO (frame_reg_rtx) != cr_save_regno
3296 && !(using_static_chain_p && cr_save_regno == 11)
3297 && !(using_split_stack && cr_save_regno == 12 && sp_adjust))
3298 {
3299 cr_save_rtx = gen_rtx_REG (SImode, cr_save_regno);
3300 START_USE (cr_save_regno);
3301 rs6000_emit_prologue_move_from_cr (cr_save_rtx);
3302 }
3303
3304 /* Do any required saving of fpr's. If only one or two to save, do
3305 it ourselves. Otherwise, call function. */
3306 if (!WORLD_SAVE_P (info) && (strategy & SAVE_INLINE_FPRS))
3307 {
3308 int offset = info->fp_save_offset + frame_off;
3309 for (int i = info->first_fp_reg_save; i < 64; i++)
3310 {
3311 if (save_reg_p (i)
3312 && !cfun->machine->fpr_is_wrapped_separately[i - 32])
3313 emit_frame_save (frame_reg_rtx, fp_reg_mode, i, offset,
3314 sp_off - frame_off);
3315
3316 offset += fp_reg_size;
3317 }
3318 }
3319 else if (!WORLD_SAVE_P (info) && info->first_fp_reg_save != 64)
3320 {
3321 bool lr = (strategy & SAVE_NOINLINE_FPRS_SAVES_LR) != 0;
3322 int sel = SAVRES_SAVE | SAVRES_FPR | (lr ? SAVRES_LR : 0);
3323 unsigned ptr_regno = ptr_regno_for_savres (sel);
3324 rtx ptr_reg = frame_reg_rtx;
3325
3326 if (REGNO (frame_reg_rtx) == ptr_regno)
3327 gcc_checking_assert (frame_off == 0);
3328 else
3329 {
3330 ptr_reg = gen_rtx_REG (Pmode, ptr_regno);
3331 NOT_INUSE (ptr_regno);
3332 emit_insn (gen_add3_insn (ptr_reg,
3333 frame_reg_rtx, GEN_INT (frame_off)));
3334 }
3335 insn = rs6000_emit_savres_rtx (info, ptr_reg,
3336 info->fp_save_offset,
3337 info->lr_save_offset,
3338 DFmode, sel);
3339 rs6000_frame_related (insn, ptr_reg, sp_off,
3340 NULL_RTX, NULL_RTX);
3341 if (lr)
3342 END_USE (0);
3343 }
3344
3345 /* Save GPRs. This is done as a PARALLEL if we are using
3346 the store-multiple instructions. */
3347 if (!WORLD_SAVE_P (info) && !(strategy & SAVE_INLINE_GPRS))
3348 {
3349 bool lr = (strategy & SAVE_NOINLINE_GPRS_SAVES_LR) != 0;
3350 int sel = SAVRES_SAVE | SAVRES_GPR | (lr ? SAVRES_LR : 0);
3351 unsigned ptr_regno = ptr_regno_for_savres (sel);
3352 rtx ptr_reg = frame_reg_rtx;
3353 bool ptr_set_up = REGNO (ptr_reg) == ptr_regno;
3354 int end_save = info->gp_save_offset + info->gp_size;
3355 int ptr_off;
3356
3357 if (ptr_regno == 12)
3358 sp_adjust = 0;
3359 if (!ptr_set_up)
3360 ptr_reg = gen_rtx_REG (Pmode, ptr_regno);
3361
3362 /* Need to adjust r11 (r12) if we saved any FPRs. */
3363 if (end_save + frame_off != 0)
3364 {
3365 rtx offset = GEN_INT (end_save + frame_off);
3366
3367 if (ptr_set_up)
3368 frame_off = -end_save;
3369 else
3370 NOT_INUSE (ptr_regno);
3371 emit_insn (gen_add3_insn (ptr_reg, frame_reg_rtx, offset));
3372 }
3373 else if (!ptr_set_up)
3374 {
3375 NOT_INUSE (ptr_regno);
3376 emit_move_insn (ptr_reg, frame_reg_rtx);
3377 }
3378 ptr_off = -end_save;
3379 insn = rs6000_emit_savres_rtx (info, ptr_reg,
3380 info->gp_save_offset + ptr_off,
3381 info->lr_save_offset + ptr_off,
3382 reg_mode, sel);
3383 rs6000_frame_related (insn, ptr_reg, sp_off - ptr_off,
3384 NULL_RTX, NULL_RTX);
3385 if (lr)
3386 END_USE (0);
3387 }
3388 else if (!WORLD_SAVE_P (info) && (strategy & SAVE_MULTIPLE))
3389 {
3390 rtvec p;
3391 int i;
3392 p = rtvec_alloc (32 - info->first_gp_reg_save);
3393 for (i = 0; i < 32 - info->first_gp_reg_save; i++)
3394 RTVEC_ELT (p, i)
3395 = gen_frame_store (gen_rtx_REG (reg_mode, info->first_gp_reg_save + i),
3396 frame_reg_rtx,
3397 info->gp_save_offset + frame_off + reg_size * i);
3398 insn = emit_insn (gen_rtx_PARALLEL (VOIDmode, p));
3399 rs6000_frame_related (insn, frame_reg_rtx, sp_off - frame_off,
3400 NULL_RTX, NULL_RTX);
3401 }
3402 else if (!WORLD_SAVE_P (info))
3403 {
3404 int offset = info->gp_save_offset + frame_off;
3405 for (int i = info->first_gp_reg_save; i < 32; i++)
3406 {
3407 if (save_reg_p (i)
3408 && !cfun->machine->gpr_is_wrapped_separately[i])
3409 emit_frame_save (frame_reg_rtx, reg_mode, i, offset,
3410 sp_off - frame_off);
3411
3412 offset += reg_size;
3413 }
3414 }
3415
3416 if (crtl->calls_eh_return)
3417 {
3418 unsigned int i;
3419 rtvec p;
3420
3421 for (i = 0; ; ++i)
3422 {
3423 unsigned int regno = EH_RETURN_DATA_REGNO (i);
3424 if (regno == INVALID_REGNUM)
3425 break;
3426 }
3427
3428 p = rtvec_alloc (i);
3429
3430 for (i = 0; ; ++i)
3431 {
3432 unsigned int regno = EH_RETURN_DATA_REGNO (i);
3433 if (regno == INVALID_REGNUM)
3434 break;
3435
3436 rtx set
3437 = gen_frame_store (gen_rtx_REG (reg_mode, regno),
3438 sp_reg_rtx,
3439 info->ehrd_offset + sp_off + reg_size * (int) i);
3440 RTVEC_ELT (p, i) = set;
3441 RTX_FRAME_RELATED_P (set) = 1;
3442 }
3443
3444 insn = emit_insn (gen_blockage ());
3445 RTX_FRAME_RELATED_P (insn) = 1;
3446 add_reg_note (insn, REG_FRAME_RELATED_EXPR, gen_rtx_PARALLEL (VOIDmode, p));
3447 }
3448
3449 /* In AIX ABI we need to make sure r2 is really saved. */
3450 if (TARGET_AIX && crtl->calls_eh_return)
3451 {
3452 rtx tmp_reg, tmp_reg_si, hi, lo, compare_result, toc_save_done, jump;
3453 rtx join_insn, note;
3454 rtx_insn *save_insn;
3455 long toc_restore_insn;
3456
3457 tmp_reg = gen_rtx_REG (Pmode, 11);
3458 tmp_reg_si = gen_rtx_REG (SImode, 11);
3459 if (using_static_chain_p)
3460 {
3461 START_USE (0);
3462 emit_move_insn (gen_rtx_REG (Pmode, 0), tmp_reg);
3463 }
3464 else
3465 START_USE (11);
3466 emit_move_insn (tmp_reg, gen_rtx_REG (Pmode, LR_REGNO));
3467 /* Peek at instruction to which this function returns. If it's
3468 restoring r2, then we know we've already saved r2. We can't
3469 unconditionally save r2 because the value we have will already
3470 be updated if we arrived at this function via a plt call or
3471 toc adjusting stub. */
3472 emit_move_insn (tmp_reg_si, gen_rtx_MEM (SImode, tmp_reg));
3473 toc_restore_insn = ((TARGET_32BIT ? 0x80410000 : 0xE8410000)
3474 + RS6000_TOC_SAVE_SLOT);
3475 hi = gen_int_mode (toc_restore_insn & ~0xffff, SImode);
3476 emit_insn (gen_xorsi3 (tmp_reg_si, tmp_reg_si, hi));
3477 compare_result = gen_rtx_REG (CCUNSmode, CR0_REGNO);
3478 validate_condition_mode (EQ, CCUNSmode);
3479 lo = gen_int_mode (toc_restore_insn & 0xffff, SImode);
3480 emit_insn (gen_rtx_SET (compare_result,
3481 gen_rtx_COMPARE (CCUNSmode, tmp_reg_si, lo)));
3482 toc_save_done = gen_label_rtx ();
3483 jump = gen_rtx_IF_THEN_ELSE (VOIDmode,
3484 gen_rtx_EQ (VOIDmode, compare_result,
3485 const0_rtx),
3486 gen_rtx_LABEL_REF (VOIDmode, toc_save_done),
3487 pc_rtx);
3488 jump = emit_jump_insn (gen_rtx_SET (pc_rtx, jump));
3489 JUMP_LABEL (jump) = toc_save_done;
3490 LABEL_NUSES (toc_save_done) += 1;
3491
3492 save_insn = emit_frame_save (frame_reg_rtx, reg_mode,
3493 TOC_REGNUM, frame_off + RS6000_TOC_SAVE_SLOT,
3494 sp_off - frame_off);
3495
3496 emit_label (toc_save_done);
3497
3498 /* ??? If we leave SAVE_INSN as marked as saving R2, then we'll
3499 have a CFG that has different saves along different paths.
3500 Move the note to a dummy blockage insn, which describes that
3501 R2 is unconditionally saved after the label. */
3502 /* ??? An alternate representation might be a special insn pattern
3503 containing both the branch and the store. That might let the
3504 code that minimizes the number of DW_CFA_advance opcodes better
3505 freedom in placing the annotations. */
3506 note = find_reg_note (save_insn, REG_FRAME_RELATED_EXPR, NULL);
3507 if (note)
3508 remove_note (save_insn, note);
3509 else
3510 note = alloc_reg_note (REG_FRAME_RELATED_EXPR,
3511 copy_rtx (PATTERN (save_insn)), NULL_RTX);
3512 RTX_FRAME_RELATED_P (save_insn) = 0;
3513
3514 join_insn = emit_insn (gen_blockage ());
3515 REG_NOTES (join_insn) = note;
3516 RTX_FRAME_RELATED_P (join_insn) = 1;
3517
3518 if (using_static_chain_p)
3519 {
3520 emit_move_insn (tmp_reg, gen_rtx_REG (Pmode, 0));
3521 END_USE (0);
3522 }
3523 else
3524 END_USE (11);
3525 }
3526
3527 /* Save CR if we use any that must be preserved. */
3528 if (!WORLD_SAVE_P (info) && info->cr_save_p)
3529 {
3530 rtx addr = gen_rtx_PLUS (Pmode, frame_reg_rtx,
3531 GEN_INT (info->cr_save_offset + frame_off));
3532 rtx mem = gen_frame_mem (SImode, addr);
3533
3534 /* If we didn't copy cr before, do so now using r0. */
3535 if (cr_save_rtx == NULL_RTX)
3536 {
3537 START_USE (0);
3538 cr_save_rtx = gen_rtx_REG (SImode, 0);
3539 rs6000_emit_prologue_move_from_cr (cr_save_rtx);
3540 }
3541
3542 /* Saving CR requires a two-instruction sequence: one instruction
3543 to move the CR to a general-purpose register, and a second
3544 instruction that stores the GPR to memory.
3545
3546 We do not emit any DWARF CFI records for the first of these,
3547 because we cannot properly represent the fact that CR is saved in
3548 a register. One reason is that we cannot express that multiple
3549 CR fields are saved; another reason is that on 64-bit, the size
3550 of the CR register in DWARF (4 bytes) differs from the size of
3551 a general-purpose register.
3552
3553 This means if any intervening instruction were to clobber one of
3554 the call-saved CR fields, we'd have incorrect CFI. To prevent
3555 this from happening, we mark the store to memory as a use of
3556 those CR fields, which prevents any such instruction from being
3557 scheduled in between the two instructions. */
3558 rtx crsave_v[9];
3559 int n_crsave = 0;
3560 int i;
3561
3562 crsave_v[n_crsave++] = gen_rtx_SET (mem, cr_save_rtx);
3563 for (i = 0; i < 8; i++)
3564 if (save_reg_p (CR0_REGNO + i))
3565 crsave_v[n_crsave++]
3566 = gen_rtx_USE (VOIDmode, gen_rtx_REG (CCmode, CR0_REGNO + i));
3567
3568 insn = emit_insn (gen_rtx_PARALLEL (VOIDmode,
3569 gen_rtvec_v (n_crsave, crsave_v)));
3570 END_USE (REGNO (cr_save_rtx));
3571
3572 /* Now, there's no way that dwarf2out_frame_debug_expr is going to
3573 understand '(unspec:SI [(reg:CC 68) ...] UNSPEC_MOVESI_FROM_CR)',
3574 so we need to construct a frame expression manually. */
3575 RTX_FRAME_RELATED_P (insn) = 1;
3576
3577 /* Update address to be stack-pointer relative, like
3578 rs6000_frame_related would do. */
3579 addr = gen_rtx_PLUS (Pmode, gen_rtx_REG (Pmode, STACK_POINTER_REGNUM),
3580 GEN_INT (info->cr_save_offset + sp_off));
3581 mem = gen_frame_mem (SImode, addr);
3582
3583 if (DEFAULT_ABI == ABI_ELFv2)
3584 {
3585 /* In the ELFv2 ABI we generate separate CFI records for each
3586 CR field that was actually saved. They all point to the
3587 same 32-bit stack slot. */
3588 rtx crframe[8];
3589 int n_crframe = 0;
3590
3591 for (i = 0; i < 8; i++)
3592 if (save_reg_p (CR0_REGNO + i))
3593 {
3594 crframe[n_crframe]
3595 = gen_rtx_SET (mem, gen_rtx_REG (SImode, CR0_REGNO + i));
3596
3597 RTX_FRAME_RELATED_P (crframe[n_crframe]) = 1;
3598 n_crframe++;
3599 }
3600
3601 add_reg_note (insn, REG_FRAME_RELATED_EXPR,
3602 gen_rtx_PARALLEL (VOIDmode,
3603 gen_rtvec_v (n_crframe, crframe)));
3604 }
3605 else
3606 {
3607 /* In other ABIs, by convention, we use a single CR regnum to
3608 represent the fact that all call-saved CR fields are saved.
3609 We use CR2_REGNO to be compatible with gcc-2.95 on Linux. */
3610 rtx set = gen_rtx_SET (mem, gen_rtx_REG (SImode, CR2_REGNO));
3611 add_reg_note (insn, REG_FRAME_RELATED_EXPR, set);
3612 }
3613 }
3614
3615 /* In the ELFv2 ABI we need to save all call-saved CR fields into
3616 *separate* slots if the routine calls __builtin_eh_return, so
3617 that they can be independently restored by the unwinder. */
3618 if (DEFAULT_ABI == ABI_ELFv2 && crtl->calls_eh_return)
3619 {
3620 int i, cr_off = info->ehcr_offset;
3621 rtx crsave;
3622
3623 /* ??? We might get better performance by using multiple mfocrf
3624 instructions. */
3625 crsave = gen_rtx_REG (SImode, 0);
3626 emit_insn (gen_prologue_movesi_from_cr (crsave));
3627
3628 for (i = 0; i < 8; i++)
3629 if (!call_used_or_fixed_reg_p (CR0_REGNO + i))
3630 {
3631 rtvec p = rtvec_alloc (2);
3632 RTVEC_ELT (p, 0)
3633 = gen_frame_store (crsave, frame_reg_rtx, cr_off + frame_off);
3634 RTVEC_ELT (p, 1)
3635 = gen_rtx_USE (VOIDmode, gen_rtx_REG (CCmode, CR0_REGNO + i));
3636
3637 insn = emit_insn (gen_rtx_PARALLEL (VOIDmode, p));
3638
3639 RTX_FRAME_RELATED_P (insn) = 1;
3640 add_reg_note (insn, REG_FRAME_RELATED_EXPR,
3641 gen_frame_store (gen_rtx_REG (SImode, CR0_REGNO + i),
3642 sp_reg_rtx, cr_off + sp_off));
3643
3644 cr_off += reg_size;
3645 }
3646 }
3647
3648 /* If we are emitting stack probes, but allocate no stack, then
3649 just note that in the dump file. */
3650 if (flag_stack_clash_protection
3651 && dump_file
3652 && !info->push_p)
3653 dump_stack_clash_frame_info (NO_PROBE_NO_FRAME, false);
3654
3655 /* Update stack and set back pointer unless this is V.4,
3656 for which it was done previously. */
3657 if (!WORLD_SAVE_P (info) && info->push_p
3658 && !(DEFAULT_ABI == ABI_V4 || crtl->calls_eh_return))
3659 {
3660 rtx ptr_reg = NULL;
3661 int ptr_off = 0;
3662
3663 /* If saving altivec regs we need to be able to address all save
3664 locations using a 16-bit offset. */
3665 if ((strategy & SAVE_INLINE_VRS) == 0
3666 || (info->altivec_size != 0
3667 && (info->altivec_save_offset + info->altivec_size - 16
3668 + info->total_size - frame_off) > 32767)
3669 || (info->vrsave_size != 0
3670 && (info->vrsave_save_offset
3671 + info->total_size - frame_off) > 32767))
3672 {
3673 int sel = SAVRES_SAVE | SAVRES_VR;
3674 unsigned ptr_regno = ptr_regno_for_savres (sel);
3675
3676 if (using_static_chain_p
3677 && ptr_regno == STATIC_CHAIN_REGNUM)
3678 ptr_regno = 12;
3679 if (REGNO (frame_reg_rtx) != ptr_regno)
3680 START_USE (ptr_regno);
3681 ptr_reg = gen_rtx_REG (Pmode, ptr_regno);
3682 frame_reg_rtx = ptr_reg;
3683 ptr_off = info->altivec_save_offset + info->altivec_size;
3684 frame_off = -ptr_off;
3685 }
3686 else if (REGNO (frame_reg_rtx) == 1)
3687 frame_off = info->total_size;
3688 sp_adjust = rs6000_emit_allocate_stack (info->total_size,
3689 ptr_reg, ptr_off);
3690 if (REGNO (frame_reg_rtx) == 12)
3691 sp_adjust = 0;
3692 sp_off = info->total_size;
3693 if (frame_reg_rtx != sp_reg_rtx)
3694 rs6000_emit_stack_tie (frame_reg_rtx, false);
3695 }
3696
3697 /* Set frame pointer, if needed. */
3698 if (frame_pointer_needed_indeed)
3699 {
3700 insn = emit_move_insn (gen_rtx_REG (Pmode, HARD_FRAME_POINTER_REGNUM),
3701 sp_reg_rtx);
3702 RTX_FRAME_RELATED_P (insn) = 1;
3703 }
3704
3705 /* Save AltiVec registers if needed. Save here because the red zone does
3706 not always include AltiVec registers. */
3707 if (!WORLD_SAVE_P (info)
3708 && info->altivec_size != 0 && (strategy & SAVE_INLINE_VRS) == 0)
3709 {
3710 int end_save = info->altivec_save_offset + info->altivec_size;
3711 int ptr_off;
3712 /* Oddly, the vector save/restore functions point r0 at the end
3713 of the save area, then use r11 or r12 to load offsets for
3714 [reg+reg] addressing. */
3715 rtx ptr_reg = gen_rtx_REG (Pmode, 0);
3716 int scratch_regno = ptr_regno_for_savres (SAVRES_SAVE | SAVRES_VR);
3717 rtx scratch_reg = gen_rtx_REG (Pmode, scratch_regno);
3718
3719 gcc_checking_assert (scratch_regno == 11 || scratch_regno == 12);
3720 NOT_INUSE (0);
3721 if (scratch_regno == 12)
3722 sp_adjust = 0;
3723 if (end_save + frame_off != 0)
3724 {
3725 rtx offset = GEN_INT (end_save + frame_off);
3726
3727 emit_insn (gen_add3_insn (ptr_reg, frame_reg_rtx, offset));
3728 }
3729 else
3730 emit_move_insn (ptr_reg, frame_reg_rtx);
3731
3732 ptr_off = -end_save;
3733 insn = rs6000_emit_savres_rtx (info, scratch_reg,
3734 info->altivec_save_offset + ptr_off,
3735 0, V4SImode, SAVRES_SAVE | SAVRES_VR);
3736 rs6000_frame_related (insn, scratch_reg, sp_off - ptr_off,
3737 NULL_RTX, NULL_RTX);
3738 if (REGNO (frame_reg_rtx) == REGNO (scratch_reg))
3739 {
3740 /* The oddity mentioned above clobbered our frame reg. */
3741 emit_move_insn (frame_reg_rtx, ptr_reg);
3742 frame_off = ptr_off;
3743 }
3744 }
3745 else if (!WORLD_SAVE_P (info)
3746 && info->altivec_size != 0)
3747 {
3748 int i;
3749
3750 for (i = info->first_altivec_reg_save; i <= LAST_ALTIVEC_REGNO; ++i)
3751 if (info->vrsave_mask & ALTIVEC_REG_BIT (i))
3752 {
3753 rtx areg, savereg, mem;
3754 HOST_WIDE_INT offset;
3755
3756 offset = (info->altivec_save_offset + frame_off
3757 + 16 * (i - info->first_altivec_reg_save));
3758
3759 savereg = gen_rtx_REG (V4SImode, i);
3760
3761 if (TARGET_P9_VECTOR && quad_address_offset_p (offset))
3762 {
3763 mem = gen_frame_mem (V4SImode,
3764 gen_rtx_PLUS (Pmode, frame_reg_rtx,
3765 GEN_INT (offset)));
3766 insn = emit_insn (gen_rtx_SET (mem, savereg));
3767 areg = NULL_RTX;
3768 }
3769 else
3770 {
3771 NOT_INUSE (0);
3772 areg = gen_rtx_REG (Pmode, 0);
3773 emit_move_insn (areg, GEN_INT (offset));
3774
3775 /* AltiVec addressing mode is [reg+reg]. */
3776 mem = gen_frame_mem (V4SImode,
3777 gen_rtx_PLUS (Pmode, frame_reg_rtx, areg));
3778
3779 /* Rather than emitting a generic move, force use of the stvx
3780 instruction, which we always want on ISA 2.07 (power8) systems.
3781 In particular we don't want xxpermdi/stxvd2x for little
3782 endian. */
3783 insn = emit_insn (gen_altivec_stvx_v4si_internal (mem, savereg));
3784 }
3785
3786 rs6000_frame_related (insn, frame_reg_rtx, sp_off - frame_off,
3787 areg, GEN_INT (offset));
3788 }
3789 }
3790
3791 /* VRSAVE is a bit vector representing which AltiVec registers
3792 are used. The OS uses this to determine which vector
3793 registers to save on a context switch. We need to save
3794 VRSAVE on the stack frame, add whatever AltiVec registers we
3795 used in this function, and do the corresponding magic in the
3796 epilogue. */
3797
3798 if (!WORLD_SAVE_P (info) && info->vrsave_size != 0)
3799 {
3800 /* Get VRSAVE into a GPR. Note that ABI_V4 and ABI_DARWIN might
3801 be using r12 as frame_reg_rtx and r11 as the static chain
3802 pointer for nested functions. */
3803 int save_regno = 12;
3804 if ((DEFAULT_ABI == ABI_AIX || DEFAULT_ABI == ABI_ELFv2)
3805 && !using_static_chain_p)
3806 save_regno = 11;
3807 else if (using_split_stack || REGNO (frame_reg_rtx) == 12)
3808 {
3809 save_regno = 11;
3810 if (using_static_chain_p)
3811 save_regno = 0;
3812 }
3813 NOT_INUSE (save_regno);
3814
3815 emit_vrsave_prologue (info, save_regno, frame_off, frame_reg_rtx);
3816 }
3817
3818 /* If we are using RS6000_PIC_OFFSET_TABLE_REGNUM, we need to set it up. */
3819 if (!TARGET_SINGLE_PIC_BASE
3820 && ((TARGET_TOC && TARGET_MINIMAL_TOC
3821 && !constant_pool_empty_p ())
3822 || (DEFAULT_ABI == ABI_V4
3823 && (flag_pic == 1 || (flag_pic && TARGET_SECURE_PLT))
3824 && df_regs_ever_live_p (RS6000_PIC_OFFSET_TABLE_REGNUM))))
3825 {
3826 /* If emit_load_toc_table will use the link register, we need to save
3827 it. We use R12 for this purpose because emit_load_toc_table
3828 can use register 0. This allows us to use a plain 'blr' to return
3829 from the procedure more often. */
3830 int save_LR_around_toc_setup = (TARGET_ELF
3831 && DEFAULT_ABI == ABI_V4
3832 && flag_pic
3833 && ! info->lr_save_p
3834 && EDGE_COUNT (EXIT_BLOCK_PTR_FOR_FN (cfun)->preds) > 0);
3835 if (save_LR_around_toc_setup)
3836 {
3837 rtx lr = gen_rtx_REG (Pmode, LR_REGNO);
3838 rtx tmp = gen_rtx_REG (Pmode, 12);
3839
3840 sp_adjust = 0;
3841 insn = emit_move_insn (tmp, lr);
3842 RTX_FRAME_RELATED_P (insn) = 1;
3843
3844 rs6000_emit_load_toc_table (TRUE);
3845
3846 insn = emit_move_insn (lr, tmp);
3847 add_reg_note (insn, REG_CFA_RESTORE, lr);
3848 RTX_FRAME_RELATED_P (insn) = 1;
3849 }
3850 else
3851 rs6000_emit_load_toc_table (TRUE);
3852 }
3853
3854 #if TARGET_MACHO
3855 if (!TARGET_SINGLE_PIC_BASE
3856 && DEFAULT_ABI == ABI_DARWIN
3857 && flag_pic && crtl->uses_pic_offset_table)
3858 {
3859 rtx lr = gen_rtx_REG (Pmode, LR_REGNO);
3860 rtx src = gen_rtx_SYMBOL_REF (Pmode, MACHOPIC_FUNCTION_BASE_NAME);
3861
3862 /* Save and restore LR locally around this call (in R0). */
3863 if (!info->lr_save_p)
3864 emit_move_insn (gen_rtx_REG (Pmode, 0), lr);
3865
3866 emit_insn (gen_load_macho_picbase (Pmode, src));
3867
3868 emit_move_insn (gen_rtx_REG (Pmode,
3869 RS6000_PIC_OFFSET_TABLE_REGNUM),
3870 lr);
3871
3872 if (!info->lr_save_p)
3873 emit_move_insn (lr, gen_rtx_REG (Pmode, 0));
3874 }
3875 #endif
3876
3877 /* If we need to, save the TOC register after doing the stack setup.
3878 Do not emit eh frame info for this save. The unwinder wants info,
3879 conceptually attached to instructions in this function, about
3880 register values in the caller of this function. This R2 may have
3881 already been changed from the value in the caller.
3882 We don't attempt to write accurate DWARF EH frame info for R2
3883 because code emitted by gcc for a (non-pointer) function call
3884 doesn't save and restore R2. Instead, R2 is managed out-of-line
3885 by a linker generated plt call stub when the function resides in
3886 a shared library. This behavior is costly to describe in DWARF,
3887 both in terms of the size of DWARF info and the time taken in the
3888 unwinder to interpret it. R2 changes, apart from the
3889 calls_eh_return case earlier in this function, are handled by
3890 linux-unwind.h frob_update_context. */
3891 if (rs6000_save_toc_in_prologue_p ()
3892 && !cfun->machine->toc_is_wrapped_separately)
3893 {
3894 rtx reg = gen_rtx_REG (reg_mode, TOC_REGNUM);
3895 emit_insn (gen_frame_store (reg, sp_reg_rtx, RS6000_TOC_SAVE_SLOT));
3896 }
3897
3898 /* Set up the arg pointer (r12) for -fsplit-stack code. */
3899 if (using_split_stack && split_stack_arg_pointer_used_p ())
3900 emit_split_stack_prologue (info, sp_adjust, frame_off, frame_reg_rtx);
3901 }
3902
3903 /* Output .extern statements for the save/restore routines we use. */
3904
3905 static void
3906 rs6000_output_savres_externs (FILE *file)
3907 {
3908 rs6000_stack_t *info = rs6000_stack_info ();
3909
3910 if (TARGET_DEBUG_STACK)
3911 debug_stack_info (info);
3912
3913 /* Write .extern for any function we will call to save and restore
3914 fp values. */
3915 if (info->first_fp_reg_save < 64
3916 && !TARGET_MACHO
3917 && !TARGET_ELF)
3918 {
3919 char *name;
3920 int regno = info->first_fp_reg_save - 32;
3921
3922 if ((info->savres_strategy & SAVE_INLINE_FPRS) == 0)
3923 {
3924 bool lr = (info->savres_strategy & SAVE_NOINLINE_FPRS_SAVES_LR) != 0;
3925 int sel = SAVRES_SAVE | SAVRES_FPR | (lr ? SAVRES_LR : 0);
3926 name = rs6000_savres_routine_name (regno, sel);
3927 fprintf (file, "\t.extern %s\n", name);
3928 }
3929 if ((info->savres_strategy & REST_INLINE_FPRS) == 0)
3930 {
3931 bool lr = (info->savres_strategy
3932 & REST_NOINLINE_FPRS_DOESNT_RESTORE_LR) == 0;
3933 int sel = SAVRES_FPR | (lr ? SAVRES_LR : 0);
3934 name = rs6000_savres_routine_name (regno, sel);
3935 fprintf (file, "\t.extern %s\n", name);
3936 }
3937 }
3938 }
3939
3940 /* Write function prologue. */
3941
3942 void
3943 rs6000_output_function_prologue (FILE *file)
3944 {
3945 if (!cfun->is_thunk)
3946 {
3947 rs6000_output_savres_externs (file);
3948 #ifdef USING_ELFOS_H
3949 const char *curr_machine = rs6000_machine_from_flags ();
3950 if (rs6000_machine != curr_machine)
3951 {
3952 rs6000_machine = curr_machine;
3953 emit_asm_machine ();
3954 }
3955 #endif
3956 }
3957
3958 /* ELFv2 ABI r2 setup code and local entry point. This must follow
3959 immediately after the global entry point label. */
3960 if (rs6000_global_entry_point_prologue_needed_p ())
3961 {
3962 const char *name = XSTR (XEXP (DECL_RTL (current_function_decl), 0), 0);
3963 (*targetm.asm_out.internal_label) (file, "LCF", rs6000_pic_labelno);
3964
3965 if (TARGET_CMODEL != CMODEL_LARGE)
3966 {
3967 /* In the small and medium code models, we assume the TOC is less
3968 2 GB away from the text section, so it can be computed via the
3969 following two-instruction sequence. */
3970 char buf[256];
3971
3972 ASM_GENERATE_INTERNAL_LABEL (buf, "LCF", rs6000_pic_labelno);
3973 fprintf (file, "0:\taddis 2,12,.TOC.-");
3974 assemble_name (file, buf);
3975 fprintf (file, "@ha\n");
3976 fprintf (file, "\taddi 2,2,.TOC.-");
3977 assemble_name (file, buf);
3978 fprintf (file, "@l\n");
3979 }
3980 else
3981 {
3982 /* In the large code model, we allow arbitrary offsets between the
3983 TOC and the text section, so we have to load the offset from
3984 memory. The data field is emitted directly before the global
3985 entry point in rs6000_elf_declare_function_name. */
3986 char buf[256];
3987
3988 #ifdef HAVE_AS_ENTRY_MARKERS
3989 /* If supported by the linker, emit a marker relocation. If the
3990 total code size of the final executable or shared library
3991 happens to fit into 2 GB after all, the linker will replace
3992 this code sequence with the sequence for the small or medium
3993 code model. */
3994 fprintf (file, "\t.reloc .,R_PPC64_ENTRY\n");
3995 #endif
3996 fprintf (file, "\tld 2,");
3997 ASM_GENERATE_INTERNAL_LABEL (buf, "LCL", rs6000_pic_labelno);
3998 assemble_name (file, buf);
3999 fprintf (file, "-");
4000 ASM_GENERATE_INTERNAL_LABEL (buf, "LCF", rs6000_pic_labelno);
4001 assemble_name (file, buf);
4002 fprintf (file, "(12)\n");
4003 fprintf (file, "\tadd 2,2,12\n");
4004 }
4005
4006 unsigned short patch_area_size = crtl->patch_area_size;
4007 unsigned short patch_area_entry = crtl->patch_area_entry;
4008 /* Emit non-split patching area now. */
4009 if (!TARGET_SPLIT_PATCH_NOPS && patch_area_size > 0)
4010 {
4011 cfun->machine->global_entry_emitted = true;
4012 /* As ELFv2 ABI shows, the allowable bytes between the global
4013 and local entry points are 0, 4, 8, 16, 32 and 64 when
4014 there is a local entry point. Considering there are two
4015 non-prefixed instructions for global entry point prologue
4016 (8 bytes), the count for patchable nops before local entry
4017 point would be 2, 6 and 14. It's possible to support those
4018 other counts of nops by not making a local entry point, but
4019 we don't have clear use cases for them, so leave them
4020 unsupported for now. */
4021 if (patch_area_entry > 0)
4022 {
4023 if (patch_area_entry != 2
4024 && patch_area_entry != 6
4025 && patch_area_entry != 14)
4026 error ("unsupported number of nops before function entry (%u)",
4027 patch_area_entry);
4028 rs6000_print_patchable_function_entry (file, patch_area_entry,
4029 true);
4030 }
4031 }
4032
4033 fputs ("\t.localentry\t", file);
4034 assemble_name (file, name);
4035 fputs (",.-", file);
4036 assemble_name (file, name);
4037 fputs ("\n", file);
4038 /* Emit the nops after local entry. */
4039 if (patch_area_size > patch_area_entry)
4040 {
4041 patch_area_size -= patch_area_entry;
4042 cfun->machine->stop_patch_area_print = false;
4043 rs6000_print_patchable_function_entry (file, patch_area_size,
4044 patch_area_entry == 0);
4045 }
4046 }
4047
4048 else if (rs6000_pcrel_p ())
4049 {
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);
4059 }
4060
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)
4065 {
4066 gcc_assert (DEFAULT_ABI == ABI_AIX || DEFAULT_ABI == ABI_ELFv2);
4067 gcc_assert (!TARGET_32BIT);
4068
4069 asm_fprintf (file, "\tmflr %s\n", reg_names[0]);
4070
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)
4076 {
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]);
4082 }
4083 else
4084 fprintf (file, "\tbl %s\n", RS6000_MCOUNT);
4085 }
4086
4087 rs6000_pic_labelno++;
4088 }
4089
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)
4094 {
4095 return TARGET_PROFILE_KERNEL;
4096 }
4097
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
4101
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. */
4110
4111 static rtx
4112 load_cr_save (int regno, rtx frame_reg_rtx, int offset, bool exit_func)
4113 {
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);
4117
4118 if (!exit_func && DEFAULT_ABI == ABI_V4)
4119 {
4120 rtx cr = gen_rtx_REG (SImode, CR2_REGNO);
4121 rtx set = gen_rtx_SET (reg, cr);
4122
4123 add_reg_note (insn, REG_CFA_REGISTER, set);
4124 RTX_FRAME_RELATED_P (insn) = 1;
4125 }
4126 return reg;
4127 }
4128
4129 /* Reload CR from REG. */
4130
4131 static void
4132 restore_saved_cr (rtx reg, bool using_mfcr_multiple, bool exit_func)
4133 {
4134 int count = 0;
4135 int i;
4136
4137 if (using_mfcr_multiple)
4138 {
4139 for (i = 0; i < 8; i++)
4140 if (save_reg_p (CR0_REGNO + i))
4141 count++;
4142 gcc_assert (count);
4143 }
4144
4145 if (using_mfcr_multiple && count > 1)
4146 {
4147 rtx_insn *insn;
4148 rtvec p;
4149 int ndx;
4150
4151 p = rtvec_alloc (count);
4152
4153 ndx = 0;
4154 for (i = 0; i < 8; i++)
4155 if (save_reg_p (CR0_REGNO + i))
4156 {
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++;
4164 }
4165 insn = emit_insn (gen_rtx_PARALLEL (VOIDmode, p));
4166 gcc_assert (ndx == count);
4167
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)
4171 {
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));
4176
4177 RTX_FRAME_RELATED_P (insn) = 1;
4178 }
4179 }
4180 else
4181 for (i = 0; i < 8; i++)
4182 if (save_reg_p (CR0_REGNO + i))
4183 {
4184 rtx insn = emit_insn (gen_movsi_to_cr_one
4185 (gen_rtx_REG (CCmode, CR0_REGNO + i), reg));
4186
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)
4191 {
4192 add_reg_note (insn, REG_CFA_RESTORE,
4193 gen_rtx_REG (SImode, CR0_REGNO + i));
4194
4195 RTX_FRAME_RELATED_P (insn) = 1;
4196 }
4197 }
4198
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))
4202 {
4203 rtx_insn *insn = get_last_insn ();
4204 rtx cr = gen_rtx_REG (SImode, CR2_REGNO);
4205
4206 add_reg_note (insn, REG_CFA_RESTORE, cr);
4207 RTX_FRAME_RELATED_P (insn) = 1;
4208 }
4209 }
4210
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. */
4215
4216 static void
4217 load_lr_save (int regno, rtx frame_reg_rtx, int offset)
4218 {
4219 rtx mem = gen_frame_mem_offset (Pmode, frame_reg_rtx, offset);
4220 rtx reg = gen_rtx_REG (Pmode, regno);
4221
4222 emit_move_insn (reg, mem);
4223 }
4224
4225 static void
4226 restore_saved_lr (int regno, bool exit_func)
4227 {
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);
4231
4232 if (!exit_func && flag_shrink_wrap)
4233 {
4234 add_reg_note (insn, REG_CFA_RESTORE, lr);
4235 RTX_FRAME_RELATED_P (insn) = 1;
4236 }
4237 }
4238
4239 static rtx
4240 add_crlr_cfa_restore (const rs6000_stack_t *info, rtx cfa_restores)
4241 {
4242 if (DEFAULT_ABI == ABI_ELFv2)
4243 {
4244 int i;
4245 for (i = 0; i < 8; i++)
4246 if (save_reg_p (CR0_REGNO + i))
4247 {
4248 rtx cr = gen_rtx_REG (SImode, CR0_REGNO + i);
4249 cfa_restores = alloc_reg_note (REG_CFA_RESTORE, cr,
4250 cfa_restores);
4251 }
4252 }
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);
4257
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;
4263 }
4264
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. */
4268
4269 static inline bool
4270 offset_below_red_zone_p (HOST_WIDE_INT offset)
4271 {
4272 return offset < (DEFAULT_ABI == ABI_V4
4273 ? 0
4274 : TARGET_32BIT ? -220 : -288);
4275 }
4276
4277 /* Append CFA_RESTORES to any existing REG_NOTES on the last insn. */
4278
4279 static void
4280 emit_cfa_restores (rtx cfa_restores)
4281 {
4282 rtx_insn *insn = get_last_insn ();
4283 rtx *loc = &REG_NOTES (insn);
4284
4285 while (*loc)
4286 loc = &XEXP (*loc, 1);
4287 *loc = cfa_restores;
4288 RTX_FRAME_RELATED_P (insn) = 1;
4289 }
4290
4291 /* Emit function epilogue as insns. */
4292
4293 void
4294 rs6000_emit_epilogue (enum epilogue_type epilogue_type)
4295 {
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;
4308
4309 rs6000_stack_t *info = rs6000_stack_info ();
4310
4311 int strategy = info->savres_strategy;
4312 bool using_load_multiple = !!(strategy & REST_MULTIPLE);
4313 bool restoring_GPRs_inline = !!(strategy & REST_INLINE_GPRS);
4314 bool restoring_FPRs_inline = !!(strategy & REST_INLINE_FPRS);
4315 if (epilogue_type == EPILOGUE_TYPE_SIBCALL)
4316 {
4317 restoring_GPRs_inline = true;
4318 restoring_FPRs_inline = true;
4319 }
4320
4321 bool using_mtcr_multiple = (rs6000_tune == PROCESSOR_PPC601
4322 || rs6000_tune == PROCESSOR_PPC603
4323 || rs6000_tune == PROCESSOR_PPC750
4324 || optimize_size);
4325
4326 /* Restore via the backchain when we have a large frame, since this
4327 is more efficient than an addis, addi pair. The second condition
4328 here will not trigger at the moment; We don't actually need a
4329 frame pointer for alloca, but the generic parts of the compiler
4330 give us one anyway. */
4331 bool use_backchain_to_restore_sp
4332 = (info->total_size + (info->lr_save_p ? info->lr_save_offset : 0) > 32767
4333 || (cfun->calls_alloca && !frame_pointer_needed));
4334
4335 bool restore_lr = (info->lr_save_p
4336 && (restoring_FPRs_inline
4337 || (strategy & REST_NOINLINE_FPRS_DOESNT_RESTORE_LR))
4338 && (restoring_GPRs_inline
4339 || info->first_fp_reg_save < 64)
4340 && !cfun->machine->lr_is_wrapped_separately);
4341
4342
4343 if (WORLD_SAVE_P (info))
4344 {
4345 gcc_assert (epilogue_type != EPILOGUE_TYPE_SIBCALL);
4346
4347 /* eh_rest_world_r10 will return to the location saved in the LR
4348 stack slot (which is not likely to be our caller.)
4349 Input: R10 -- stack adjustment. Clobbers R0, R11, R12, R7, R8.
4350 rest_world is similar, except any R10 parameter is ignored.
4351 The exception-handling stuff that was here in 2.95 is no
4352 longer necessary. */
4353
4354 rtvec p;
4355 p = rtvec_alloc (9
4356 + 32 - info->first_gp_reg_save
4357 + LAST_ALTIVEC_REGNO + 1 - info->first_altivec_reg_save
4358 + 63 + 1 - info->first_fp_reg_save);
4359
4360 const char *rname;
4361 switch (epilogue_type)
4362 {
4363 case EPILOGUE_TYPE_NORMAL:
4364 rname = ggc_strdup ("*rest_world");
4365 break;
4366
4367 case EPILOGUE_TYPE_EH_RETURN:
4368 rname = ggc_strdup ("*eh_rest_world_r10");
4369 break;
4370
4371 default:
4372 gcc_unreachable ();
4373 }
4374
4375 int j = 0;
4376 RTVEC_ELT (p, j++) = ret_rtx;
4377 RTVEC_ELT (p, j++)
4378 = gen_rtx_USE (VOIDmode, gen_rtx_SYMBOL_REF (Pmode, rname));
4379 /* The instruction pattern requires a clobber here;
4380 it is shared with the restVEC helper. */
4381 RTVEC_ELT (p, j++) = gen_hard_reg_clobber (Pmode, 11);
4382
4383 {
4384 /* CR register traditionally saved as CR2. */
4385 rtx reg = gen_rtx_REG (SImode, CR2_REGNO);
4386 RTVEC_ELT (p, j++)
4387 = gen_frame_load (reg, frame_reg_rtx, info->cr_save_offset);
4388 if (flag_shrink_wrap)
4389 {
4390 cfa_restores = alloc_reg_note (REG_CFA_RESTORE,
4391 gen_rtx_REG (Pmode, LR_REGNO),
4392 cfa_restores);
4393 cfa_restores = alloc_reg_note (REG_CFA_RESTORE, reg, cfa_restores);
4394 }
4395 }
4396
4397 int i;
4398 for (i = 0; i < 32 - info->first_gp_reg_save; i++)
4399 {
4400 rtx reg = gen_rtx_REG (reg_mode, info->first_gp_reg_save + i);
4401 RTVEC_ELT (p, j++)
4402 = gen_frame_load (reg,
4403 frame_reg_rtx, info->gp_save_offset + reg_size * i);
4404 if (flag_shrink_wrap
4405 && save_reg_p (info->first_gp_reg_save + i))
4406 cfa_restores = alloc_reg_note (REG_CFA_RESTORE, reg, cfa_restores);
4407 }
4408 for (i = 0; info->first_altivec_reg_save + i <= LAST_ALTIVEC_REGNO; i++)
4409 {
4410 rtx reg = gen_rtx_REG (V4SImode, info->first_altivec_reg_save + i);
4411 RTVEC_ELT (p, j++)
4412 = gen_frame_load (reg,
4413 frame_reg_rtx, info->altivec_save_offset + 16 * i);
4414 if (flag_shrink_wrap
4415 && save_reg_p (info->first_altivec_reg_save + i))
4416 cfa_restores = alloc_reg_note (REG_CFA_RESTORE, reg, cfa_restores);
4417 }
4418 for (i = 0; info->first_fp_reg_save + i <= 63; i++)
4419 {
4420 rtx reg = gen_rtx_REG (TARGET_HARD_FLOAT ? DFmode : SFmode,
4421 info->first_fp_reg_save + i);
4422 RTVEC_ELT (p, j++)
4423 = gen_frame_load (reg, frame_reg_rtx, info->fp_save_offset + 8 * i);
4424 if (flag_shrink_wrap
4425 && save_reg_p (info->first_fp_reg_save + i))
4426 cfa_restores = alloc_reg_note (REG_CFA_RESTORE, reg, cfa_restores);
4427 }
4428 RTVEC_ELT (p, j++) = gen_hard_reg_clobber (Pmode, 0);
4429 RTVEC_ELT (p, j++) = gen_hard_reg_clobber (SImode, 12);
4430 RTVEC_ELT (p, j++) = gen_hard_reg_clobber (SImode, 7);
4431 RTVEC_ELT (p, j++) = gen_hard_reg_clobber (SImode, 8);
4432 RTVEC_ELT (p, j++)
4433 = gen_rtx_USE (VOIDmode, gen_rtx_REG (SImode, 10));
4434 insn = emit_jump_insn (gen_rtx_PARALLEL (VOIDmode, p));
4435
4436 if (flag_shrink_wrap)
4437 {
4438 REG_NOTES (insn) = cfa_restores;
4439 add_reg_note (insn, REG_CFA_DEF_CFA, sp_reg_rtx);
4440 RTX_FRAME_RELATED_P (insn) = 1;
4441 }
4442 return;
4443 }
4444
4445 /* frame_reg_rtx + frame_off points to the top of this stack frame. */
4446 if (info->push_p)
4447 frame_off = info->total_size;
4448
4449 /* Restore AltiVec registers if we must do so before adjusting the
4450 stack. */
4451 if (info->altivec_size != 0
4452 && (ALWAYS_RESTORE_ALTIVEC_BEFORE_POP
4453 || (DEFAULT_ABI != ABI_V4
4454 && offset_below_red_zone_p (info->altivec_save_offset))))
4455 {
4456 int i;
4457 int scratch_regno = ptr_regno_for_savres (SAVRES_VR);
4458
4459 gcc_checking_assert (scratch_regno == 11 || scratch_regno == 12);
4460 if (use_backchain_to_restore_sp)
4461 {
4462 int frame_regno = 11;
4463
4464 if ((strategy & REST_INLINE_VRS) == 0)
4465 {
4466 /* Of r11 and r12, select the one not clobbered by an
4467 out-of-line restore function for the frame register. */
4468 frame_regno = 11 + 12 - scratch_regno;
4469 }
4470 frame_reg_rtx = gen_rtx_REG (Pmode, frame_regno);
4471 emit_move_insn (frame_reg_rtx,
4472 gen_rtx_MEM (Pmode, sp_reg_rtx));
4473 frame_off = 0;
4474 }
4475 else if (frame_pointer_needed)
4476 frame_reg_rtx = hard_frame_pointer_rtx;
4477
4478 if ((strategy & REST_INLINE_VRS) == 0)
4479 {
4480 int end_save = info->altivec_save_offset + info->altivec_size;
4481 int ptr_off;
4482 rtx ptr_reg = gen_rtx_REG (Pmode, 0);
4483 rtx scratch_reg = gen_rtx_REG (Pmode, scratch_regno);
4484
4485 if (end_save + frame_off != 0)
4486 {
4487 rtx offset = GEN_INT (end_save + frame_off);
4488
4489 emit_insn (gen_add3_insn (ptr_reg, frame_reg_rtx, offset));
4490 }
4491 else
4492 emit_move_insn (ptr_reg, frame_reg_rtx);
4493
4494 ptr_off = -end_save;
4495 insn = rs6000_emit_savres_rtx (info, scratch_reg,
4496 info->altivec_save_offset + ptr_off,
4497 0, V4SImode, SAVRES_VR);
4498 }
4499 else
4500 {
4501 for (i = info->first_altivec_reg_save; i <= LAST_ALTIVEC_REGNO; ++i)
4502 if (info->vrsave_mask & ALTIVEC_REG_BIT (i))
4503 {
4504 rtx addr, areg, mem, insn;
4505 rtx reg = gen_rtx_REG (V4SImode, i);
4506 HOST_WIDE_INT offset
4507 = (info->altivec_save_offset + frame_off
4508 + 16 * (i - info->first_altivec_reg_save));
4509
4510 if (TARGET_P9_VECTOR && quad_address_offset_p (offset))
4511 {
4512 mem = gen_frame_mem (V4SImode,
4513 gen_rtx_PLUS (Pmode, frame_reg_rtx,
4514 GEN_INT (offset)));
4515 insn = gen_rtx_SET (reg, mem);
4516 }
4517 else
4518 {
4519 areg = gen_rtx_REG (Pmode, 0);
4520 emit_move_insn (areg, GEN_INT (offset));
4521
4522 /* AltiVec addressing mode is [reg+reg]. */
4523 addr = gen_rtx_PLUS (Pmode, frame_reg_rtx, areg);
4524 mem = gen_frame_mem (V4SImode, addr);
4525
4526 /* Rather than emitting a generic move, force use of the
4527 lvx instruction, which we always want. In particular we
4528 don't want lxvd2x/xxpermdi for little endian. */
4529 insn = gen_altivec_lvx_v4si_internal (reg, mem);
4530 }
4531
4532 (void) emit_insn (insn);
4533 }
4534 }
4535
4536 for (i = info->first_altivec_reg_save; i <= LAST_ALTIVEC_REGNO; ++i)
4537 if (((strategy & REST_INLINE_VRS) == 0
4538 || (info->vrsave_mask & ALTIVEC_REG_BIT (i)) != 0)
4539 && (flag_shrink_wrap
4540 || (offset_below_red_zone_p
4541 (info->altivec_save_offset
4542 + 16 * (i - info->first_altivec_reg_save))))
4543 && save_reg_p (i))
4544 {
4545 rtx reg = gen_rtx_REG (V4SImode, i);
4546 cfa_restores = alloc_reg_note (REG_CFA_RESTORE, reg, cfa_restores);
4547 }
4548 }
4549
4550 /* Restore VRSAVE if we must do so before adjusting the stack. */
4551 if (info->vrsave_size != 0
4552 && (ALWAYS_RESTORE_ALTIVEC_BEFORE_POP
4553 || (DEFAULT_ABI != ABI_V4
4554 && offset_below_red_zone_p (info->vrsave_save_offset))))
4555 {
4556 rtx reg;
4557
4558 if (frame_reg_rtx == sp_reg_rtx)
4559 {
4560 if (use_backchain_to_restore_sp)
4561 {
4562 frame_reg_rtx = gen_rtx_REG (Pmode, 11);
4563 emit_move_insn (frame_reg_rtx,
4564 gen_rtx_MEM (Pmode, sp_reg_rtx));
4565 frame_off = 0;
4566 }
4567 else if (frame_pointer_needed)
4568 frame_reg_rtx = hard_frame_pointer_rtx;
4569 }
4570
4571 reg = gen_rtx_REG (SImode, 12);
4572 emit_insn (gen_frame_load (reg, frame_reg_rtx,
4573 info->vrsave_save_offset + frame_off));
4574
4575 emit_insn (generate_set_vrsave (reg, info, 1));
4576 }
4577
4578 insn = NULL_RTX;
4579 /* If we have a large stack frame, restore the old stack pointer
4580 using the backchain. */
4581 if (use_backchain_to_restore_sp)
4582 {
4583 if (frame_reg_rtx == sp_reg_rtx)
4584 {
4585 /* Under V.4, don't reset the stack pointer until after we're done
4586 loading the saved registers. */
4587 if (DEFAULT_ABI == ABI_V4)
4588 frame_reg_rtx = gen_rtx_REG (Pmode, 11);
4589
4590 insn = emit_move_insn (frame_reg_rtx,
4591 gen_rtx_MEM (Pmode, sp_reg_rtx));
4592 frame_off = 0;
4593 }
4594 else if (ALWAYS_RESTORE_ALTIVEC_BEFORE_POP
4595 && DEFAULT_ABI == ABI_V4)
4596 /* frame_reg_rtx has been set up by the altivec restore. */
4597 ;
4598 else
4599 {
4600 insn = emit_move_insn (sp_reg_rtx, frame_reg_rtx);
4601 frame_reg_rtx = sp_reg_rtx;
4602 }
4603 }
4604 /* If we have a frame pointer, we can restore the old stack pointer
4605 from it. */
4606 else if (frame_pointer_needed_indeed)
4607 {
4608 frame_reg_rtx = sp_reg_rtx;
4609 if (DEFAULT_ABI == ABI_V4)
4610 frame_reg_rtx = gen_rtx_REG (Pmode, 11);
4611 /* Prevent reordering memory accesses against stack pointer restore. */
4612 else if (cfun->calls_alloca
4613 || offset_below_red_zone_p (-info->total_size))
4614 rs6000_emit_stack_tie (frame_reg_rtx, true);
4615
4616 insn = emit_insn (gen_add3_insn (frame_reg_rtx, hard_frame_pointer_rtx,
4617 GEN_INT (info->total_size)));
4618 frame_off = 0;
4619 }
4620 else if (info->push_p
4621 && DEFAULT_ABI != ABI_V4
4622 && epilogue_type != EPILOGUE_TYPE_EH_RETURN)
4623 {
4624 /* Prevent reordering memory accesses against stack pointer restore. */
4625 if (cfun->calls_alloca
4626 || offset_below_red_zone_p (-info->total_size))
4627 rs6000_emit_stack_tie (frame_reg_rtx, false);
4628 insn = emit_insn (gen_add3_insn (sp_reg_rtx, sp_reg_rtx,
4629 GEN_INT (info->total_size)));
4630 frame_off = 0;
4631 }
4632 if (insn && frame_reg_rtx == sp_reg_rtx)
4633 {
4634 if (cfa_restores)
4635 {
4636 REG_NOTES (insn) = cfa_restores;
4637 cfa_restores = NULL_RTX;
4638 }
4639 add_reg_note (insn, REG_CFA_DEF_CFA, sp_reg_rtx);
4640 RTX_FRAME_RELATED_P (insn) = 1;
4641 }
4642
4643 /* Restore AltiVec registers if we have not done so already. */
4644 if (!ALWAYS_RESTORE_ALTIVEC_BEFORE_POP
4645 && info->altivec_size != 0
4646 && (DEFAULT_ABI == ABI_V4
4647 || !offset_below_red_zone_p (info->altivec_save_offset)))
4648 {
4649 int i;
4650
4651 if ((strategy & REST_INLINE_VRS) == 0)
4652 {
4653 int end_save = info->altivec_save_offset + info->altivec_size;
4654 int ptr_off;
4655 rtx ptr_reg = gen_rtx_REG (Pmode, 0);
4656 int scratch_regno = ptr_regno_for_savres (SAVRES_VR);
4657 rtx scratch_reg = gen_rtx_REG (Pmode, scratch_regno);
4658
4659 if (end_save + frame_off != 0)
4660 {
4661 rtx offset = GEN_INT (end_save + frame_off);
4662
4663 emit_insn (gen_add3_insn (ptr_reg, frame_reg_rtx, offset));
4664 }
4665 else
4666 emit_move_insn (ptr_reg, frame_reg_rtx);
4667
4668 ptr_off = -end_save;
4669 insn = rs6000_emit_savres_rtx (info, scratch_reg,
4670 info->altivec_save_offset + ptr_off,
4671 0, V4SImode, SAVRES_VR);
4672 if (REGNO (frame_reg_rtx) == REGNO (scratch_reg))
4673 {
4674 /* Frame reg was clobbered by out-of-line save. Restore it
4675 from ptr_reg, and if we are calling out-of-line gpr or
4676 fpr restore set up the correct pointer and offset. */
4677 unsigned newptr_regno = 1;
4678 if (!restoring_GPRs_inline)
4679 {
4680 bool lr = info->gp_save_offset + info->gp_size == 0;
4681 int sel = SAVRES_GPR | (lr ? SAVRES_LR : 0);
4682 newptr_regno = ptr_regno_for_savres (sel);
4683 end_save = info->gp_save_offset + info->gp_size;
4684 }
4685 else if (!restoring_FPRs_inline)
4686 {
4687 bool lr = !(strategy & REST_NOINLINE_FPRS_DOESNT_RESTORE_LR);
4688 int sel = SAVRES_FPR | (lr ? SAVRES_LR : 0);
4689 newptr_regno = ptr_regno_for_savres (sel);
4690 end_save = info->fp_save_offset + info->fp_size;
4691 }
4692
4693 if (newptr_regno != 1 && REGNO (frame_reg_rtx) != newptr_regno)
4694 frame_reg_rtx = gen_rtx_REG (Pmode, newptr_regno);
4695
4696 if (end_save + ptr_off != 0)
4697 {
4698 rtx offset = GEN_INT (end_save + ptr_off);
4699
4700 frame_off = -end_save;
4701 if (TARGET_32BIT)
4702 emit_insn (gen_addsi3_carry (frame_reg_rtx,
4703 ptr_reg, offset));
4704 else
4705 emit_insn (gen_adddi3_carry (frame_reg_rtx,
4706 ptr_reg, offset));
4707 }
4708 else
4709 {
4710 frame_off = ptr_off;
4711 emit_move_insn (frame_reg_rtx, ptr_reg);
4712 }
4713 }
4714 }
4715 else
4716 {
4717 for (i = info->first_altivec_reg_save; i <= LAST_ALTIVEC_REGNO; ++i)
4718 if (info->vrsave_mask & ALTIVEC_REG_BIT (i))
4719 {
4720 rtx addr, areg, mem, insn;
4721 rtx reg = gen_rtx_REG (V4SImode, i);
4722 HOST_WIDE_INT offset
4723 = (info->altivec_save_offset + frame_off
4724 + 16 * (i - info->first_altivec_reg_save));
4725
4726 if (TARGET_P9_VECTOR && quad_address_offset_p (offset))
4727 {
4728 mem = gen_frame_mem (V4SImode,
4729 gen_rtx_PLUS (Pmode, frame_reg_rtx,
4730 GEN_INT (offset)));
4731 insn = gen_rtx_SET (reg, mem);
4732 }
4733 else
4734 {
4735 areg = gen_rtx_REG (Pmode, 0);
4736 emit_move_insn (areg, GEN_INT (offset));
4737
4738 /* AltiVec addressing mode is [reg+reg]. */
4739 addr = gen_rtx_PLUS (Pmode, frame_reg_rtx, areg);
4740 mem = gen_frame_mem (V4SImode, addr);
4741
4742 /* Rather than emitting a generic move, force use of the
4743 lvx instruction, which we always want. In particular we
4744 don't want lxvd2x/xxpermdi for little endian. */
4745 insn = gen_altivec_lvx_v4si_internal (reg, mem);
4746 }
4747
4748 (void) emit_insn (insn);
4749 }
4750 }
4751
4752 for (i = info->first_altivec_reg_save; i <= LAST_ALTIVEC_REGNO; ++i)
4753 if (((strategy & REST_INLINE_VRS) == 0
4754 || (info->vrsave_mask & ALTIVEC_REG_BIT (i)) != 0)
4755 && (DEFAULT_ABI == ABI_V4 || flag_shrink_wrap)
4756 && save_reg_p (i))
4757 {
4758 rtx reg = gen_rtx_REG (V4SImode, i);
4759 cfa_restores = alloc_reg_note (REG_CFA_RESTORE, reg, cfa_restores);
4760 }
4761 }
4762
4763 /* Restore VRSAVE if we have not done so already. */
4764 if (!ALWAYS_RESTORE_ALTIVEC_BEFORE_POP
4765 && info->vrsave_size != 0
4766 && (DEFAULT_ABI == ABI_V4
4767 || !offset_below_red_zone_p (info->vrsave_save_offset)))
4768 {
4769 rtx reg;
4770
4771 reg = gen_rtx_REG (SImode, 12);
4772 emit_insn (gen_frame_load (reg, frame_reg_rtx,
4773 info->vrsave_save_offset + frame_off));
4774
4775 emit_insn (generate_set_vrsave (reg, info, 1));
4776 }
4777
4778 /* If we exit by an out-of-line restore function on ABI_V4 then that
4779 function will deallocate the stack, so we don't need to worry
4780 about the unwinder restoring cr from an invalid stack frame
4781 location. */
4782 bool exit_func = (!restoring_FPRs_inline
4783 || (!restoring_GPRs_inline
4784 && info->first_fp_reg_save == 64));
4785
4786 /* In the ELFv2 ABI we need to restore all call-saved CR fields from
4787 *separate* slots if the routine calls __builtin_eh_return, so
4788 that they can be independently restored by the unwinder. Since
4789 it is for CR fields restoring, it should be done for any epilogue
4790 types (not EPILOGUE_TYPE_EH_RETURN specific). */
4791 if (DEFAULT_ABI == ABI_ELFv2 && crtl->calls_eh_return)
4792 {
4793 int i, cr_off = info->ehcr_offset;
4794
4795 for (i = 0; i < 8; i++)
4796 if (!call_used_or_fixed_reg_p (CR0_REGNO + i))
4797 {
4798 rtx reg = gen_rtx_REG (SImode, 0);
4799 emit_insn (gen_frame_load (reg, frame_reg_rtx,
4800 cr_off + frame_off));
4801
4802 insn = emit_insn (gen_movsi_to_cr_one
4803 (gen_rtx_REG (CCmode, CR0_REGNO + i), reg));
4804
4805 if (!exit_func && flag_shrink_wrap)
4806 {
4807 add_reg_note (insn, REG_CFA_RESTORE,
4808 gen_rtx_REG (SImode, CR0_REGNO + i));
4809
4810 RTX_FRAME_RELATED_P (insn) = 1;
4811 }
4812
4813 cr_off += reg_size;
4814 }
4815 }
4816
4817 /* Get the old lr if we saved it. If we are restoring registers
4818 out-of-line, then the out-of-line routines can do this for us. */
4819 if (restore_lr && restoring_GPRs_inline)
4820 load_lr_save (0, frame_reg_rtx, info->lr_save_offset + frame_off);
4821
4822 /* Get the old cr if we saved it. */
4823 if (info->cr_save_p)
4824 {
4825 unsigned cr_save_regno = 12;
4826
4827 if (!restoring_GPRs_inline)
4828 {
4829 /* Ensure we don't use the register used by the out-of-line
4830 gpr register restore below. */
4831 bool lr = info->gp_save_offset + info->gp_size == 0;
4832 int sel = SAVRES_GPR | (lr ? SAVRES_LR : 0);
4833 int gpr_ptr_regno = ptr_regno_for_savres (sel);
4834
4835 if (gpr_ptr_regno == 12)
4836 cr_save_regno = 11;
4837 gcc_checking_assert (REGNO (frame_reg_rtx) != cr_save_regno);
4838 }
4839 else if (REGNO (frame_reg_rtx) == 12)
4840 cr_save_regno = 11;
4841
4842 /* For ELFv2 r12 is already in use as the GEP. */
4843 if (DEFAULT_ABI == ABI_ELFv2)
4844 cr_save_regno = 11;
4845
4846 cr_save_reg = load_cr_save (cr_save_regno, frame_reg_rtx,
4847 info->cr_save_offset + frame_off,
4848 exit_func);
4849 }
4850
4851 /* Set LR here to try to overlap restores below. */
4852 if (restore_lr && restoring_GPRs_inline)
4853 restore_saved_lr (0, exit_func);
4854
4855 /* Load exception handler data registers, if needed. */
4856 if (epilogue_type == EPILOGUE_TYPE_EH_RETURN)
4857 {
4858 unsigned int i, regno;
4859
4860 if (TARGET_AIX)
4861 {
4862 rtx reg = gen_rtx_REG (reg_mode, 2);
4863 emit_insn (gen_frame_load (reg, frame_reg_rtx,
4864 frame_off + RS6000_TOC_SAVE_SLOT));
4865 }
4866
4867 for (i = 0; ; ++i)
4868 {
4869 rtx mem;
4870
4871 regno = EH_RETURN_DATA_REGNO (i);
4872 if (regno == INVALID_REGNUM)
4873 break;
4874
4875 mem = gen_frame_mem_offset (reg_mode, frame_reg_rtx,
4876 info->ehrd_offset + frame_off
4877 + reg_size * (int) i);
4878
4879 emit_move_insn (gen_rtx_REG (reg_mode, regno), mem);
4880 }
4881 }
4882
4883 /* Restore GPRs. This is done as a PARALLEL if we are using
4884 the load-multiple instructions. */
4885 if (!restoring_GPRs_inline)
4886 {
4887 /* We are jumping to an out-of-line function. */
4888 rtx ptr_reg;
4889 int end_save = info->gp_save_offset + info->gp_size;
4890 bool can_use_exit = end_save == 0;
4891 int sel = SAVRES_GPR | (can_use_exit ? SAVRES_LR : 0);
4892 int ptr_off;
4893
4894 /* Emit stack reset code if we need it. */
4895 ptr_regno = ptr_regno_for_savres (sel);
4896 ptr_reg = gen_rtx_REG (Pmode, ptr_regno);
4897 if (can_use_exit)
4898 rs6000_emit_stack_reset (frame_reg_rtx, frame_off, ptr_regno);
4899 else if (end_save + frame_off != 0)
4900 emit_insn (gen_add3_insn (ptr_reg, frame_reg_rtx,
4901 GEN_INT (end_save + frame_off)));
4902 else if (REGNO (frame_reg_rtx) != ptr_regno)
4903 emit_move_insn (ptr_reg, frame_reg_rtx);
4904 if (REGNO (frame_reg_rtx) == ptr_regno)
4905 frame_off = -end_save;
4906
4907 if (can_use_exit && info->cr_save_p)
4908 restore_saved_cr (cr_save_reg, using_mtcr_multiple, true);
4909
4910 ptr_off = -end_save;
4911 rs6000_emit_savres_rtx (info, ptr_reg,
4912 info->gp_save_offset + ptr_off,
4913 info->lr_save_offset + ptr_off,
4914 reg_mode, sel);
4915 }
4916 else if (using_load_multiple)
4917 {
4918 rtvec p;
4919 p = rtvec_alloc (32 - info->first_gp_reg_save);
4920 for (i = 0; i < 32 - info->first_gp_reg_save; i++)
4921 RTVEC_ELT (p, i)
4922 = gen_frame_load (gen_rtx_REG (reg_mode, info->first_gp_reg_save + i),
4923 frame_reg_rtx,
4924 info->gp_save_offset + frame_off + reg_size * i);
4925 emit_insn (gen_rtx_PARALLEL (VOIDmode, p));
4926 }
4927 else
4928 {
4929 int offset = info->gp_save_offset + frame_off;
4930 for (i = info->first_gp_reg_save; i < 32; i++)
4931 {
4932 if (save_reg_p (i)
4933 && !cfun->machine->gpr_is_wrapped_separately[i])
4934 {
4935 rtx reg = gen_rtx_REG (reg_mode, i);
4936 emit_insn (gen_frame_load (reg, frame_reg_rtx, offset));
4937 }
4938
4939 offset += reg_size;
4940 }
4941 }
4942
4943 if (DEFAULT_ABI == ABI_V4 || flag_shrink_wrap)
4944 {
4945 /* If the frame pointer was used then we can't delay emitting
4946 a REG_CFA_DEF_CFA note. This must happen on the insn that
4947 restores the frame pointer, r31. We may have already emitted
4948 a REG_CFA_DEF_CFA note, but that's OK; A duplicate is
4949 discarded by dwarf2cfi.cc/dwarf2out.cc, and in any case would
4950 be harmless if emitted. */
4951 if (frame_pointer_needed_indeed)
4952 {
4953 insn = get_last_insn ();
4954 add_reg_note (insn, REG_CFA_DEF_CFA,
4955 plus_constant (Pmode, frame_reg_rtx, frame_off));
4956 RTX_FRAME_RELATED_P (insn) = 1;
4957 }
4958
4959 /* Set up cfa_restores. We always need these when
4960 shrink-wrapping. If not shrink-wrapping then we only need
4961 the cfa_restore when the stack location is no longer valid.
4962 The cfa_restores must be emitted on or before the insn that
4963 invalidates the stack, and of course must not be emitted
4964 before the insn that actually does the restore. The latter
4965 is why it is a bad idea to emit the cfa_restores as a group
4966 on the last instruction here that actually does a restore:
4967 That insn may be reordered with respect to others doing
4968 restores. */
4969 if (flag_shrink_wrap
4970 && !restoring_GPRs_inline
4971 && info->first_fp_reg_save == 64)
4972 cfa_restores = add_crlr_cfa_restore (info, cfa_restores);
4973
4974 for (i = info->first_gp_reg_save; i < 32; i++)
4975 if (save_reg_p (i)
4976 && !cfun->machine->gpr_is_wrapped_separately[i])
4977 {
4978 rtx reg = gen_rtx_REG (reg_mode, i);
4979 cfa_restores = alloc_reg_note (REG_CFA_RESTORE, reg, cfa_restores);
4980 }
4981 }
4982
4983 if (!restoring_GPRs_inline
4984 && info->first_fp_reg_save == 64)
4985 {
4986 /* We are jumping to an out-of-line function. */
4987 if (cfa_restores)
4988 emit_cfa_restores (cfa_restores);
4989 return;
4990 }
4991
4992 if (restore_lr && !restoring_GPRs_inline)
4993 {
4994 load_lr_save (0, frame_reg_rtx, info->lr_save_offset + frame_off);
4995 restore_saved_lr (0, exit_func);
4996 }
4997
4998 /* Restore fpr's if we need to do it without calling a function. */
4999 if (restoring_FPRs_inline)
5000 {
5001 int offset = info->fp_save_offset + frame_off;
5002 for (i = info->first_fp_reg_save; i < 64; i++)
5003 {
5004 if (save_reg_p (i)
5005 && !cfun->machine->fpr_is_wrapped_separately[i - 32])
5006 {
5007 rtx reg = gen_rtx_REG (fp_reg_mode, i);
5008 emit_insn (gen_frame_load (reg, frame_reg_rtx, offset));
5009 if (DEFAULT_ABI == ABI_V4 || flag_shrink_wrap)
5010 cfa_restores = alloc_reg_note (REG_CFA_RESTORE, reg,
5011 cfa_restores);
5012 }
5013
5014 offset += fp_reg_size;
5015 }
5016 }
5017
5018 /* If we saved cr, restore it here. Just those that were used. */
5019 if (info->cr_save_p)
5020 restore_saved_cr (cr_save_reg, using_mtcr_multiple, exit_func);
5021
5022 /* If this is V.4, unwind the stack pointer after all of the loads
5023 have been done, or set up r11 if we are restoring fp out of line. */
5024 ptr_regno = 1;
5025 if (!restoring_FPRs_inline)
5026 {
5027 bool lr = (strategy & REST_NOINLINE_FPRS_DOESNT_RESTORE_LR) == 0;
5028 int sel = SAVRES_FPR | (lr ? SAVRES_LR : 0);
5029 ptr_regno = ptr_regno_for_savres (sel);
5030 }
5031
5032 insn = rs6000_emit_stack_reset (frame_reg_rtx, frame_off, ptr_regno);
5033 if (REGNO (frame_reg_rtx) == ptr_regno)
5034 frame_off = 0;
5035
5036 if (insn && restoring_FPRs_inline)
5037 {
5038 if (cfa_restores)
5039 {
5040 REG_NOTES (insn) = cfa_restores;
5041 cfa_restores = NULL_RTX;
5042 }
5043 add_reg_note (insn, REG_CFA_DEF_CFA, sp_reg_rtx);
5044 RTX_FRAME_RELATED_P (insn) = 1;
5045 }
5046
5047 if (epilogue_type == EPILOGUE_TYPE_EH_RETURN)
5048 {
5049 rtx sa = EH_RETURN_STACKADJ_RTX;
5050 emit_insn (gen_add3_insn (sp_reg_rtx, sp_reg_rtx, sa));
5051 }
5052
5053 /* The ROP hash check must occur after the stack pointer is restored
5054 (since the hash involves r1), and is not performed for a sibcall. */
5055 if (info->rop_hash_size
5056 && epilogue_type != EPILOGUE_TYPE_SIBCALL)
5057 {
5058 rtx stack_ptr = gen_rtx_REG (Pmode, STACK_POINTER_REGNUM);
5059 rtx addr = gen_rtx_PLUS (Pmode, stack_ptr,
5060 GEN_INT (info->rop_hash_save_offset));
5061 rtx mem = gen_rtx_MEM (Pmode, addr);
5062 rtx reg0 = gen_rtx_REG (Pmode, 0);
5063 emit_insn (gen_hashchk (reg0, mem));
5064 }
5065
5066 if (epilogue_type != EPILOGUE_TYPE_SIBCALL && restoring_FPRs_inline)
5067 {
5068 if (cfa_restores)
5069 {
5070 /* We can't hang the cfa_restores off a simple return,
5071 since the shrink-wrap code sometimes uses an existing
5072 return. This means there might be a path from
5073 pre-prologue code to this return, and dwarf2cfi code
5074 wants the eh_frame unwinder state to be the same on
5075 all paths to any point. So we need to emit the
5076 cfa_restores before the return. For -m64 we really
5077 don't need epilogue cfa_restores at all, except for
5078 this irritating dwarf2cfi with shrink-wrap
5079 requirement; The stack red-zone means eh_frame info
5080 from the prologue telling the unwinder to restore
5081 from the stack is perfectly good right to the end of
5082 the function. */
5083 emit_insn (gen_blockage ());
5084 emit_cfa_restores (cfa_restores);
5085 cfa_restores = NULL_RTX;
5086 }
5087
5088 emit_jump_insn (targetm.gen_simple_return ());
5089 }
5090
5091 if (epilogue_type != EPILOGUE_TYPE_SIBCALL && !restoring_FPRs_inline)
5092 {
5093 bool lr = (strategy & REST_NOINLINE_FPRS_DOESNT_RESTORE_LR) == 0;
5094 rtvec p = rtvec_alloc (3 + !!lr + 64 - info->first_fp_reg_save);
5095 int elt = 0;
5096 RTVEC_ELT (p, elt++) = ret_rtx;
5097 if (lr)
5098 RTVEC_ELT (p, elt++) = gen_hard_reg_clobber (Pmode, LR_REGNO);
5099
5100 /* We have to restore more than two FP registers, so branch to the
5101 restore function. It will return to our caller. */
5102 int i;
5103 int reg;
5104 rtx sym;
5105
5106 if (flag_shrink_wrap)
5107 cfa_restores = add_crlr_cfa_restore (info, cfa_restores);
5108
5109 sym = rs6000_savres_routine_sym (info, SAVRES_FPR | (lr ? SAVRES_LR : 0));
5110 RTVEC_ELT (p, elt++) = gen_rtx_USE (VOIDmode, sym);
5111 reg = (DEFAULT_ABI == ABI_AIX || DEFAULT_ABI == ABI_ELFv2)? 1 : 11;
5112 RTVEC_ELT (p, elt++) = gen_rtx_USE (VOIDmode, gen_rtx_REG (Pmode, reg));
5113
5114 for (i = 0; i < 64 - info->first_fp_reg_save; i++)
5115 {
5116 rtx reg = gen_rtx_REG (DFmode, info->first_fp_reg_save + i);
5117
5118 RTVEC_ELT (p, elt++)
5119 = gen_frame_load (reg, sp_reg_rtx, info->fp_save_offset + 8 * i);
5120 if (flag_shrink_wrap
5121 && save_reg_p (info->first_fp_reg_save + i))
5122 cfa_restores = alloc_reg_note (REG_CFA_RESTORE, reg, cfa_restores);
5123 }
5124
5125 emit_jump_insn (gen_rtx_PARALLEL (VOIDmode, p));
5126 }
5127
5128 if (cfa_restores)
5129 {
5130 if (epilogue_type == EPILOGUE_TYPE_SIBCALL)
5131 /* Ensure the cfa_restores are hung off an insn that won't
5132 be reordered above other restores. */
5133 emit_insn (gen_blockage ());
5134
5135 emit_cfa_restores (cfa_restores);
5136 }
5137 }
5138
5139 #if TARGET_MACHO
5140
5141 /* Generate far-jump branch islands for everything recorded in
5142 branch_islands. Invoked immediately after the last instruction of
5143 the epilogue has been emitted; the branch islands must be appended
5144 to, and contiguous with, the function body. Mach-O stubs are
5145 generated in machopic_output_stub(). */
5146
5147 static void
5148 macho_branch_islands (void)
5149 {
5150 char tmp_buf[512];
5151
5152 while (!vec_safe_is_empty (branch_islands))
5153 {
5154 branch_island *bi = &branch_islands->last ();
5155 const char *label = IDENTIFIER_POINTER (bi->label_name);
5156 const char *name = IDENTIFIER_POINTER (bi->function_name);
5157 char name_buf[512];
5158 /* Cheap copy of the details from the Darwin ASM_OUTPUT_LABELREF(). */
5159 if (name[0] == '*' || name[0] == '&')
5160 strcpy (name_buf, name+1);
5161 else
5162 {
5163 name_buf[0] = '_';
5164 strcpy (name_buf+1, name);
5165 }
5166 strcpy (tmp_buf, "\n");
5167 strcat (tmp_buf, label);
5168 if (flag_pic)
5169 {
5170 strcat (tmp_buf, ":\n\tmflr r0\n\tbcl 20,31,");
5171 strcat (tmp_buf, label);
5172 strcat (tmp_buf, "_pic\n");
5173 strcat (tmp_buf, label);
5174 strcat (tmp_buf, "_pic:\n\tmflr r11\n");
5175
5176 strcat (tmp_buf, "\taddis r11,r11,ha16(");
5177 strcat (tmp_buf, name_buf);
5178 strcat (tmp_buf, " - ");
5179 strcat (tmp_buf, label);
5180 strcat (tmp_buf, "_pic)\n");
5181
5182 strcat (tmp_buf, "\tmtlr r0\n");
5183
5184 strcat (tmp_buf, "\taddi r12,r11,lo16(");
5185 strcat (tmp_buf, name_buf);
5186 strcat (tmp_buf, " - ");
5187 strcat (tmp_buf, label);
5188 strcat (tmp_buf, "_pic)\n");
5189
5190 strcat (tmp_buf, "\tmtctr r12\n\tbctr\n");
5191 }
5192 else
5193 {
5194 strcat (tmp_buf, ":\n\tlis r12,hi16(");
5195 strcat (tmp_buf, name_buf);
5196 strcat (tmp_buf, ")\n\tori r12,r12,lo16(");
5197 strcat (tmp_buf, name_buf);
5198 strcat (tmp_buf, ")\n\tmtctr r12\n\tbctr");
5199 }
5200 output_asm_insn (tmp_buf, 0);
5201 branch_islands->pop ();
5202 }
5203 }
5204 #endif
5205
5206 /* Write function epilogue. */
5207
5208 void
5209 rs6000_output_function_epilogue (FILE *file)
5210 {
5211 #if TARGET_MACHO
5212 macho_branch_islands ();
5213
5214 {
5215 rtx_insn *insn = get_last_insn ();
5216 rtx_insn *deleted_debug_label = NULL;
5217
5218 /* Mach-O doesn't support labels at the end of objects, so if
5219 it looks like we might want one, take special action.
5220
5221 First, collect any sequence of deleted debug labels. */
5222 while (insn
5223 && NOTE_P (insn)
5224 && NOTE_KIND (insn) != NOTE_INSN_DELETED_LABEL)
5225 {
5226 /* Don't insert a nop for NOTE_INSN_DELETED_DEBUG_LABEL
5227 notes only, instead set their CODE_LABEL_NUMBER to -1,
5228 otherwise there would be code generation differences
5229 in between -g and -g0. */
5230 if (NOTE_P (insn) && NOTE_KIND (insn) == NOTE_INSN_DELETED_DEBUG_LABEL)
5231 deleted_debug_label = insn;
5232 insn = PREV_INSN (insn);
5233 }
5234
5235 /* Second, if we have:
5236 label:
5237 barrier
5238 then this needs to be detected, so skip past the barrier. */
5239
5240 if (insn && BARRIER_P (insn))
5241 insn = PREV_INSN (insn);
5242
5243 /* Up to now we've only seen notes or barriers. */
5244 if (insn)
5245 {
5246 if (LABEL_P (insn)
5247 || (NOTE_P (insn)
5248 && NOTE_KIND (insn) == NOTE_INSN_DELETED_LABEL))
5249 /* Trailing label: <barrier>. */
5250 fputs ("\tnop\n", file);
5251 else
5252 {
5253 /* Lastly, see if we have a completely empty function body. */
5254 while (insn && ! INSN_P (insn))
5255 insn = PREV_INSN (insn);
5256 /* If we don't find any insns, we've got an empty function body;
5257 I.e. completely empty - without a return or branch. This is
5258 taken as the case where a function body has been removed
5259 because it contains an inline __builtin_unreachable(). GCC
5260 states that reaching __builtin_unreachable() means UB so we're
5261 not obliged to do anything special; however, we want
5262 non-zero-sized function bodies. To meet this, and help the
5263 user out, let's trap the case. */
5264 if (insn == NULL)
5265 fputs ("\ttrap\n", file);
5266 }
5267 }
5268 else if (deleted_debug_label)
5269 for (insn = deleted_debug_label; insn; insn = NEXT_INSN (insn))
5270 if (NOTE_KIND (insn) == NOTE_INSN_DELETED_DEBUG_LABEL)
5271 CODE_LABEL_NUMBER (insn) = -1;
5272 }
5273 #endif
5274
5275 /* Output a traceback table here. See /usr/include/sys/debug.h for info
5276 on its format.
5277
5278 We don't output a traceback table if -finhibit-size-directive was
5279 used. The documentation for -finhibit-size-directive reads
5280 ``don't output a @code{.size} assembler directive, or anything
5281 else that would cause trouble if the function is split in the
5282 middle, and the two halves are placed at locations far apart in
5283 memory.'' The traceback table has this property, since it
5284 includes the offset from the start of the function to the
5285 traceback table itself.
5286
5287 System V.4 Powerpc's (and the embedded ABI derived from it) use a
5288 different traceback table. */
5289 if ((DEFAULT_ABI == ABI_AIX || DEFAULT_ABI == ABI_ELFv2)
5290 && ! flag_inhibit_size_directive
5291 && rs6000_traceback != traceback_none && !cfun->is_thunk)
5292 {
5293 const char *fname = NULL;
5294 const char *language_string = lang_hooks.name;
5295 int fixed_parms = 0, float_parms = 0, parm_info = 0;
5296 int i;
5297 int optional_tbtab;
5298 rs6000_stack_t *info = rs6000_stack_info ();
5299
5300 if (rs6000_traceback == traceback_full)
5301 optional_tbtab = 1;
5302 else if (rs6000_traceback == traceback_part)
5303 optional_tbtab = 0;
5304 else
5305 optional_tbtab = !optimize_size && !TARGET_ELF;
5306
5307 if (optional_tbtab)
5308 {
5309 fname = XSTR (XEXP (DECL_RTL (current_function_decl), 0), 0);
5310 while (*fname == '.') /* V.4 encodes . in the name */
5311 fname++;
5312
5313 /* Need label immediately before tbtab, so we can compute
5314 its offset from the function start. */
5315 ASM_OUTPUT_INTERNAL_LABEL_PREFIX (file, "LT");
5316 ASM_OUTPUT_LABEL (file, fname);
5317 }
5318
5319 /* The .tbtab pseudo-op can only be used for the first eight
5320 expressions, since it can't handle the possibly variable
5321 length fields that follow. However, if you omit the optional
5322 fields, the assembler outputs zeros for all optional fields
5323 anyways, giving each variable length field is minimum length
5324 (as defined in sys/debug.h). Thus we cannot use the .tbtab
5325 pseudo-op at all. */
5326
5327 /* An all-zero word flags the start of the tbtab, for debuggers
5328 that have to find it by searching forward from the entry
5329 point or from the current pc. */
5330 fputs ("\t.long 0\n", file);
5331
5332 /* Tbtab format type. Use format type 0. */
5333 fputs ("\t.byte 0,", file);
5334
5335 /* Language type. Unfortunately, there does not seem to be any
5336 official way to discover the language being compiled, so we
5337 use language_string.
5338 C is 0. Fortran is 1. Ada is 3. Modula-2 is 8. C++ is 9.
5339 Java is 13. Objective-C is 14. Objective-C++ isn't assigned
5340 a number, so for now use 9. LTO, Go, D, and JIT aren't assigned
5341 numbers either, so for now use 0. */
5342 if (lang_GNU_C ()
5343 || ! strcmp (language_string, "GNU GIMPLE")
5344 || ! strcmp (language_string, "GNU Go")
5345 || ! strcmp (language_string, "GNU D")
5346 || ! strcmp (language_string, "GNU Rust")
5347 || ! strcmp (language_string, "libgccjit"))
5348 i = 0;
5349 else if (! strcmp (language_string, "GNU F77")
5350 || lang_GNU_Fortran ())
5351 i = 1;
5352 else if (! strcmp (language_string, "GNU Ada"))
5353 i = 3;
5354 else if (! strcmp (language_string, "GCC COBOL"))
5355 i = 7;
5356 else if (! strcmp (language_string, "GNU Modula-2"))
5357 i = 8;
5358 else if (lang_GNU_CXX ()
5359 || ! strcmp (language_string, "GNU Objective-C++"))
5360 i = 9;
5361 else if (! strcmp (language_string, "GNU Java"))
5362 i = 13;
5363 else if (! strcmp (language_string, "GNU Objective-C"))
5364 i = 14;
5365 else
5366 gcc_unreachable ();
5367 fprintf (file, "%d,", i);
5368
5369 /* 8 single bit fields: global linkage (not set for C extern linkage,
5370 apparently a PL/I convention?), out-of-line epilogue/prologue, offset
5371 from start of procedure stored in tbtab, internal function, function
5372 has controlled storage, function has no toc, function uses fp,
5373 function logs/aborts fp operations. */
5374 /* Assume that fp operations are used if any fp reg must be saved. */
5375 fprintf (file, "%d,",
5376 (optional_tbtab << 5) | ((info->first_fp_reg_save != 64) << 1));
5377
5378 /* 6 bitfields: function is interrupt handler, name present in
5379 proc table, function calls alloca, on condition directives
5380 (controls stack walks, 3 bits), saves condition reg, saves
5381 link reg. */
5382 /* The `function calls alloca' bit seems to be set whenever reg 31 is
5383 set up as a frame pointer, even when there is no alloca call. */
5384 fprintf (file, "%d,",
5385 ((optional_tbtab << 6)
5386 | ((optional_tbtab & frame_pointer_needed) << 5)
5387 | (info->cr_save_p << 1)
5388 | (info->lr_save_p)));
5389
5390 /* 3 bitfields: saves backchain, fixup code, number of fpr saved
5391 (6 bits). */
5392 fprintf (file, "%d,",
5393 (info->push_p << 7) | (64 - info->first_fp_reg_save));
5394
5395 /* 2 bitfields: spare bits (2 bits), number of gpr saved (6 bits). */
5396 fprintf (file, "%d,", (32 - first_reg_to_save ()));
5397
5398 if (optional_tbtab)
5399 {
5400 /* Compute the parameter info from the function decl argument
5401 list. */
5402 tree decl;
5403 int next_parm_info_bit = 31;
5404
5405 for (decl = DECL_ARGUMENTS (current_function_decl);
5406 decl; decl = DECL_CHAIN (decl))
5407 {
5408 rtx parameter = DECL_INCOMING_RTL (decl);
5409 machine_mode mode = GET_MODE (parameter);
5410
5411 if (REG_P (parameter))
5412 {
5413 if (SCALAR_FLOAT_MODE_P (mode))
5414 {
5415 int bits;
5416
5417 float_parms++;
5418
5419 switch (mode)
5420 {
5421 case E_SFmode:
5422 case E_SDmode:
5423 bits = 0x2;
5424 break;
5425
5426 case E_DFmode:
5427 case E_DDmode:
5428 case E_TFmode:
5429 case E_TDmode:
5430 case E_IFmode:
5431 case E_KFmode:
5432 bits = 0x3;
5433 break;
5434
5435 default:
5436 gcc_unreachable ();
5437 }
5438
5439 /* If only one bit will fit, don't or in this entry. */
5440 if (next_parm_info_bit > 0)
5441 parm_info |= (bits << (next_parm_info_bit - 1));
5442 next_parm_info_bit -= 2;
5443 }
5444 else
5445 {
5446 fixed_parms += ((GET_MODE_SIZE (mode)
5447 + (UNITS_PER_WORD - 1))
5448 / UNITS_PER_WORD);
5449 next_parm_info_bit -= 1;
5450 }
5451 }
5452 }
5453 }
5454
5455 /* Number of fixed point parameters. */
5456 /* This is actually the number of words of fixed point parameters; thus
5457 an 8 byte struct counts as 2; and thus the maximum value is 8. */
5458 fprintf (file, "%d,", fixed_parms);
5459
5460 /* 2 bitfields: number of floating point parameters (7 bits), parameters
5461 all on stack. */
5462 /* This is actually the number of fp registers that hold parameters;
5463 and thus the maximum value is 13. */
5464 /* Set parameters on stack bit if parameters are not in their original
5465 registers, regardless of whether they are on the stack? Xlc
5466 seems to set the bit when not optimizing. */
5467 fprintf (file, "%d\n", ((float_parms << 1) | (! optimize)));
5468
5469 if (optional_tbtab)
5470 {
5471 /* Optional fields follow. Some are variable length. */
5472
5473 /* Parameter types, left adjusted bit fields: 0 fixed, 10 single
5474 float, 11 double float. */
5475 /* There is an entry for each parameter in a register, in the order
5476 that they occur in the parameter list. Any intervening arguments
5477 on the stack are ignored. If the list overflows a long (max
5478 possible length 34 bits) then completely leave off all elements
5479 that don't fit. */
5480 /* Only emit this long if there was at least one parameter. */
5481 if (fixed_parms || float_parms)
5482 fprintf (file, "\t.long %d\n", parm_info);
5483
5484 /* Offset from start of code to tb table. */
5485 fputs ("\t.long ", file);
5486 ASM_OUTPUT_INTERNAL_LABEL_PREFIX (file, "LT");
5487 RS6000_OUTPUT_BASENAME (file, fname);
5488 putc ('-', file);
5489 rs6000_output_function_entry (file, fname);
5490 putc ('\n', file);
5491
5492 /* Interrupt handler mask. */
5493 /* Omit this long, since we never set the interrupt handler bit
5494 above. */
5495
5496 /* Number of CTL (controlled storage) anchors. */
5497 /* Omit this long, since the has_ctl bit is never set above. */
5498
5499 /* Displacement into stack of each CTL anchor. */
5500 /* Omit this list of longs, because there are no CTL anchors. */
5501
5502 /* Length of function name. */
5503 if (*fname == '*')
5504 ++fname;
5505 fprintf (file, "\t.short %d\n", (int) strlen (fname));
5506
5507 /* Function name. */
5508 assemble_string (fname, strlen (fname));
5509
5510 /* Register for alloca automatic storage; this is always reg 31.
5511 Only emit this if the alloca bit was set above. */
5512 if (frame_pointer_needed)
5513 fputs ("\t.byte 31\n", file);
5514
5515 fputs ("\t.align 2\n", file);
5516 }
5517 }
5518
5519 /* Arrange to define .LCTOC1 label, if not already done. */
5520 if (need_toc_init)
5521 {
5522 need_toc_init = 0;
5523 if (!toc_initialized)
5524 {
5525 switch_to_section (toc_section);
5526 switch_to_section (current_function_section ());
5527 }
5528 }
5529 }
5530
5531 /* -fsplit-stack support. */
5532
5533 /* A SYMBOL_REF for __morestack. */
5534 static GTY(()) rtx morestack_ref;
5535
5536 static rtx
5537 gen_add3_const (rtx rt, rtx ra, long c)
5538 {
5539 if (TARGET_64BIT)
5540 return gen_adddi3 (rt, ra, GEN_INT (c));
5541 else
5542 return gen_addsi3 (rt, ra, GEN_INT (c));
5543 }
5544
5545 /* Emit -fsplit-stack prologue, which goes before the regular function
5546 prologue (at local entry point in the case of ELFv2). */
5547
5548 void
5549 rs6000_expand_split_stack_prologue (void)
5550 {
5551 rs6000_stack_t *info = rs6000_stack_info ();
5552 unsigned HOST_WIDE_INT allocate;
5553 long alloc_hi, alloc_lo;
5554 rtx r0, r1, r12, lr, ok_label, compare, jump, call_fusage;
5555 rtx_insn *insn;
5556
5557 gcc_assert (flag_split_stack && reload_completed);
5558
5559 if (!info->push_p)
5560 {
5561 /* We need the -fsplit-stack prologue for functions that make
5562 tail calls. Tail calls don't count against crtl->is_leaf.
5563 Note that we are called inside a sequence. get_insns will
5564 just return that (as yet empty) sequence, so instead we
5565 access the function rtl with get_topmost_sequence. */
5566 for (insn = get_topmost_sequence ()->first; insn; insn = NEXT_INSN (insn))
5567 if (CALL_P (insn))
5568 break;
5569 if (!insn)
5570 return;
5571 }
5572
5573 if (global_regs[29])
5574 {
5575 error ("%qs uses register r29", "%<-fsplit-stack%>");
5576 inform (DECL_SOURCE_LOCATION (global_regs_decl[29]),
5577 "conflicts with %qD", global_regs_decl[29]);
5578 }
5579
5580 allocate = info->total_size;
5581 if (allocate > (unsigned HOST_WIDE_INT) 1 << 31)
5582 {
5583 sorry ("Stack frame larger than 2G is not supported for "
5584 "%<-fsplit-stack%>");
5585 return;
5586 }
5587 if (morestack_ref == NULL_RTX)
5588 {
5589 morestack_ref = gen_rtx_SYMBOL_REF (Pmode, "__morestack");
5590 SYMBOL_REF_FLAGS (morestack_ref) |= (SYMBOL_FLAG_LOCAL
5591 | SYMBOL_FLAG_FUNCTION);
5592 }
5593
5594 r0 = gen_rtx_REG (Pmode, 0);
5595 r1 = gen_rtx_REG (Pmode, STACK_POINTER_REGNUM);
5596 r12 = gen_rtx_REG (Pmode, 12);
5597 emit_insn (gen_load_split_stack_limit (r0));
5598 /* Always emit two insns here to calculate the requested stack,
5599 so that the linker can edit them when adjusting size for calling
5600 non-split-stack code. */
5601 alloc_hi = (-allocate + 0x8000) & ~0xffffL;
5602 alloc_lo = -allocate - alloc_hi;
5603 if (alloc_hi != 0)
5604 {
5605 emit_insn (gen_add3_const (r12, r1, alloc_hi));
5606 if (alloc_lo != 0)
5607 emit_insn (gen_add3_const (r12, r12, alloc_lo));
5608 else
5609 emit_insn (gen_nop ());
5610 }
5611 else
5612 {
5613 emit_insn (gen_add3_const (r12, r1, alloc_lo));
5614 emit_insn (gen_nop ());
5615 }
5616
5617 compare = gen_rtx_REG (CCUNSmode, CR7_REGNO);
5618 emit_insn (gen_rtx_SET (compare, gen_rtx_COMPARE (CCUNSmode, r12, r0)));
5619 ok_label = gen_label_rtx ();
5620 jump = gen_rtx_IF_THEN_ELSE (VOIDmode,
5621 gen_rtx_GEU (VOIDmode, compare, const0_rtx),
5622 gen_rtx_LABEL_REF (VOIDmode, ok_label),
5623 pc_rtx);
5624 insn = emit_jump_insn (gen_rtx_SET (pc_rtx, jump));
5625 JUMP_LABEL (insn) = ok_label;
5626 /* Mark the jump as very likely to be taken. */
5627 add_reg_br_prob_note (insn, profile_probability::very_likely ());
5628
5629 lr = gen_rtx_REG (Pmode, LR_REGNO);
5630 insn = emit_move_insn (r0, lr);
5631 RTX_FRAME_RELATED_P (insn) = 1;
5632 insn = emit_insn (gen_frame_store (r0, r1, info->lr_save_offset));
5633 RTX_FRAME_RELATED_P (insn) = 1;
5634
5635 insn = emit_call_insn (gen_call (gen_rtx_MEM (SImode, morestack_ref),
5636 const0_rtx, const0_rtx));
5637 call_fusage = NULL_RTX;
5638 use_reg (&call_fusage, r12);
5639 /* Say the call uses r0, even though it doesn't, to stop regrename
5640 from twiddling with the insns saving lr, trashing args for cfun.
5641 The insns restoring lr are similarly protected by making
5642 split_stack_return use r0. */
5643 use_reg (&call_fusage, r0);
5644 add_function_usage_to (insn, call_fusage);
5645 /* Indicate that this function can't jump to non-local gotos. */
5646 make_reg_eh_region_note_nothrow_nononlocal (insn);
5647 emit_insn (gen_frame_load (r0, r1, info->lr_save_offset));
5648 insn = emit_move_insn (lr, r0);
5649 add_reg_note (insn, REG_CFA_RESTORE, lr);
5650 RTX_FRAME_RELATED_P (insn) = 1;
5651 emit_insn (gen_split_stack_return ());
5652
5653 emit_label (ok_label);
5654 LABEL_NUSES (ok_label) = 1;
5655 }
5656
5657 /* We may have to tell the dataflow pass that the split stack prologue
5658 is initializing a register. */
5659
5660 void
5661 rs6000_live_on_entry (bitmap regs)
5662 {
5663 if (flag_split_stack)
5664 bitmap_set_bit (regs, 12);
5665 }
5666
5667 /* Emit -fsplit-stack dynamic stack allocation space check. */
5668
5669 void
5670 rs6000_split_stack_space_check (rtx size, rtx label)
5671 {
5672 rtx sp = gen_rtx_REG (Pmode, STACK_POINTER_REGNUM);
5673 rtx limit = gen_reg_rtx (Pmode);
5674 rtx requested = gen_reg_rtx (Pmode);
5675 rtx cmp = gen_reg_rtx (CCUNSmode);
5676 rtx jump;
5677
5678 emit_insn (gen_load_split_stack_limit (limit));
5679 if (CONST_INT_P (size))
5680 emit_insn (gen_add3_insn (requested, sp, GEN_INT (-INTVAL (size))));
5681 else
5682 {
5683 size = force_reg (Pmode, size);
5684 emit_move_insn (requested, gen_rtx_MINUS (Pmode, sp, size));
5685 }
5686 emit_insn (gen_rtx_SET (cmp, gen_rtx_COMPARE (CCUNSmode, requested, limit)));
5687 jump = gen_rtx_IF_THEN_ELSE (VOIDmode,
5688 gen_rtx_GEU (VOIDmode, cmp, const0_rtx),
5689 gen_rtx_LABEL_REF (VOIDmode, label),
5690 pc_rtx);
5691 jump = emit_jump_insn (gen_rtx_SET (pc_rtx, jump));
5692 JUMP_LABEL (jump) = label;
5693 }
5694
5695
5696 /* Return whether we need to always update the saved TOC pointer when we update
5697 the stack pointer. */
5698
5699 static bool
5700 rs6000_save_toc_in_prologue_p (void)
5701 {
5702 return (cfun && cfun->machine && cfun->machine->save_toc_in_prologue);
5703 }
5704
5705 #include "gt-rs6000-logue.h"