MODULE 5 �Pipelining
Overview
Basic Concepts
Making the Execution of Programs Faster
Traditional Pipeline Concept
A
B
C
D
Traditional Pipeline Concept
A
B
C
D
30
40
20
30
40
20
30
40
20
30
40
20
6 PM
7
8
9
10
11
Midnight
Time
Traditional Pipeline Concept
A
B
C
D
6 PM
7
8
9
10
11
Midnight
T
a
s
k
O
r
d
e
r
Time
30
40
40
40
40
20
Traditional Pipeline Concept
A
B
C
D
6 PM
7
8
9
T
a
s
k
O
r
d
e
r
Time
30
40
40
40
40
20
Use the Idea of Pipelining in a Computer
F
1
E
1
F
2
E
2
F
3
E
3
I
1
I
2
I
3
(a) Sequential execution
Instruction
fetch
unit
Ex
ecution
unit
Interstage buffer
B1
(b) Hardware organization
T
ime
F
1
E
1
F
2
E
2
F
3
E
3
I
1
I
2
I
3
Instruction
(c) Pipelined execution
Figure 8.1. Basic idea of instruction pipelining.
Clock cycle
1
2
3
4
T
ime
Fetch + Execution
Use the Idea of Pipelining in a Computer
Fetch + Decode
+ Execution + Write
Textbook page: 457
Role of Cache Memory
Pipeline Performance
Data Hazards
Data Hazards
A ← 3 + A
B ← 4 × A
A ← 5 × C
B ← 20 + C
Mul R2, R3, R4
Add R5, R4, R6
Data Hazards
Figure 8.6. Pipeline stalled by data dependency between D2 and W1.
Operand Forwarding,� Instruction Hazards
Operand Forwarding
Handling Data Hazards in Software
I1: Mul R2, R3, R4
NOP
NOP
I2: Add R5, R4, R6
Side Effects
Add R1, R3
AddWithCarry R2, R4
Instruction Hazards
Overview
Unconditional Branches
Branch Timing
- Branch penalty
- Reducing the penalty
Instruction Queue and Prefetching
F : Fetch
instruction
E : Ex
ecute
instruction
W : Write
results
D : Dispatch/
Decode
Instruction queue
Instruction fetch unit
Figure 8.10. Use of an instruction queue in the hardware organization of Figure 8.2b.
unit
Conditional Braches
Delayed Branch
Delayed Branch
Add
LOOP
Shift_left
R1
Decrement
Branch=0
R2
LOOP
NEXT
(a) Original program loop
LOOP
Decrement
R2
Branch=0
Shift_left
LOOP
R1
NEXT
(b) Reordered instructions
Figure 8.12. Reordering of instructions for a delayed branch.
Add
R1,R3
R1,R3
Delayed Branch
F
E
F
E
F
E
F
E
F
E
F
E
F
E
Instruction
Decrement
Branch
Shift (delay slot)
Figure 8.13. Execution timing showing the delay slot being filled
during the last two passes through the loop in Figure 8.12.
Decrement (Branch tak
en)
Branch
Shift (delay slot)
Add (Branch not tak
en)
1
2
3
4
5
6
7
8
Clock c
ycle
T
ime
Branch Prediction
Incorrectly Predicted Branch
F
1
F
2
I
1
(Compare)
I
2
(Branch>0)
I
3
D
1
E
1
W
1
F
3
F
4
F
k
D
k
D
3
X
X
I
4
I
k
Instruction
Figure 8.14. Timing when a branch decision has been incorrectly predicted
as not taken.
E
2
Clock cycle
1
2
3
4
5
6
D
2
/P
2
T
ime
Branch Prediction
Influence on Instruction Sets
Overview
Addressing Modes
Recall
Load X(R1), R2
Load (R1), R2
Complex Addressing Mode
F
F
D
D
E
X
+
[R1]
[X
+
[R1]]
[[X
+
[R1]]]
Load
Ne
xt instruction
(a) Complex addressing mode
W
1
2
3
4
5
6
7
Clock c
ycle
T
ime
W
F
orw
ard
Load (X(R1)), R2
Simple Addressing Mode
X
+
[R1]
F
D
F
F
F
D
D
D
E
[X
+
[R1]]
[[X
+
[R1]]]
Add
Load
Load
Ne
xt instruction
(b) Simple addressing mode
W
W
W
W
Add #X, R1, R2
Load (R2), R2
Load (R2), R2
Addressing Modes
Addressing Modes
Conditional Codes
Conditional Codes
Add
Compare
Branch=0
R1,R2
R3,R4
. . .
Compare
Add
Branch=0
R3,R4
R1,R2
. . .
(a) A program fragment
(b) Instructions reordered
Figure 8.17. Instruction reordering.
Conditional Codes
Datapath and Control Considerations
Original Design
Pipelined Design
- Separate instruction and data caches
- PC is connected to IMAR
- DMAR
- Separate MDR
- Buffers for ALU
- Instruction queue
- Instruction decoder output
- Reading an instruction from the instruction cache
- Incrementing the PC
- Decoding an instruction
- Reading from or writing into the data cache
- Reading the contents of up to two regs
- Writing into one register in the reg file
- Performing an ALU operation
Superscalar Operation
Overview
Superscalar
Timing
I
1
(F
add)
D
1
D
2
D
3
D
4
E
1A
E
1B
E
1C
E
2
E
3
E
3
E
3
E
4
W
1
W
2
W
3
W
4
I
2
(Add)
I
3
(Fsub)
I
4
(Sub)
Figure 8.20. An example of instruction execution flow in the processor of Figure 8.19,
assuming no hazards are encountered.
1
2
3
4
5
6
Clock c
ycle
T
ime
F
1
F
2
F
3
F
4
7
Out-of-Order Execution
I
1
(F
add)
D
1
D
2
D
3
D
4
E
1A
E
1B
E
1C
E
2
E
3A
E
3B
E
3C
E
4
W
1
W
2
W
3
W
4
I
2
(Add)
I
3
(Fsub)
I
4
(Sub)
1
2
3
4
5
6
Clock c
ycle
T
ime
(a) Delayed write
F
1
F
2
F
3
F
4
7
Execution Completion
I
1
(F
add)
D
1
D
2
D
3
D
4
E
1A
E
1B
E
1C
E
2
E
3A
E
3B
E
3C
E
4
W
1
W
2
W
3
W
4
I
2
(Add)
I
3
(Fsub)
I
4
(Sub)
1
2
3
4
5
6
Clock c
ycle
T
ime
(b) Using temporary registers
TW
2
TW
4
7
F
1
F
2
F
3
F
4
Performance Considerations
Overview
where S is the average number of clock cycles it takes to fetch and execute one instruction, and R is the clock rate.
Overview
Number of Pipeline Stages