The simplest solution for a RISC machine to take when it encounters a branch statement, is to simply fill the pipleine with stalls until the outcome of the branch statement becomes known. Once the outcome is known, the pipeline can be started up again, with the next instruction to execute at the head of the pipe.
The problem with this method is that it is too simple. It doesn't do anything to speed up the machines execution, it just stops the machine until the machine knows what it is supposed to do next. This means a lot of stalls, and a lot of wasted processor cycles. Just like stopping in front of an oncoming Mack truck generally isn't the best option for a deer, freezing is the least favorable option for a RISC machine. But, it is simple to implement, doesn't add anything to the cost of the machine, and doesn't require any finesse on the part of the compiler.
Example: |
LOOP: |
LW R1, 0(R2)
|
||||||||||||||||
|
||||||||||||||||||
1 |
2 |
3 |
4 |
5 |
6 |
7 |
8 |
9 |
10 |
11 |
12 |
13 |
14 |
15 |
16 |
17 |
||
---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
LW R1,0(R2) |
IF |
ID |
EX |
Mem |
WB |
|
|
|
|
|
|
|
|
|
|
|
|
|
SUB R3,R3,R1 |
|
IF |
ID |
EX |
Mem |
WB |
|
|
|
|
|
|
|
|
|
|
|
|
BNEZ R3,Loop |
|
|
IF |
ID |
EX |
Mem |
WB |
|
|
|
|
|
|
|
|
|
|
|
LW R1,0(R2) |
|
|
|
stall |
stall |
stall |
IF |
ID |
EX |
Mem |
WB |
|
|
|
|
|
|
|
SUB R3,R3,R1 |
|
|
|
|
|
|
|
IF |
ID |
EX |
Mem |
WB |
|
|
|
|
|
|
BNEZ R3,Loop |
|
|
|
|
|
|
|
|
IF |
ID |
EX |
Mem |
WB |
|
|
|
|
|
SW R4, 0(R3) |
|
|
|
|
|
|
|
|
|
stall |
stall |
stall |
IF |
ID |
EX |
Mem |
WB |
Even with just 2 iterations through the loop, we already have 6 stalls because
we must wait for the BNEZ instruction to complete before we can know what instruction
to execute next. Obviously with a lot of iterations through a loop, we will
not be taking full advantage of the pipeline.
freezing