1. Field of the Invention
This invention relates in general to the field of microprocessors, and more particularly to a method and apparatus for performing branch prediction on far jump and far call instructions.
2. Description of the Related Art
In information handling systems computer instructions are typically stored in successive addressable locations within a memory. When processed by a Central Processing Unit (CPU), the instructions are fetched from these consecutive memory locations and executed. Each time an instruction is fetched from memory, a program counter within the CPU is incremented so that it contains the address of the next instruction in the sequence. Fetching of an instruction, incrementing of the program counter, and execution of the instruction continue linearly through memory until a program control instruction such as a jump-on-condition, a non-conditional jump, or a call instruction is encountered.
A program control instruction, when executed, changes the address in the program counter and causes the flow of control to be altered. In other words, program control instructions specify conditions for altering the contents of the program counter. The change in the value of the program counter as a result of the execution of a program control instruction causes a break in the otherwise successive sequence of instruction execution. This is an important feature in digital computers since it provides for programmable control over the flow of instruction execution and a capability for branching to different portions of a program.
A non-conditional jump instruction causes the CPU to unconditionally change the contents of the program counter to a specific value, i.e., to the target address for the instruction where the program is to continue execution. A Test-and-Jump instruction, or Conditional Jump instruction, conditionally causes the CPU to test the contents of a status register, or possibly compare two values, and either continue sequential execution or jump to a new address, called the target address, based on the outcome of the test or comparison. A Call instruction causes the CPU to unconditionally jump to a new target address and also saves the value of the program counter to allow the CPU to return to the program location it is leaving. A Return instruction causes the CPU to retrieve the value of the program counter that was saved by the last Call instruction, and return program flow back to the retrieved instruction address.
In early microprocessors, execution of program control instructions did not impose significant processing delays because such microprocessors were designed to execute only one instruction at a time. Consequently, no penalties were incurred if the instruction being executed was a program control instruction, regardless of whether execution of the instruction determined if it should branch or not. Since only one instruction was capable of being executed, the same delays were experienced by both sequential and branch instructions.
However, modern microprocessors are not so simple. Rather, it is common for modern microprocessors to operate on several instructions at the same time, within different blocks or pipeline stages of the microprocessor. Hennessy and Patterson define pipelining as, “an implementation technique whereby multiple instructions are overlapped in execution.” Computer Architecture: A Quantitative Approach, second edition, by John L. Hennessy and David A. Patterson, Morgan Kaufmann Publishers, San Francisco, Calif., 1996. The authors go on to provide the following excellent illustration of pipelining: “A pipeline is like an assembly line. In an automobile assembly line, there are many steps, each contributing something to the construction of the car. Each step operates in parallel with the other steps, though on a different car. In a computer pipeline, each step in the pipeline completes a part of an instruction. Like the assembly line, different steps are completing different parts of the different instructions in parallel. Each of these steps is called a pipe stage or a pipe segment. The stages are connected one to the next to form a pipe-instructions enter at one end, progress through the stages, and exit at the other end, just as cars would in an assembly line.”
Thus, in a present day microprocessor, instructions are fetched into one end of the pipeline, and then they proceed through successive pipeline stages until they complete execution. In such pipelined microprocessors it is not known whether a branch instruction will alter program flow until the instruction reaches a late stage in the pipeline. But to stall fetching of instructions while allowing the branch instruction to proceed through the pipeline until it is determined whether or not program flow is altered is inefficient.
To alleviate this problem, many pipelined microprocessors use branch prediction mechanisms in an early stage of the pipeline that predict the outcome of branch instructions, and then fetch subsequent instructions according to the branch prediction. If the branch prediction is correct, then the aforementioned inefficiency is overcome. If the branch prediction is incorrect, then the pipeline must be flushed of those instructions resulting from the incorrect branch prediction and refilled with instructions associated with the correct outcome of the branch.
There are two kinds of jump instructions: near jump instructions branch to an address within the same data segment; far jump instructions branch to an address in a different data segment. Similarly, near call instructions branch to an address within the same data segment, and far call instructions branch to an address in a different data segment.
In earlier X86 pipeline microprocessors, the pipeline was stalled whenever a far jump or far call instruction is executed until the instruction proceeds through the pipeline to the point that its target address is computed. This is because computation of a target address for a far jump or far call instruction requires that a new code segment descriptor be loaded into the code segment descriptor register of the microprocessor. (The term “far jump/call” is used collectively herein to indicate a far jump or far call instruction.) The far jump/call instruction prescribes a new code segment selector along with an offset. The code segment selector designates the new code segment descriptor. The new code segment descriptor includes a new code segment base address to which the offset is added to determine the far jump/call target address. Once this target address has been computed, it is provided to the NSIP so that subsequent instructions beginning at the target address can be fetched and executed.
In addition to specifying the new code segment base address, a code segment descriptor specifies a default length (i.e. address mode) for all effective addresses and operands (i.e. operand mode) referenced by instructions within the respective code segment. More particularly, in an x86-compatible microprocessor, the default length, or operation size, is specified in a bit of the segment descriptor known as the D bit. If the D bit is set, then default 32-bit addresses/operands are prescribed, whereas if the D bit is not set, then default 16-bit addresses/operands are prescribed.
As briefly referenced earlier, a disadvantage of prior microprocessor technology is that the pipeline is stalled to allow for computation of the target address corresponding to a far jump/call instruction. Unfortunately, the execution of all far jumps/calls incurs a penalty that is roughly equivalent to the number of stages in the pipeline between the stage where a far jump/call instruction is fetched and the stage where it is executed.
Earlier X86-compatible microprocessors did not perform any type of speculative branch prediction for far jumps/calls. More recent x86-compatible microprocessors do perform speculative branches for far jumps/calls, but the scope of the associated branch predictions is prescribed simply in terms of a branch target address; it is assumed that the state of the D bit does not change.
The present inventors have observed that many application programs employ far jump/call instructions to change default size of addresses/operands (i.e., the state of the D bit) used for subsequent instructions within a program flow. Yet when such instructions are executed according to present day far jump/call prediction techniques, the result is that the pipeline must be flushed when the new default address/operand size is determined (i.e., when the state of the D bit is accessed from the specified segment descriptor) because pipeline stage logic operating on instructions in preceding pipeline stages—albeit the instructions have been fetched from the correct target address—has performed address/operand calculations using the wrong default address/operand size.
Therefore, what is needed is a technique for performing branch prediction on far jumps and far calls in a manner which reduces the pipeline flushing penalties associated with far jumps and calls.
In accordance with one embodiment of the present invention, a microprocessor is provided for processing instructions and for speculatively executing a plurality of far jump-call instructions. The microprocessor includes a memory for storing instructions and a far jump-call target buffer for storing a default address/operand size corresponding to each of a plurality of previously executed far jump-call instructions. The microprocessor also includes instruction fetch logic, coupled to the memory and the far jump-call target buffer, for fetching a far jump-call instruction from the memory thus providing a fetched far jump-call instruction. The far jump-call target buffer provides the pipeline with a default address/operand size corresponding to the fetched far jump-call instruction, thus providing a speculative default address/operand size.
In accordance with another embodiment of the present invention, a method is provided for speculatively executing a plurality of far jump-call instructions in a microprocessor including a pipeline for processing instructions. The method includes storing, in a far jump-call target buffer, a default address/operand size corresponding to each of a plurality of previously executed far jump/call instructions. The method also includes fetching a far jump-call instruction from an instruction memory thus providing a fetched far jump-call instruction. The method further includes retrieving, from the far jump-call target buffer, a default address/operand size corresponding to the fetched far jump-call instruction, thus providing a speculative default address/operand size. The method still further includes speculatively executing the fetched far jump-call instruction employing the speculative default address/operand size. The method also includes propagating the fetched far jump-call instruction through the pipeline until the fetched far jump-call instruction is executed and resolved to provide an actual address/operand size. The method further includes comparing the actual address/operand size with the speculative default address/operand size, and flushing the pipeline if the actual address/operand size is not the same as the speculative default address/operand size. The method still further includes continuing to process instructions without flushing the pipeline if the actual address/operand size is the same as the speculative default address/operand size.
Other features and advantages of the present invention will become apparent upon study of the remaining portions of the specification and drawings.
These and other objects, features, and advantages of the present invention will become better understood with regard to the following description, and accompanying drawings where:
The following description is presented to enable one of ordinary skill in the art to make and use the present invention as provided within the context of a particular application and its requirements. Various modifications to the preferred embodiment will, however, be apparent to one skilled in the art, and the general principles defined herein may be applied to other embodiments. Therefore, the present invention is not intended to be limited to the particular embodiments shown and described herein, but is to be accorded the widest scope consistent with the principles and novel features herein disclosed.
Operationally, fetch stage 105 fetches macro instructions from memory (not shown) that are to be executed by microprocessor 100. Translate stage 110 translates the fetched macro instructions into associated micro instructions.
Each micro instruction directs microprocessor 100 to perform a specific subtask related to accomplishment of an overall operation specified by a fetched macro instruction. Register stage 115 retrieves operands specified by the micro instructions from a register file (not shown) for use by later stages in the pipeline. Address stage 120 calculates memory addresses specified by the micro instructions to be used in data storage and retrieval operations. Data/ALU stage 125 either performs arithmetic logic unit (ALU) operations on data retrieved from the register file, or reads/writes data from/to memory using the memory address calculated in address stage 120. Write back stage 130 writes the result of a data read operation, or an ALU operation, to the register file. Thus, to review, macro instructions are fetched by fetch stage 105 and are translated into micro instructions by translate stage 110. The translated micro instructions proceed through stages 115-130 for execution. Pipeline operation is thus provided by microprocessor 100.
Translate stage 110 employs conventional branch prediction to increase the efficiency of the pipeline as discussed earlier. A significant disadvantage of this conventional microprocessor technology is that the pipeline is flushed whenever the execution logic determines a default address/operand size from accessing a new segment descriptor, although instructions in preceding pipeline stages have been properly fetched according to a correctly predicted target address.
Current x86 pipelined microprocessors are known to handle far jump/call instructions by either 1) not performing any type of speculative branch prediction or 2) performing speculative branches which are prescribed simply in terms of a branch target address. For example, the target address taken the last time the branch was taken is recorded in a conventional branch target buffer. The inventors of the technology disclosed herein have recognized that, particularly with regard to legacy code, many far jumps and far calls are executed merely to change address/operand mode (i.e. instruction length), for example from 16 bit to 32 bit and vice versa. In the absence of far jump branch prediction, a penalty is incurred each time a far jump/call is executed. With conventional branch prediction techniques it is highly likely that a greater penalty is incurred when a far jump/call is resolved and it is found that the state of the D bit has changed.
To overcome these limitations, the microprocessor according to the present invention includes a dedicated far branch target buffer BTB which stores not only branch target addresses but also default address/operation sizes for far jump/call instructions that have been fetched from memory. In the particular embodiment discussed subsequently, the far branch target buffer is a BTB dedicated to far branch instructions. It should be appreciated that a far branch target buffer can be integrated with a near branch target buffer within the spirit of this disclosure. When a far jump/call is encountered by the disclosed microprocessor, a corresponding speculative code segment base, speculative offset, and speculative D bit are provided by the far branch target buffer. The speculative code segment base, speculative offset, and speculative D bit may also be referred to as the predicted code segment base, predicted offset, and predicted D bit, respectively. The code segment base and offset are provided to fetch logic so that subsequent instructions can be speculatively fetched from the resulting speculative jump target address. The D bit is provided to subsequent pipeline stages for the processing of effective addresses and operands associated with the subsequent instructions.
To provide more detail,
Far Jump/Call Target Buffer 230 is coupled to instruction pointer 220. In this manner the CS base and Offset associated with particular far jump/call branch instructions are provided to the instruction pointer 220 to enable fetching of designated targets. The D bit associated with instruction pointers and opcodes 225 reaching Fetch Instruction Queue (IQ) 235 is provided to subsequent stages in the pipeline as indicated at D bit 240 in
The Fetch IQ 235 and D bit 240 are coupled to translate stage 245 as shown in
From XIQ 260 the micro instructions are fed to register stage 265. Register stage 265 retrieves operands specified by micro instructions from a register file 270 for use by later stages in the pipeline. Register operands are retrieved from the register file 270 according to the state of the provided D bit. In a manner similar to translate stage 245, the D bit associated with each instruction is passed forward to the D bit output 275 of register stage 265.
Register stage 265 is coupled forward to address stage 280 as shown in
Address stage 280 is coupled forward to execute stage 291 which is also called the data/ALU stage 291. Execute stage 291 performs arithmetic logic unit (ALU) operations on data retrieved from the register file 270 or reads/writes data from/to memory using the memory address calculated in address stage 280. Execute stage 291 includes arithmetic logic unit (ALU) 292 which is coupled to segment descriptor table 293 as shown. The ALU 292 retrieves new segment descriptors from the segment descriptor table 293 when a far jump/call instruction is executed. The new data segment descriptor includes a D bit for the far jump/call instruction currently being executed, namely the actual D bit. Far jump resolution logic 294 compares the retrieved actual D bit of a far jump/call instruction currently being executed with the carried forward predicted D bit 295 from far jump target buffer 230 to determine if the default address/operand size prediction was correct. If the state of the retrieved actual D bit does not match the predicted D bit state 295, then the pipeline is flushed by appropriately asserting the FLUSH signal of far jump resolution logic 294. However, if the state of the retrieved actual D bit matches the predicted D bit state 295, then the pipeline is not flushed.
A write back stage 296 is coupled to execute stage 291 as shown. Write back stage 296 writes the result of a data read operation, or an ALU operation, to register file 270.
The above description with reference to
Moreover, although the present invention has been described with reference to particular apparatus and method, other alternative embodiments may used without departing from the scope of the invention.
Finally, those skilled in the art should appreciate that they can readily use the disclosed conception and specific embodiments as a basis for designing or modifying other structures for carrying out the same purposes of the present invention without departing from the spirit and scope of the invention as defined by the appended claims.
This application claims priority based on U.S. Provisional Application Ser. No. 60/345,453, filed Oct. 23, 2001, entitled BRANCH PREDICTION FOR FAR JUMPS THAT INCLUDES DEFAULT OPERATION SIZE. This application is related to U.S. patent application Ser. No. ______ (Docket CNTR.2019) entitled “PROCESSOR INCLUDING FALLBACK BRANCH PREDICTION MECHANISM FOR FAR JUMP AND FAR CALL INSTRUCTIONS,” by Gerard M. Col and Thomas C. McDonald, and filed on the same date as the present application, the disclosure thereof being incorporated herein by reference.
Number | Date | Country | |
---|---|---|---|
60345453 | Oct 2001 | US |