85359c
2014-05-05  Andreas Krebbel  <Andreas.Krebbel@de.ibm.com>
85359c
85359c
	* target.def: Add new target hook.
85359c
	* doc/tm.texi: Regenerate.
85359c
	* targhooks.h (default_keep_leaf_when_profiled): Add prototype.
85359c
	* targhooks.c (default_keep_leaf_when_profiled): New function.
85359c
85359c
2015-04-23  Anton Blanchard  <anton@samba.org>
85359c
85359c
	* config/rs6000/rs6000.c (rs6000_output_function_prologue): No
85359c
	need for -mprofile-kernel to save LR to stack.
85359c
85359c
2016-01-21  Anton Blanchard  <anton@samba.org>
85359c
	    Bill Schmidt  <wschmidt@linux.vnet.ibm.com>
85359c
85359c
	PR target/63354
85359c
	* config/rs6000/linux64.h (TARGET_KEEP_LEAF_WHEN_PROFILED): New
85359c
	#define.
85359c
	* config/rs6000/rs6000.c (rs6000_keep_leaf_when_profiled): New
85359c
	function.
85359c
85359c
	* gcc.target/powerpc/pr63354.c:  New test.
85359c
85359c
--- gcc/doc/tm.texi
85359c
+++ gcc/doc/tm.texi
85359c
@@ -4953,6 +4953,10 @@ Define this macro if the code for function profiling should come before
85359c
 the function prologue.  Normally, the profiling code comes after.
85359c
 @end defmac
85359c
 
85359c
+@deftypefn {Target Hook} bool TARGET_KEEP_LEAF_WHEN_PROFILED (void)
85359c
+This target hook returns true if the target wants the leaf flag for the current function to stay true even if it calls mcount.  This might make sense for targets using the leaf flag only to determine whether a stack frame needs to be generated or not and for which the call to mcount is generated before the function prologue.
85359c
+@end deftypefn
85359c
+
85359c
 @node Tail Calls
85359c
 @subsection Permitting tail calls
85359c
 @cindex tail calls
85359c
--- gcc/doc/tm.texi.in
85359c
+++ gcc/doc/tm.texi.in
85359c
@@ -3963,6 +3963,8 @@ Define this macro if the code for function profiling should come before
85359c
 the function prologue.  Normally, the profiling code comes after.
85359c
 @end defmac
85359c
 
85359c
+@hook TARGET_KEEP_LEAF_WHEN_PROFILED
85359c
+
85359c
 @node Tail Calls
85359c
 @subsection Permitting tail calls
85359c
 @cindex tail calls
85359c
--- gcc/final.c
85359c
+++ gcc/final.c
85359c
@@ -4241,7 +4241,9 @@ leaf_function_p (void)
85359c
 {
85359c
   rtx insn;
85359c
 
85359c
-  if (crtl->profile || profile_arc_flag)
85359c
+  /* Some back-ends (e.g. s390) want leaf functions to stay leaf
85359c
+     functions even if they call mcount.  */
85359c
+  if (crtl->profile && !targetm.keep_leaf_when_profiled ())
85359c
     return 0;
85359c
 
85359c
   for (insn = get_insns (); insn; insn = NEXT_INSN (insn))
85359c
--- gcc/target.def
85359c
+++ gcc/target.def
85359c
@@ -2658,6 +2658,18 @@ The default version of this hook use the target macro\n\
85359c
  bool, (void),
85359c
  default_profile_before_prologue)
85359c
 
85359c
+/* Return true if a leaf function should stay leaf even with profiling
85359c
+   enabled.  */
85359c
+DEFHOOK
85359c
+(keep_leaf_when_profiled,
85359c
+ "This target hook returns true if the target wants the leaf flag for\
85359c
+ the current function to stay true even if it calls mcount.  This might\
85359c
+ make sense for targets using the leaf flag only to determine whether a\
85359c
+ stack frame needs to be generated or not and for which the call to\
85359c
+ mcount is generated before the function prologue.",
85359c
+ bool, (void),
85359c
+ default_keep_leaf_when_profiled)
85359c
+
85359c
 /* Modify and return the identifier of a DECL's external name,
85359c
    originally identified by ID, as required by the target,
85359c
    (eg, append @nn to windows32 stdcall function names).
85359c
--- gcc/targhooks.c
85359c
+++ gcc/targhooks.c
85359c
@@ -1447,6 +1447,15 @@ default_get_reg_raw_mode (int regno)
85359c
   return reg_raw_mode[regno];
85359c
 }
85359c
 
85359c
+/* Return true if a leaf function should stay leaf even with profiling
85359c
+   enabled.  */
85359c
+
85359c
+bool
85359c
+default_keep_leaf_when_profiled ()
85359c
+{
85359c
+  return false;
85359c
+}
85359c
+
85359c
 /* Return true if the state of option OPTION should be stored in PCH files
85359c
    and checked by default_pch_valid_p.  Store the option's current state
85359c
    in STATE if so.  */
