3 ms·
"But it goes further than that: If you specify zero as the destination register for an instruction, the entire instruction may be optimized out by the processor
by Someone 9y ago
"But it goes further than that: If you specify zero as the destination register for an instruction, the entire instruction may be optimized out by the processor"
What's the logic for adding logic to the CPU for doing that? It may make such code run faster, but if programmers want faster code, they can just leave out such instructions.
- rbanffy 9y agoIIRC, the instruction has a bit field to specify a register as a destination. If you specify a register that is hardwired to have the value zero, the instruction is, effectively, a NOP.
- Someone 9y agoNot _is_, but. _may_be_. At least that's what I read. My question is why would they spend effort (and it is an effort; they would have to add logic to not set carry/overflow/zero flags when writing results to that register, for example) making that a no-op if no sane programmer would use them?
- rbanffy 9y agoSorry. I was thinking about stores. Other instructions may cause side effects that could be hard to avoid. It's fine to keep some behaviors of the ISA undefined. Different implementations may act differently. One generation may really avoid any side effects by retiring the instruction during the decoding stage while a simpler, say, low-power implementation may leave some side-effects (a carry that was set during an addition while the result is ignored). Stating that it may be ignored keeps the implementations free to do whatever is more convenient.
- tlb 9y agoYou need a NOP instruction, for many reasons (padding loops to cache line boundaries, jump tables, ...). This gets you free NOPs without allocating an instruction encoding for it. The processor needs to optimize it, because otherwise the NOP-equivalents ADD zero, zero, zero ADD zero, zero, zero would take 2-3 cycles to complete, because it looks like the first is writing to a register that the second is reading from, creating a scheduling hazard. The AXP should be able to issue those side-by-side, knowing that they don't actually do anything.