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