Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Use a shorter insruction on x86 for loading unsigned 32-bit immediates into 64-bit registers. #487

Merged
merged 1 commit into from
Apr 5, 2023

Conversation

valoran-M
Copy link
Contributor

Writing into a 32-bit register erases the upper 32 bits of a 64-bit register. This is the shortest instruction for loading unsigned 32-bit immediates. The length of those instructions is:

  • movl: 5 bytes
  • movq: 7 bytes
  • movabsq: 10 bytes

Note: Most assemblers will choose the proper instruction by themselves; this commit just makes this explicit.

…s into 64-bit registers.

Writing into a 32-bit register erases the upper 32 bits of a 64-bit
register. This is the shortest instruction for loading unsigned
32-bit immediates. The length of those instructions is:
- movl: 5 bytes
- movq: 7 bytes
- movabsq: 10 bytes

Note: Most assemblers will choose the proper instruction by themselves;
this commit just makes this explicit.
@silene
Copy link
Contributor

silene commented Apr 5, 2023

For the record, this issue showed up when improving the code generation for division by a constant. The algorithm loads a 64-bit immediate between 2^31 and 2^32-1, which was thus emitted as movabsq by CompCert. But GCC/GAS honors this choice (to account for self-modifying code?), which means that the machine code would use the 10-byte instruction where the 5-byte one would have been sufficient. This can have adverse effect on instruction decoding and caching.

@xavierleroy
Copy link
Contributor

Well spotted! I was not aware that movl produces shorter code than movq even in 64 bit mode. And there's the additional benefit for constants in the 2^31.. 2^32-1 range. Nice!

@xavierleroy xavierleroy merged commit 358230d into AbsInt:master Apr 5, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants