Search code examples
mipscpu-architecture

What is the purpose of using $0 as destination in MIPS


While I was studying about the conditions for detecting hazards while forwarding, I found this sentence very confusing.

'In the event that an instruction in the pipeline has $0 as its destination (for example, sll $0, $1, 2), we want to avoid forwarding its possibly nonzero result value.'

I thought register $0 cannot be written in a different value, so it's value is always 0. Then what is the use of sll $0, $1, 2, shifting left the value in $1 by 2 and storing it in $0, which cannot be changed? So how can $0 being as destination possibly be related to the need to forwarding?


Solution

  • "the need to forwarding" seems to indicate that you don't understand forwarding. It's an optimization. There is no "need" to do it.

    Optimizations should not affect semantics. Storing a value to $0 and subsequently reading the value of $0 should return 0 per the usual semantics, not the previously-stored value. Yet forwarding would forward the stored value.

    Now there's the question why $0 can be used as the store location. That's just the RISC logic of having orthogonal instruction sets. On non-MIPS architectures, this is somewhat more useful as these discarded-value operations can still set flags.