tracing: gcc for x86 calling mcount with -fomit-frame-pointer
On x86 and x86_64, current "gcc -pg -fomit-frame-pointer" is not allowed. This experimental patch against:
http://mirrors.kernel.org/fedora/releases/12/Fedora/source/SRPMS/gcc-4.4.2-7.fc12.src.rpm
allows such a combination, via the command line options:
gcc --profile-before-prolog -fomit-frame-pointer
This turns on profiling (as if -pg), moves the "call mcount" to be the very first instruction of a profiled routine, and omits the frame pointer (unless some condition other than profiling requires a frame pointer.) Placing the "call mcount" first, before any other code, has some advantages. For instance, a postprocessor easily can modify a CALL whose destination is known, to skip past the "call mcount" at the entry point.
The current glibc implementation of mcount relies on a frame pointer. At least one recent change to Linux kernel traceback for tracing also relies on a frame pointer. So still there are conflicts, but they are different.
gcc/c-opts.c +4 -0
@@ -746,6 +746,10 @@ c_common_handle_option (size_t scode, const char *arg, int value) cpp_opts->preprocessed = value;break ; +case OPT_fprofile_before_prolog: + flag_profile_before_prolog = value; +break ; +case OPT_freplace_objc_classes: flag_replace_objc_classes = value;break ;
gcc/c.opt +0 -0
@@ -720,6 +720,10 @@ fpreprocessed
C ObjC C++ ObjC++
Treat the input file as already preprocessed
+fprofile-before-prolog
+C ObjC C++ ObjC++
+Generate profiling code before the function prolog
+
freplace-objc-classes
ObjC ObjC++
Used in Fix-and-Continue mode to indicate that object files may be swapped in at runtime
gcc/config/i386/i386.c +0 -0
@@ -7496,7 +7496,7 @@ ix86_frame_pointer_required (void)
|| ix86_current_function_calls_tls_descriptor))
return 1;- if (crtl->profile)
+ if (crtl->profile && !crtl->profile_before_prolog)
return 1;
return 0;
gcc/config/i386/linux.h +0 -0
@@ -55,7 +55,7 @@ along with GCC; see the file COPYING3. If not seeframe, so we cannot allow profiling without a frame pointer. */
#undef SUBTARGET_FRAME_POINTER_REQUIRED
-#define SUBTARGET_FRAME_POINTER_REQUIRED crtl->profile
+#define SUBTARGET_FRAME_POINTER_REQUIRED (crtl->profile && !crtl->profile_before_prolog)
#undef SIZE_TYPE
#define SIZE_TYPE "unsigned int"
gcc/final.c +0 -0
@@ -1512,12 +1512,8 @@ final_start_function (rtx first ATTRIBUTE_UNUSED, FILE *file,leaf_renumber_regs (first);
#endif
- /* The Sun386i and perhaps other machines don't work right
- if the profiling code comes after the prologue. */
-#ifdef PROFILE_BEFORE_PROLOGUE
- if (crtl->profile)
+ if (crtl->profile && crtl->profile_before_prolog)
profile_function (file);
-#endif /* PROFILE_BEFORE_PROLOGUE */
#if defined (DWARF2_UNWIND_INFO) && defined (HAVE_prologue)
if (dwarf2out_do_frame ())
@@ -1559,10 +1555,8 @@ final_start_function (rtx first ATTRIBUTE_UNUSED, FILE *file, static voidprofile_after_prologue (FILE *file ATTRIBUTE_UNUSED)
{
-#ifndef PROFILE_BEFORE_PROLOGUE
- if (crtl->profile)
+ if (crtl->profile && !crtl->profile_before_prolog)
profile_function (file);
-#endif /* not PROFILE_BEFORE_PROLOGUE */
}
static void
@@ -3897,7 +3891,7 @@ leaf_function_p (void)
rtx insn;
rtx link;
- if (crtl->profile || profile_arc_flag)
+ if ((crtl->profile && !crtl->profile_before_prolog) || profile_arc_flag)
return 0;
for (insn = get_insns (); insn; insn = NEXT_INSN (insn))
gcc/flags.h +0 -0
@@ -189,6 +189,9 @@ extern int flag_permissive;
/* Nonzero if we are compiling code for a shared library, zero for
executable. */
+/* Nonzero means generate profiling code before the function prolog. */ +extern int flag_profile_before_prolog;
+
extern int flag_shlib;
/* -dA causes debug information to be produced in
gcc/function.c +5 -1
@@ -4362,9 +4362,13 @@ expand_function_start (tree subr)valid operands of arithmetic insns. */ init_recog_no_volatile (); +/* flag_profile_before_prolog: also set ->profile as temporary hack +to evade conflict between -pg and -fomit-frame-pointer. */ crtl->profile - = (profile_flag + = ((profile_flag || flag_profile_before_prolog) && !DECL_NO_INSTRUMENT_FUNCTION_ENTRY_EXIT (subr)); + crtl->profile_before_prolog + = (crtl->profile && flag_profile_before_prolog); crtl->limit_stack = (stack_limit_rtx !=NULL_RTX && !DECL_NO_LIMIT_STACK (subr));
@@ -5016,13 +5020,11 @@ thread_prologue_and_epilogue_insns (void)record_insns (seq,NULL , &prologue_insn_hash);emit_note (NOTE_INSN_PROLOGUE_END ); -#ifndef PROFILE_BEFORE_PROLOGUE/* Ensure that instructions are not moved into the prologue when
profiling is on. The call to the profiling routine can be emitted within the live range of a call-clobbered register. */
- if (crtl->profile)
+ if (crtl->profile && !crtl->profile_before_prolog)
emit_insn (gen_blockage ());
-#endif
seq = get_insns ();
end_sequence ();gcc/function.h +3 -0
@@ -396,6 +396,9 @@ struct rtl_data GTY(())/* Nonzero if profiling code should be generated. */ bool profile; +/* Nonzero if profiling code should be generated before prolog. */ +bool profile_before_prolog; +/* Nonzero if the current function uses the constant pool. */ bool uses_const_pool;
gcc/toplev.c +0 -0
@@ -290,6 +290,9 @@ int flag_pedantic_errors = 0;
int flag_permissive = 0;
+/* Nonzero means generate profiling code before the function prolog. */ +int flag_profile_before_prolog = 0;
+
/* -dA causes debug commentary information to be produced in
the generated assembly code (to make it more readable). This option is generally only of use to those who actually need to read the
@@ -1687,6 +1690,11 @@ process_options (void)
if (warn_unused_value == -1)
warn_unused_value = warn_unused;
+#ifdef PROFILE_BEFORE_PROLOG
+ /* Forced on some architectures. */
+ flag_profile_before_prolog = 1;
+#endif
+
/* Allow the front end to perform consistency checks and do further
initialization based on the command line options. This hook also sets the original filename if appropriate (e.g. foo.i -> foo.c)
Quoted 12 lines · John Reiser
On x86 and
x86_64, current "gcc -pg -fomit-frame-pointer" is not allowed. This experimental patch against:
http://mirrors.kernel.org/fedora/releases/12/Fedora/source/SRPMS/gcc-4.4.2-7.fc12.src.rpm
allows such a combination, via the command line options:
gcc --profile-before-prolog -fomit-frame-pointer
This turns on profiling (as if -pg), moves the "call mcount" to be the very first instruction of a profiled routine, and omits the frame pointer (unless some condition other than profiling requires a frame pointer.) Placing the "call mcount" first, before any other code, has some advantages. For instance, a postprocessor easily can modify a CALL whose destination is known, to skip past the "call mcount" at the entry point.
Quoted 3 lines
The current glibc implementation of mcount relies on a frame pointer. At least one recent change to Linux kernel traceback for tracing also relies on a frame pointer. So still there are conflicts, but they are different.
I submitted a similar patch for all of this some time to gcc, and also resubmitted it recently. It doesn't seem to be very high up the priority list of the gcc people.
It missed their "merge window" (although lots of other changes went in), but they told me it's queued in some way for the next stage 1. We'll see if that's true or not.
I also have a glibc patch for this.
-Andi
Signature · 1 line
[email protected] -- Speaking for myself only.
Quoted 11 lines · Andi Kleen
Quoted 3 lines · John Reiser
On x86 and
x86_64, current "gcc -pg -fomit-frame-pointer" is not allowed. This experimental patch against:http://mirrors.kernel.org/fedora/releases/12/Fedora/source/SRPMS/gcc-4.4.2-7.fc12.src.rpmI submitted a similar patch for all of this some time [ago] to gcc, and also resubmitted it recently. ... they told me it's queued in some way for the next stage 1. ...
I also have a glibc patch for this.
I found http://gcc.gnu.org/ml/gcc/2009-11/msg00565.html . Where is the glibc patch? Reports by users of successful experiences can help shorten the cycle for official changes.