The execution of every instruction needs the same series of steps, so the processor can start fetching the next instruction while the present one is being decoded. This overlap of the steps of different instructions is an instruction pipeline.

Four-Segment Instruction Pipeline

A common instruction pipeline has four segments. FI fetches the instruction, DA decodes it and calculates the effective address, FO fetches the operand, and EX executes the instruction. As soon as an instruction leaves FI, the next one is fetched, so four instructions are in progress together.

Definition: Instruction pipeline. A pipeline in which the fetch, decode, operand fetch and execute steps of successive instructions overlap in time.

Pipeline Conflicts

Three kinds of difficulty, called hazards, reduce the speed of an instruction pipeline. A structural hazard occurs when two segments need the same hardware, such as one memory, at the same time. A data hazard occurs when an instruction needs a result that an earlier instruction has not yet produced. A control hazard is caused by a branch, because the instructions fetched after it may not be the ones that should execute.

Table 4.4: Pipeline hazards and their remedies

Hazard Cause Remedy
Structural Two segments need the same resource Separate instruction and data memory
Data Result needed before it is ready Forwarding, delay or reordering
Control A branch changes the PC Branch prediction, delayed branch

RISC Pipeline

The simple instructions of a RISC machine suit pipelining well. A typical RISC pipeline has three segments: I fetches the instruction, A performs the ALU operation, and E transfers the result to a register or to memory. Only load and store instructions use memory, so conflicts are few. The compiler can also help. In a delayed load the compiler places an unrelated instruction after a load, so the processor never waits for the data. In a delayed branch an instruction that is useful in any case is placed after the branch, to fill the delay.

Example 4.4: Finding the effect of a branch

Problem: In a four-segment pipeline, instruction 3 is a branch to a distant instruction. How many cycles does the pipeline need for 6 instructions without a branch, and what happens at the branch?

Solution:

  1. Without a branch, the time for 6 instructions is 4 + 6 - 1 = 9 cycles.
  2. When the branch is decoded, the instructions fetched after it are wrong and are discarded.
  3. The pipeline must be refilled from the target address, which costs additional cycles.

Result: Without the branch 9 cycles are needed, and with it a few more are lost in refilling.

k, n = 4, 6
print("cycles without branch:", k + n - 1)

Output

cycles without branch: 9

Common mistakes

  • Assuming the pipeline never stalls. Hazards cause delays.
  • Believing that a data hazard can only occur with memory data. It can occur with registers too.
  • Forgetting that the compiler can fill the slot after a delayed load or branch.

Key takeaways

  • The four segments are FI, DA, FO and EX.
  • The hazards are structural, data and control.
  • The RISC pipeline has three segments: I, A and E.
  • Delayed load and delayed branch are compiler techniques.

Practice questions

  1. What are the segments of a four-segment instruction pipeline?
  2. What causes a control hazard?
  3. What is a delayed branch?

Answers to the practice questions

  1. Instruction fetch, decode and address calculation, operand fetch, and execute.
  2. A branch instruction that changes the program counter.
  3. Placing a useful instruction after a branch so the delay is filled.

Sources

  • Official BCA syllabus Computer Architecture, course code 25BCA401DS02, Unit IV