85359c
--- gcc/targhooks.h
85359c
+++ gcc/targhooks.h
85359c
@@ -188,6 +188,7 @@ extern section * default_function_sectio
85359c
 					  bool startup, bool exit);
85359c
 extern enum machine_mode default_dwarf_frame_reg_mode (int);
85359c
 extern enum machine_mode default_get_reg_raw_mode(int);
85359c
+extern bool default_keep_leaf_when_profiled ();
85359c
 
85359c
 extern void *default_get_pch_validity (size_t *);
85359c
 extern const char *default_pch_valid_p (const void *, size_t);
85359c
--- gcc/config/rs6000/rs6000.c
85359c
+++ gcc/config/rs6000/rs6000.c
85359c
@@ -24433,7 +24433,6 @@ rs6000_output_function_prologue (FILE *file,
85359c
       gcc_assert (!TARGET_32BIT);
85359c
 
85359c
       asm_fprintf (file, "\tmflr %s\n", reg_names[0]);
85359c
-      asm_fprintf (file, "\tstd %s,16(%s)\n", reg_names[0], reg_names[1]);
85359c
 
85359c
       /* In the ELFv2 ABI we have no compiler stack word.  It must be
85359c
 	 the resposibility of _mcount to preserve the static chain
85359c
--- gcc/config/rs6000/linux64.h
85359c
+++ gcc/config/rs6000/linux64.h
85359c
@@ -59,6 +59,9 @@ extern int dot_symbols;
85359c
 
85359c
 #define TARGET_PROFILE_KERNEL profile_kernel
85359c
 
85359c
+#undef TARGET_KEEP_LEAF_WHEN_PROFILED
85359c
+#define TARGET_KEEP_LEAF_WHEN_PROFILED rs6000_keep_leaf_when_profiled
85359c
+
85359c
 #define TARGET_USES_LINUX64_OPT 1
85359c
 #ifdef HAVE_LD_LARGE_TOC
85359c
 #undef TARGET_CMODEL
85359c
--- gcc/config/rs6000/rs6000.c
85359c
+++ gcc/config/rs6000/rs6000.c
85359c
@@ -26237,6 +26237,14 @@ rs6000_output_function_prologue (FILE *file,
85359c
   rs6000_pic_labelno++;
85359c
 }
85359c
 
85359c
+/* -mprofile-kernel code calls mcount before the function prolog,
85359c
+   so a profiled leaf function should stay a leaf function.  */
85359c
+static bool
85359c
+rs6000_keep_leaf_when_profiled ()
85359c
+{
85359c
+  return TARGET_PROFILE_KERNEL;
85359c
+}
85359c
+
85359c
 /* Non-zero if vmx regs are restored before the frame pop, zero if
85359c
    we restore after the pop when possible.  */
85359c
 #define ALWAYS_RESTORE_ALTIVEC_BEFORE_POP 0
85359c
--- /dev/null
85359c
+++ gcc/testsuite/gcc.target/powerpc/pr63354.c
85359c
@@ -0,0 +1,12 @@
85359c
+/* Verify that we don't stack a frame for leaf functions when using
85359c
+   -pg -mprofile-kernel.  */
85359c
+
85359c
+/* { dg-do compile { target { powerpc64*-*-* } } } */
85359c
+/* { dg-options "-O2 -pg -mprofile-kernel" } */
85359c
+/* { dg-require-effective-target lp64 } */
85359c
+/* { dg-final { scan-assembler-not "mtlr" } } */
85359c
+
85359c
+int foo(void)
85359c
+{
85359c
+  return 1;
85359c
+}