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