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