25112 Rev. 3.06 September 2005

Software Optimization Guide for AMD64 Processors

4.8Using LEAVE for Function Epilogues

Optimization

The recommended optimization for function epilogues depends on whether the function allocates local variables.

If the function

Then

Allocates local variables

Replace the traditional function epilogue with the LEAVE instruction.

Does not allocate local variables

Do no use function prologues or epilogues. Access function

 

arguments and local variables through rSP.

Application

This optimization applies to:

32-bit software

64-bit software

Rationale

Functions That Allocate Local Variables

The LEAVE instruction is a single-byte instruction and saves 2 bytes of code space over the traditional epilogue. Replacing the traditional sequence with LEAVE also preserves decode bandwidth.

Functions That Do not Allocate Local Variables

Accessing function arguments and local variables directly through ESP frees EBP for use as a general-purpose register.

Background

The function arguments and local variables inside a function are referenced through a so-called frame pointer. In AMD64 code, the base pointer register (rBP) is customarily used as a frame pointer. You set up the frame pointer at the beginning of the function using a function prologue:

push

ebp

; Save old

frame

pointer.

mov

ebp, esp

; Initialize new

frame pointer.

sub

esp, n

;

Allocate

space

for

local variables (only if the

 

 

;

function

allocates

local variables).

Chapter 4

Instruction-Decoding Optimizations

83

Page 99
Image 99
AMD 250 manual Using Leave for Function Epilogues, Functions That Allocate Local Variables