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

JIT: Optimize bit-wise AND with a constant mask in combination with a left shift in a compare #111979

Conversation

varelen
Copy link
Contributor

@varelen varelen commented Jan 29, 2025

This resolves #111554.

C# code (example code from the issue):

[MethodImpl(MethodImplOptions.NoInlining)]
public static bool CheckLetterOrDigitConst(int uc)
    => (LetterOrDigitCategories & (1 << uc)) != 0;

x64 (before):

mov      eax, 1
shlx     eax, eax, ecx
test     eax, 287
setne    al
movzx    rax, al

x64 (after):

mov      eax, 287
bt       eax, edi
setb     al
movzx    rax, al

…a left shift in a compare

This optmizes the generated code when having a pattern  like '(SomeConstant & (1 << value)) != 0' which was previously only optimized for '(variable & (1 << value)) != 0'.

Fix dotnet#111554
@dotnet-issue-labeler dotnet-issue-labeler bot added the area-CodeGen-coreclr CLR JIT compiler in src/coreclr/src/jit and related components such as SuperPMI label Jan 29, 2025
@dotnet-policy-service dotnet-policy-service bot added the community-contribution Indicates that the PR has been added by a community member label Jan 29, 2025
@varelen
Copy link
Contributor Author

varelen commented Jan 29, 2025

@dotnet-policy-service agree

Copy link
Member

@jakobbotsch jakobbotsch left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM. Thanks!

@EgorBo
Copy link
Member

EgorBo commented Feb 4, 2025

@jakobbotsch should this be merged?

@jakobbotsch
Copy link
Member

Yes, I was waiting for CI and it fell off my radar.
@varelen For future PRs, just FYI, we do not require the PR branch to be up to date for merging, so no need to press that "Update branch" button. Things move too quickly in this repo to always have PRs up to date, and pressing it makes CI run again.

Thanks again!

@jakobbotsch jakobbotsch merged commit cf89ec4 into dotnet:main Feb 4, 2025
113 checks passed
grendello added a commit to grendello/runtime that referenced this pull request Feb 4, 2025
* main: (30 commits)
  JIT: Optimize bit-wise AND with a constant mask in combination with a left shift in a compare (dotnet#111979)
  Change how we build the cross-OS DAC to support building in the VMR (dotnet#111927)
  Add Windows Server 2025 to test configurations (dotnet#111938)
  [PERF] Move performance testing YAML from dotnet/runtime to dotnet/performance (dotnet#111454)
  arm64: Add support for Bitwise OR NOT & XOR NOT (dotnet#111893)
  JIT: Fix cross crossgen comparison failures (dotnet#112078)
  Bump `StyleCop.Analyzers` to `1.2.0-beta.556` (dotnet#111278)
  Remove `RequiresProcessIsolation` on InterfaceFolding tests (dotnet#112098)
  Use hardlinks in helixpublishwitharcade (dotnet#112091)
  Update breaking change rules regarding byref/objref fields. (dotnet#112087)
  [daccess] Do not use USE_DAC_TABLE_RVA on Apple platforms (dotnet#112076)
  use collection syntax in illink (dotnet#108458)
  Include PDB for all TfmRuntimeSpecificPackageFile (dotnet#111879)
  [main] Update dependencies from dotnet/emsdk (dotnet#111690)
  Enable Mono tests (dotnet#111981)
  Let the debugger knows DATAS is on (dotnet#107115)
  Tests ran counter (dotnet#111145)
  Some System.Decimal performance improvements (dotnet#99212)
  [mono][mini] Remove support for the Xamarin.iOS and Xamarin.Mac assemblies in the AOT compiler. (dotnet#108886)
  Remove one usage of `Unsafe.AsPointer`. (dotnet#112079)
  ...
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
area-CodeGen-coreclr CLR JIT compiler in src/coreclr/src/jit and related components such as SuperPMI community-contribution Indicates that the PR has been added by a community member
Projects
None yet
Development

Successfully merging this pull request may close these issues.

JIT does not use BT instruction for constant mask
3 participants