Pipelining | Set 2 (Dependencies and Data Hazard) (original) (raw)

Last Updated : 24 Apr, 2026

Please see **Set 1 for Execution, Stages and Performance (Throughput) and **Set 3 for Types of Pipeline and Stalling.

Dependencies in a pipelined processor

There are mainly three types of dependencies possible in a pipelined processor.

These dependencies may introduce stalls in the pipeline.

**Example

Instruction / Cycle 1 2 3 4 5
I1 IF(Mem) ID EX Mem
I2 IF(Mem) ID EX
I3 IF(Mem) ID EX
I4 IF(Mem) ID

In the above scenario, in Cycle 4, instructions I1 and I4 are trying to access same resource (Memory) which introduces a resource conflict. To avoid this problem, we have to keep the instruction on wait until the required resource (memory in our case) becomes available.

This wait will introduce stalls in the pipeline as shown below:

Cycle 1 2 3 4 5 6 7 8
I1 IF(Mem) ID EX Mem WB
I2 IF(Mem) ID EX Mem WB
I3 IF(Mem) ID EX Mem WB
I4 - - - IF(Mem)

**Solution for structural dependency: To minimize structural dependency stalls in the pipeline, we use separate instruction and data memory (Harvard architecture) or duplicate resources

**Renaming : Renaming: It is a technique used to eliminate false data dependencies (WAR and WAW) by assigning different physical registers to logical registers.

Instruction/ Cycle 1 2 3 4 5 6 7
I1 IF(CM) ID EX DM WB
I2 IF(CM) ID EX DM WB
I3 IF(CM) ID EX DM WB
I4 IF(CM) ID EX DM
I5 IF(CM) ID EX
I6 IF(CM) ID
I7 IF(CM)

**Control Dependency (Branch Hazards): This type of dependency occurs during the transfer of control instructions such as BRANCH, CALL, JMP, etc. On many instruction architectures, the processor will not know the target address of these instructions when it needs to insert the new instruction into the pipeline. Due to this, unwanted instructions are fed to the pipeline.

Consider the following sequence of instructions in the program:

Expected output: I1 -> I2 -> BI1

**NOTE: Generally, the target address of the JMP instruction is known after ID stage only.

Instruction/ Cycle 1 2 3 4 5 6
I1 IF ID EX MEM WB
I2 IF ID (PC:250) EX Mem WB
I3 IF ID EX Mem
BI1 IF ID EX

**Output Sequence: I1 -> I2 -> I3 -> BI1

Instruction/ Cycle 1 2 3 4 5 6
I1 IF ID EX MEM WB
I2 IF ID (PC:250) EX Mem WB
Delay - - - - - -
BI1 IF ID EX

**Output Sequence: I1 -> I2 -> Delay (Stall) -> BI1

**NOTE : As we see that the target address is available after the ID stage, so the number of stalls introduced in the pipeline is 1. Suppose, the branch target address would have been present after the ALU stage, there would have been 2 stalls. Generally, if the target address is present after the kth stage, then there will be (k – 1) stalls in the pipeline.

Total number of stalls introduced in the pipeline due to branch instructions:

Branch frequency x Branch Penalty     

**Data Dependency (Data Hazard)

Let us consider an ADD instruction S, such that

Now, we say that instruction S2 depends in instruction S1, when pipelining 7

This condition is called Bernstein condition. Three cases exist:

1. Flow (data) dependence / True Dependence:

O(S1) ∩ I(S2) ≠ ∅, S1 → S2 and S2 reads a value written by S1.

2. Anti-dependence:

I(S1) ∩ O(S2) ≠ ∅, S1 → S2 and S1 reads a value before S2 overwrites it.

3. Output dependence:

O(S1) ∩ O(S2) ≠ ∅, S1 → S2 and both write to the same memory location.

**Example: Let there be two instructions I1 and I2 such that:

When the above instructions are executed in a pipelined processor, then data dependency condition will occur, which means that I2 tries to read the data before I1 writes it, therefore, I2 incorrectly gets the old value from I1.

Instruction / Cycle 1 2 3 4
I1 IF ID EX DM
I2 IF ID(Old value) EX

To minimize data dependency stalls in the pipeline,operand forwardingis used.

**Operand Forwarding : In operand forwarding, we use the interface registers present between the stages to hold intermediate output so that dependent instruction can access new value from the interface register directly. Operand Forwarding can avoid stalls only if the dependent instructions are ALU type instructions.

Considering the same example:

Instruction / Cycle 1 2 3 4
I1 IF ID EX DM
I2 IF ID EX

Data Hazards

Data hazards occur when instructions that exhibit data dependence, modify data in different stages of a pipeline. Hazard cause delays in the pipeline.

There are mainly three types of data hazards:

Let there be two instructions I and J, such that J follow I. Then,

WAR and WAW hazards occur during the out-of-order execution of the instructions.