In this lesson, you will learn how the Branch Target Buffer (BTB) acts as a high-speed "shortcut" to keep your CPU pipeline full even when it encounters unpredictable branching code.
The Waiting Game
As we discussed in our study of control hazards, a processor usually doesn't know where a branch will go until it reaches the execution stage. If the pipeline has to wait for the branch to be "resolved" (decided), the stages behind it stall, creating a bubble. We previously used static prediction to guess the path, but that doesn't tell us where to jump if the branch is taken. That is where the Branch Target Buffer (BTB) comes in.
What is a BTB?
Think of the BTB as a specialized cache. While a regular instruction cache stores the instructions themselves, the BTB stores the history of where branches have jumped in the past.
Each entry in the BTB typically contains:
- The Branch Address: The address of the branch instruction itself.
- The Target Address: The destination address (the address of the next instruction if the branch is taken).
Speculation in Action
Consider this simple loop:
LOOP:
ADD R1, R1, #1
BEQ R1, R10, EXIT ; Branch if equal
JMP LOOP
EXIT:
...
When the CPU fetches the BEQ instruction, it simultaneously checks the BTB using the address of that BEQ. If the BTB finds a match, it immediately provides the EXIT address. The CPU then speculates—it ignores the next sequential instruction and starts fetching from the EXIT address right away, without waiting for the BEQ to be evaluated in the execution stage.
If the speculation is correct, the pipeline experiences zero stalls. If it is wrong, the CPU must perform a pipeline flush, discarding the speculatively fetched instructions and restarting from the correct path. By caching these target addresses, the BTB allows the processor to maintain a high-speed stream of instructions, turning a potential multi-cycle stall into a seamless transition.
Key Takeaway: The Branch Target Buffer enables high-performance speculation by caching jump destinations, allowing the CPU to fetch and execute predicted code before a branch is officially resolved.