Hi,

the attached patch adds a new target hook allowing targets to keep
crtl->is_leaf true for leaf functions even if function profiling is
being used.

This e.g. helps s390 to omit generating a stack frame only due to
calling mcount.  On S/390 we call mcount before the function prologue
so the mcount stub has to take care of saving and restoring GPRs
anyway.

This is the result of a discussion with Jeff after trying to use the
profile_before_prologue instead of adding a new hook:
http://gcc.gnu.org/ml/gcc-patches/2013-10/msg00604.html

Tested on x86_64, s390, and s390x.

Ok for mainline?

Bye,

-Andreas-

2014-04-29  Andreas Krebbel  <andreas.kreb...@de.ibm.com>

        * target.def: Add new target hook.
        * doc/tm.texi: Regenerate.
        * targhooks.h (default_keep_leaf_when_profiled): Add prototype.
        * targhooks.c (default_keep_leaf_when_profiled): New function.

        * config/s390/s390.c (s390_keep_leaf_when_profiled): New function.
        (TARGET_KEEP_LEAF_WHEN_PROFILED): Define.

2014-04-29  Andreas Krebbel  <andreas.kreb...@de.ibm.com>

        * gcc.target/s390/leaf-profile.c: New testcase.

diff --git a/gcc/config/s390/s390.c b/gcc/config/s390/s390.c
index cc8f32e..556a701 100644
--- a/gcc/config/s390/s390.c
+++ b/gcc/config/s390/s390.c
@@ -10160,6 +10160,14 @@ s390_expand_builtin (tree exp, rtx target, rtx 
subtarget ATTRIBUTE_UNUSED,
     return const0_rtx;
 }
 
+/* We call mcount before the function prologue.  So a profiled leaf
+   function should stay a leaf function.  */
+
+static int
+s390_keep_leaf_when_profiled ()
+{
+  return 1;
+}
 
 /* Output assembly code for the trampoline template to
    stdio stream FILE.
@@ -12163,6 +12171,9 @@ s390_option_override (void)
 #undef TARGET_LIBCALL_VALUE
 #define TARGET_LIBCALL_VALUE s390_libcall_value
 
+#undef TARGET_KEEP_LEAF_WHEN_PROFILED
+#define TARGET_KEEP_LEAF_WHEN_PROFILED s390_keep_leaf_when_profiled
+
 #undef TARGET_FIXED_CONDITION_CODE_REGS
 #define TARGET_FIXED_CONDITION_CODE_REGS s390_fixed_condition_code_regs
 
diff --git a/gcc/doc/tm.texi b/gcc/doc/tm.texi
index b8ca17e..937c2d5 100644
--- a/gcc/doc/tm.texi
+++ b/gcc/doc/tm.texi
@@ -4953,6 +4953,10 @@ Define this macro if the code for function profiling 
should come before
 the function prologue.  Normally, the profiling code comes after.
 @end defmac
 
+@deftypefn {Target Hook} int TARGET_KEEP_LEAF_WHEN_PROFILED (void)
+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.
+@end deftypefn
+
 @node Tail Calls
 @subsection Permitting tail calls
 @cindex tail calls
diff --git a/gcc/doc/tm.texi.in b/gcc/doc/tm.texi.in
index d793d26..b42dd12 100644
--- a/gcc/doc/tm.texi.in
+++ b/gcc/doc/tm.texi.in
@@ -3963,6 +3963,8 @@ Define this macro if the code for function profiling 
should come before
 the function prologue.  Normally, the profiling code comes after.
 @end defmac
 
+@hook TARGET_KEEP_LEAF_WHEN_PROFILED
+
 @node Tail Calls
 @subsection Permitting tail calls
 @cindex tail calls
diff --git a/gcc/final.c b/gcc/final.c
index 8c6f6ee..cf649fb 100644
--- a/gcc/final.c
+++ b/gcc/final.c
@@ -4241,7 +4241,9 @@ leaf_function_p (void)
 {
   rtx insn;
 
-  if (crtl->profile || profile_arc_flag)
+  /* Some back-ends (e.g. s390) want leaf functions to stay leaf
+     functions even if they call mcount.  */
+  if (crtl->profile && !targetm.keep_leaf_when_profiled ())
     return 0;
 
   for (insn = get_insns (); insn; insn = NEXT_INSN (insn))
diff --git a/gcc/target.def b/gcc/target.def
index 3a64cd1..4c8ba85 100644
--- a/gcc/target.def
+++ b/gcc/target.def
@@ -2658,6 +2658,18 @@ The default version of this hook use the target macro\n\
  bool, (void),
  default_profile_before_prologue)
 
+/* Return true if a leaf function should stay leaf even with profiling
+   enabled.  */
+DEFHOOK
+(keep_leaf_when_profiled,
+ "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.",
+ int, (void),
+ default_keep_leaf_when_profiled)
+
 /* Modify and return the identifier of a DECL's external name,
    originally identified by ID, as required by the target,
    (eg, append @nn to windows32 stdcall function names).
diff --git a/gcc/targhooks.c b/gcc/targhooks.c
index 79491c7..8c84b64 100644
--- a/gcc/targhooks.c
+++ b/gcc/targhooks.c
@@ -1447,6 +1447,15 @@ default_get_reg_raw_mode (int regno)
   return reg_raw_mode[regno];
 }
 
+/* Return true if a leaf function should stay leaf even with profiling
+   enabled.  */
+
+int
+default_keep_leaf_when_profiled ()
+{
+  return 0;
+}
+
 /* Return true if the state of option OPTION should be stored in PCH files
    and checked by default_pch_valid_p.  Store the option's current state
    in STATE if so.  */
diff --git a/gcc/targhooks.h b/gcc/targhooks.h
index 9dd4c83..3de40de 100644
--- a/gcc/targhooks.h
+++ b/gcc/targhooks.h
@@ -195,6 +195,7 @@ extern int default_jump_align_max_skip (rtx);
 extern section * default_function_section(tree decl, enum node_frequency freq,
                                          bool startup, bool exit);
 extern enum machine_mode default_get_reg_raw_mode (int);
+extern int default_keep_leaf_when_profiled ();
 
 extern void *default_get_pch_validity (size_t *);
 extern const char *default_pch_valid_p (const void *, size_t);
diff --git a/gcc/testsuite/gcc.target/s390/leaf-profile.c 
b/gcc/testsuite/gcc.target/s390/leaf-profile.c
new file mode 100644
index 0000000..6047740
--- /dev/null
+++ b/gcc/testsuite/gcc.target/s390/leaf-profile.c
@@ -0,0 +1,10 @@
+/* { dg-do compile } */
+/* { dg-options "-O3 -march=z900 -pg" } */
+
+int
+foo ()
+{
+}
+/* Make sure no stack frame is generated.  */
+/* { dg-final { scan-assembler-not "ahi" { target s390-*-* } } } */
+/* { dg-final { scan-assembler-not "aghi" { target s390x-*-* } } } */

Reply via email to