3 ms·
Why does mov'ing a constant end up having overhead?
by oconnor663 6y ago
Why does mov'ing a constant end up having overhead?
- eMSF 6y agoIIRC immediate operands have to be the same size as the destination, so to zero a 32-bit register you need a 32-bit constant, and it simply makes the whole instruction larger than a simple xor.
- deleted 6y ago[deleted]
- mytailorisrich 6y agoA comprehensive answer: https://stackoverflow.com/questions/33666617/what-is-the-best-way-to-set-a-register-to-zero-in-x86-assembly-xor-mov-or-and https://stackoverflow.com/questions/33666617/what-is-the-bes... Basically, xor leads to smaller code and more efficient use of resources.
- detaro 6y agoIt's larger, because it needs to fit a 32bit value of 0 in the instruction, and thus e.g. on x86 needs 5 bytes, whereas xor reg,reg needs 2. As such it was a common code size optimization, which in turn has lead to CPU manufacturers optimizing their CPUs to recognize it and treat it even more efficiently.
- nickelpro 6y agoIt's not overhead, it's about dependency breaking. 32-bit xors on a single register are universally recognized as a zeroing idiom, which means the CPU doesn't have to wait for the results of previous operations in order to set the value of the applicable register to zero. In modern CPUs zero'ing idioms aren't even executed, they only get as far as the register allocater. The register allocater will allocate a zero'd physical register for the architectural register that had the idiom applied to it and the job is done.
- shiftingleft 6y agoCould you share a source about register allocation describing optimizations such as the one you described?