Control (Branch) Hazards
Control hazards occur when instructions after a branch are executed in the pipeline, even though the branch is taken. Control hazards can be addressed using:
- Flushing, where incorrect instructions are converted to nops by stalling.
- Branch prediction, where branch logic is computed before subsequent instructions enter the pipeline, reducing the number of stalls needed.
Examples
RISC-V
Suppose we currently have the following RISC-V instructions in our pipeline, where :
| Instruction | 1 | 2 | 3 | 4 | 5 | 6 | 7 |
| :---------------: | :-: | :-: | :-: | :-: | :-: | :-: | :-: |
|beq t0 t1 label| IF | ID | EX | M | WB | | |
|or t3 t4 t5| | IF | ID | EX | M | WB | |
|slt t6 t0 t3| | | IF | ID | EX | M | WB |
There is a clear control hazard, sinceorandsltare in our pipeline as our branch logic is calculated and taken. However, we can address this.Flushing
By flushing the pipeline, we convert the last two instructions to nops and take the branch.