Pipeline Hazards
What can go wrong in pipelining?
- Data Hazards: Wrong data read before data is written.
- Control Hazards: Wrong instruction fetched before a taken branch.
- Exceptions
Control Hazards
Detect and stall
Delay fetch until branch resolved.
- Detection:
- Must wait until decode (otherwise no way to tell whether it’s a branch/jump).
- Compare opcode to beq or jalr.
- Stall:
- Keep current instructions in fetch. (IF/ID reg & pc remain unchanged)
- Pass noop to decode stage, not execute! (This is because the detection happens at decode)
Problem: CPI increases every time a branch is detected!
Speculate and Squash-if-Wrong
- Speculate: Assume branch not taken
- Keep fetching from PC+1 until we know that the branch is really taken.
- Squash: flush bad instructions if branch taken
- Overwrite Decode, Execute, and Memory with a noop (these instructions are bad).
- Send target address of the taken beq to PC (the correct fetch address fo