[ < ] [ > ]   [ << ] [ Up ] [ >> ]         [Top] [Contents] [Index] [ ? ]

21.8.11 Generating Code for Profiling

These macros will help you generate code for profiling.

FUNCTION_PROFILER (file, labelno)
A C statement or compound statement to output to file some assembler code to call the profiling subroutine mcount.

The details of how mcount expects to be called are determined by your operating system environment, not by GCC. To figure them out, compile a small program for profiling using the system's installed C compiler and look at the assembler code that results.

Older implementations of mcount expect the address of a counter variable to be loaded into some register. The name of this variable is `LP' followed by the number labelno, so you would generate the name using `LP%d' in a fprintf.

PROFILE_HOOK
A C statement or compound statement to output to file some assembly code to call the profiling subroutine mcount even the target does not support profiling.

NO_PROFILE_COUNTERS
Define this macro if the mcount subroutine on your system does not need a counter variable allocated for each function. This is true for almost all modern implementations. If you define this macro, you must not use the labelno argument to FUNCTION_PROFILER.

PROFILE_BEFORE_PROLOGUE
Define this macro if the code for function profiling should come before the function prologue. Normally, the profiling code comes after.

FUNCTION_BLOCK_PROFILER (file, labelno)
A C statement or compound statement to output to file some assembler code to initialize basic-block profiling for the current object module. The global compile flag profile_block_flag distinguishes two profile modes.

profile_block_flag != 2
Output code to call the subroutine __bb_init_func once per object module, passing it as its sole argument the address of a block allocated in the object module.

The name of the block is a local symbol made with this statement:

 
ASM_GENERATE_INTERNAL_LABEL (buffer, "LPBX", 0);

Of course, since you are writing the definition of ASM_GENERATE_INTERNAL_LABEL as well as that of this macro, you can take a short cut in the definition of this macro and use the name that you know will result.

The first word of this block is a flag which will be nonzero if the object module has already been initialized. So test this word first, and do not call __bb_init_func if the flag is nonzero. BLOCK_OR_LABEL contains a unique number which may be used to generate a label as a branch destination when __bb_init_func will not be called.

Described in assembler language, the code to be output looks like:

 
  cmp (LPBX0),0
  bne local_label
  parameter1 <- LPBX0
  call __bb_init_func
local_label:

profile_block_flag == 2
Output code to call the subroutine __bb_init_trace_func and pass two parameters to it. The first parameter is the same as for __bb_init_func. The second parameter is the number of the first basic block of the function as given by BLOCK_OR_LABEL. Note that __bb_init_trace_func has to be called, even if the object module has been initialized already.

Described in assembler language, the code to be output looks like:
 
parameter1 <- LPBX0
parameter2 <- BLOCK_OR_LABEL
call __bb_init_trace_func

BLOCK_PROFILER (file, blockno)
A C statement or compound statement to output to file some assembler code to increment the count associated with the basic block number blockno. The global compile flag profile_block_flag distinguishes two profile modes.

profile_block_flag != 2
Output code to increment the counter directly. Basic blocks are numbered separately from zero within each compilation. The count associated with block number blockno is at index blockno in a vector of words; the name of this array is a local symbol made with this statement:

 
ASM_GENERATE_INTERNAL_LABEL (buffer, "LPBX", 2);

Of course, since you are writing the definition of ASM_GENERATE_INTERNAL_LABEL as well as that of this macro, you can take a short cut in the definition of this macro and use the name that you know will result.

Described in assembler language, the code to be output looks like:

 
inc (LPBX2+4*BLOCKNO)

profile_block_flag == 2
Output code to initialize the global structure __bb and call the function __bb_trace_func, which will increment the counter.

__bb consists of two words. In the first word, the current basic block number, as given by BLOCKNO, has to be stored. In the second word, the address of a block allocated in the object module has to be stored. The address is given by the label created with this statement:

 
ASM_GENERATE_INTERNAL_LABEL (buffer, "LPBX", 0);

Described in assembler language, the code to be output looks like:
 
move BLOCKNO -> (__bb)
move LPBX0 -> (__bb+4)
call __bb_trace_func

FUNCTION_BLOCK_PROFILER_EXIT (file)
A C statement or compound statement to output to file assembler code to call function __bb_trace_ret. The assembler code should only be output if the global compile flag profile_block_flag == 2. This macro has to be used at every place where code for returning from a function is generated (e.g. FUNCTION_EPILOGUE). Although you have to write the definition of FUNCTION_EPILOGUE as well, you have to define this macro to tell the compiler, that the proper call to __bb_trace_ret is produced.

MACHINE_STATE_SAVE (id)
A C statement or compound statement to save all registers, which may be clobbered by a function call, including condition codes. The asm statement will be mostly likely needed to handle this task. Local labels in the assembler code can be concatenated with the string id, to obtain a unique label name.

Registers or condition codes clobbered by FUNCTION_PROLOGUE or FUNCTION_EPILOGUE must be saved in the macros FUNCTION_BLOCK_PROFILER, FUNCTION_BLOCK_PROFILER_EXIT and BLOCK_PROFILER prior calling __bb_init_trace_func, __bb_trace_ret and __bb_trace_func respectively.

MACHINE_STATE_RESTORE (id)
A C statement or compound statement to restore all registers, including condition codes, saved by MACHINE_STATE_SAVE.

Registers or condition codes clobbered by FUNCTION_PROLOGUE or FUNCTION_EPILOGUE must be restored in the macros FUNCTION_BLOCK_PROFILER, FUNCTION_BLOCK_PROFILER_EXIT and BLOCK_PROFILER after calling __bb_init_trace_func, __bb_trace_ret and __bb_trace_func respectively.

BLOCK_PROFILER_CODE
A C function or functions which are needed in the library to support block profiling.

TARGET_ALLOWS_PROFILING_WITHOUT_FRAME_POINTER
On some targets, it is impossible to use profiling when the frame pointer has been omitted. For example, on x86 GNU/Linux systems, the mcount routine provided by the GNU C Library finds the address of the routine that called the routine that called mcount by looking in the immediate caller's stack frame. If the immediate caller has no frame pointer, this lookup will fail.

By default, GCC assumes that the target does allow profiling when the frame pointer is omitted. This macro should be defined to a C expression that evaluates to false if the target does not allow profiling when the frame pointer is omitted.


[ < ] [ > ]   [ << ] [ Up ] [ >> ]         [Top] [Contents] [Index] [ ? ]

This document was generated by Vincent Chung on June, 26 2001 using texi2html