Some of the MIPS instructions have immediate offsets.
For example, while moving lw command to fill the branch delay slot below the beq, its immediate offset changes from 100 to 96.
PC Loop: lw $2, 100($3)
PC+4 addi $3, $3, 4
PC+8 beq $3, $4, Loop
transformed into
PC Loop: addi $3, $3, 4
PC+4 beq $3, $4, Loop
PC+8 lw $2, 96($3) # branch delay slot
Is it because PC is always PC+4, so it goes as 100 – 8 + 4 = 96 ?
What happens if instruction is moving up?
For example,
PC Loop: xxxxxxxxxxxxx
PC+4 addi $5, 4($5)
Will this be correct?
PC Loop: addi $5, 4($5)
PC+4 xxxxxxxxxxxxx
The change in the immediate has nothing to do with the PC.
In the original you are loading from address ($3 + 100), then adding 4 to $3.
In the transformed one, you are adding 4 to $3, then loading from address ($3 + 96) = (original_$3 + 4 + 96) = (original_$3 + 100). It’s the reordering between
lwandaddiwhich affects the immediate (addicannot be moved to the delay slot because the branch depends on the new value of $3).