In this lesson, you will learn how to boost pipeline performance by "bypassing" the register file to feed computed results directly into functional units.
The Problem: Waiting for the Write-Back
In a standard pipeline, an instruction typically calculates a result (e.g., ADD R1, R2, R3) in the Execute (EX) stage, but it doesn't write that result back to the Register File until the final Write-Back (WB) stage. If the very next instruction needs R1 as an input, it normally has to stall until the ADD completes its write. This creates "data hazards" that waste precious clock cycles.
The Solution: Data Forwarding
Data forwarding (or "bypassing") recognizes that the computed result is actually available at the end of the EX stage—long before it is officially stored in the Register File. Instead of waiting, we add extra hardware paths (wires) and multiplexers that "snatch" the result directly from the pipeline registers and route it back to the input of the ALU.
Imagine this sequence:
ADD R1, R2, R3 ; R1 is calculated in EX
SUB R4, R1, R5 ; R1 is needed immediately in the next EX stage
Without forwarding, SUB must wait. With forwarding, we add a multiplexer at the ALU input. The control unit detects that the source register of the SUB instruction matches the destination register of the ADD instruction still in the pipeline. It then toggles the multiplexer to select the data coming from the EX/MEM pipeline register rather than the stale value read from the register file.
How it Works
- Detection: Logic compares the destination register address of instructions currently in the EX/MEM or MEM/WB stages with the source register addresses of the instruction currently in the ID/EX stage.
- Selection: If a match is found, the forwarding unit signals a multiplexer located at the ALU input to switch its input source from the "Register File output" to the "Forwarded data path."
This eliminates the need to stall the pipeline, allowing the processor to maintain a throughput of one instruction per cycle even when dependencies exist.
Key Takeaway: Data forwarding hardware allows a processor to route a result directly from a functional unit's output to another unit's input, bypassing the register file and eliminating stalls caused by data dependencies.