Definition
A Branch Target Buffer (BTB) is a cache-like component in processors that is used for branch prediction.

Explanation
The main concept of the BTB is to store the program counter (PC) of a branch instruction, and also the PC of the target of the branch (currentPC => targetPC). This way, when a branch instruction is fetched out of memory, the processor can look in the BTB (based on the current PC) to see which direction the branch is going to go (the target PC). The processor needs some sort of predictor for the branch direction to determine whether or not to go to the target path (target PC) or the fall-through path (PC plus 1). Because most modern processors are pipelined, when the branch instruction is first fetched, it may not be until several cycles later that the true direction of the branch can be determined. This is why a BTB is implemented to try and predict the direction of the branch. How the BTB predicts the branch direction can vary.

Branch Prediction
A simple type of BTB consists of a 2-bit predictor, which is a saturated counter that counts from 0 to 3. When the counter is at '0' or '1', the BTB is predicting that the branch instruction is not taken, or fall-through. When the counter is '2' or '3', the BTB is predicting that the instruction is taken, and the next instruction should come from the taken path. If the BTB ends up predicting incorectly, then the incorrect instructions are squashed, the correct instructions are fetched, and the BTB counter is altered by 1, plus or minus depending on taken or not taken. That way the BTB will fairly accuratly predict branches in single loops and multi-nested loops. There are other more complicated types of branch prediction not covered here such as path-based prediction.

Log in or register to write something here or to contact authors.