A Control Hazard is one of the three types of pipeline hazards which occurs when the control has to decide on the instruction is fetch next, while a branch instruction is executing.
A branch instruction is special type of instruction which needs to execute a conditional or unconditional statement or instruction. So, that the pipeline processor can begin fetching the next instruction according to the order of the given program.
When this next instruction is decided by the outcome of a branch instruction, the pipeline processor suffers from high clock cycles as it waits for the branch instruction to execute. This causes the Control Hazard in the pipeline processor.
There are three methods for dealing with control hazards. They are.
1. Stall pipeline
2. Branch prediction
3. Dynamic branch prediction.
Stall Pipeline In this method, the pipeline processor has to stop or stall the fetch next instructions until the branch instruction complete its execution. Eventhough this method eliminates the control Hazard, this is a very low process which increases the clock cycle.
Branch Prediction This method eliminates the control hazard by assuming that the conditional "branch instruction will not be accepted or taken into consideration of the control for its execution. So that the pipeline can start to fetch the next instruction in the execution order.
In some cases, if a branch instruction does get taken, it means that the prediction is wrong. Then the next instruction in the order will be executed only after the branch instructions are completely executed.
Dynamic Branch Prediction
Normal branch prediction technique is efficient for smaller pipelines, but with deeper pipelines, when a branch gets taken the clock cycle increases drastically. To overcome this issue, a check must be made before executing any branch instruction to see whether the branch was taken or not when the last time it was executed.
For this purpose, a special buffer called branch instruction buffer or branch history table is maintained by every branch instruction. This table helps the control to check for whether the branch was taken when the last time it was executed.
After the checking process one of the below two tasks will be performed by the control, If control finds that branch is taken, then next instruction from the same place of branch instruction will be fetched and executed.
If branch is not taken during the last time it was executed, then the control will predict that it will get taken at this time as well, and begins to fetch the next instruction in the program execution order.