Simplify Expression
- You have a mathematical expression with unnecessary complexities.
Simplify that expression.
eax = eax ^ eax;
⇓
eax = 0;
Motivation
When generating machine code to evaluate mathematical expression the compiler frequently resorts to equivalent expression that are more complex (from a human point of view) yet evaluated faster by the targeted hardware. For example, if zeroing a register processor is pretended, it is often faster to perform a XOR of the register against itself than to move the immediate zero operand from the machine code into the register. These more complex expressions are rarely the ones idealized by the programmer, and they should be simplified in order to improve their understanding.
Mechanics
- Apply the appropriate mathematical simplification rules to the expression in order to make it more simple.
Example: equality comparisons
Equality comparisons are normally translated into comparisons against zero, to make efficient usage of the zero flag register, which exists in most processors.
if(eax - 2 == 0) ...
⇓
if(eax == 2) ...
Example: small (bitwise) integer constant multiplication
Integer multiplications of constants with a small number of non-zero bits are normally compiled as a sum of bitwise shifts, which take fewer processor cycles than a regular multiplication instruction.
eax = (ecx << 3) + (ecx << 1);
⇓
eax = ecx * 10;