Compiler Optimization: Why XOR Instructions Are Used to Zero Registers in x86 Assembly
By
hasheddan
A baker's-dozen of insight crammed into one ring.
Summary
The article explores why the XOR instruction appears so frequently in compiled x86 code, explaining that compilers use 'xor eax, eax' as an efficient way to zero registers. The author, Matt Godbolt, discusses how this optimization technique became common practice among compiler writers and how it's more efficient than using 'mov eax, 0' due to smaller instruction size and better performance characteristics. The article provides technical insights into compiler optimization strategies and assembly language patterns.
Key quotes
· 4 pulledIn one of my talks on assembly, I show a list of the 20 most executed instructions on an average x86 Linux desktop. All the usual culprits are there, mov, add, lea, sub, jmp, call and so on, but the surprise interloper is xor - 'eXclusive OR'.
In my 6502 hacking days, the presence of an exclusive OR was a sure-fire indicator you'd either found the encryption part of the code, or some kind of sprite routine. It's surprising then, that a Linux machine just minding its own business, would be executing so many.
That is, until you realize that compilers love to use xor to zero registers. 'xor eax, eax' is the canonical way to set eax to zero, and it's used all over the place in compiled code.
The reason is simple: it's smaller and faster than 'mov eax, 0'. The xor instruction is only two bytes, while the mov instruction is five. And on modern processors, xor has special handling that makes it even faster.
You might also wanna read
An analysis of C++ compiler devirtualization optimization capabilities and corner cases
This article explores C++ compiler devirtualization optimizations — when compilers can replace virtual function calls with direct calls. It
Go Language Performance Improvements: Shifting Memory Allocations from Heap to Stack
The article discusses recent efforts by the Go programming language team to improve performance by shifting more memory allocations from the
Understanding Function Call Overhead and Compiler Inlining Optimization
The article discusses the performance implications of function calls in programming, explaining how compilers optimize code through techniqu
lemire.me·3mo agoCompiler Optimization Surprises: Clever Loop Transformations That Impress Engineers
The article discusses compiler optimization surprises, specifically how compilers can perform clever optimizations that even experienced eng
Understanding Partial Inlining: How Compilers Optimize Functions with Fast and Slow Paths
This technical blog post explores partial inlining as a compiler optimization technique, explaining how compilers can selectively inline onl
How Compiler Optimizers Recognize Patterns and Simplify Obfuscated Code
The article discusses how modern compilers and optimizers can recognize patterns in code and simplify complex-looking operations into effici
