Skip to content

[JIT] Always transform AND(X, CNS(-1)) to X - #82276

Merged
TIHan merged 8 commits into
dotnet:mainfrom
TIHan:x86-skip-and-negative-one
Feb 23, 2023
Merged

[JIT] Always transform AND(X, CNS(-1)) to X#82276
TIHan merged 8 commits into
dotnet:mainfrom
TIHan:x86-skip-and-negative-one

Conversation

@TIHan

@TIHan TIHan commented Feb 17, 2023

Copy link
Copy Markdown
Contributor

Description

We should always try to lower AND(X, CNS(-1)) to X if possible. We do this transformation in lowering to pick up this pattern as a result of decomposing longs on 32bit archs.

@ghost ghost added the area-CodeGen-coreclr CLR JIT compiler in src/coreclr/src/jit and related components such as SuperPMI label Feb 17, 2023
@ghost ghost assigned TIHan Feb 17, 2023
@ghost

ghost commented Feb 17, 2023

Copy link
Copy Markdown

Tagging subscribers to this area: @JulieLeeMSFT, @jakobbotsch, @kunalspathak
See info in area-owners.md if you want to be subscribed.

Issue Details

We can always skip and reg0, -1, if the target type is TYP_INT, on X86 as the result will never be different.

Author: TIHan
Assignees: TIHan
Labels:

area-CodeGen-coreclr

Milestone: -

@jakobbotsch

Copy link
Copy Markdown
Member

Can this optimization be done in lowering instead so that all targets benefit equally? Are there any benefits to the existing x64 peephole for doing the optimization in codegen?

@tannergooding

Copy link
Copy Markdown
Member

What's leading to the x & ~0 in the first place? Is this something specific that's being introduced in lowering or is it a more general missing opt in morph/constant folding?

@TIHan

TIHan commented Feb 17, 2023

Copy link
Copy Markdown
Contributor Author

@tannergooding @jakobbotsch

Good points, since we don't have to worry about the upper 32 bits, we could do the transform earlier. I have an idea where.

@jakobbotsch

Copy link
Copy Markdown
Member

since we don't have to worry about the upper 32 bits, we could do the transform earlier

Even on 64-bit we can do the optimization for TYP_INT GT_AND on IR nodes and I'd be curious to see if the peephole isn't just a more conservative version of that.

@TIHan

TIHan commented Feb 17, 2023

Copy link
Copy Markdown
Contributor Author

Even on 64-bit we can do the optimization

I don't think we can - I remember trying to do (x & -1) optimization in an earlier phase before; ran into issues of the upper 32bits, which is why I did it as a peephole.

However, we could definitely do a TYP_LONG version of GT_AND and make sure the constant is a TYP_LONG of -1.

@jakobbotsch

Copy link
Copy Markdown
Member

I don't think we can - I remember trying to do (x & -1) optimization in an earlier phase before; ran into issues of the upper 32bits, which is why I did it as a peephole.

I'd be interested to see the example, that certainly sounds odd or like there is a bug somewhere else.

@TIHan

TIHan commented Feb 17, 2023

Copy link
Copy Markdown
Contributor Author

So, we definitely should do the transformation in lowering for x86 because we catch cases of (x & -1) after decomposing longs.

@TIHan

TIHan commented Feb 21, 2023

Copy link
Copy Markdown
Contributor Author

@dotnet/jit-contrib This is ready PTAL @BruceForstall

@BruceForstall

Copy link
Copy Markdown
Contributor

Diffs -- significant asm diffs on x86, a few on x64

Comment thread src/coreclr/jit/lowerxarch.cpp Outdated
Comment thread src/coreclr/jit/lowerxarch.cpp Outdated
@TIHan TIHan changed the title [JIT] X86 - Always skip emitting 'and reg0, -1' on x86 [JIT] Always transform AND(X, CNS(-1)) to X Feb 22, 2023
@TIHan

TIHan commented Feb 23, 2023

Copy link
Copy Markdown
Contributor Author

/azp run runtime-coreclr superpmi-diffs

@TIHan

TIHan commented Feb 23, 2023

Copy link
Copy Markdown
Contributor Author

/azp run runtime-coreclr superpmi-replay

@azure-pipelines

Copy link
Copy Markdown
Azure Pipelines successfully started running 1 pipeline(s).

1 similar comment
@azure-pipelines

Copy link
Copy Markdown
Azure Pipelines successfully started running 1 pipeline(s).

@TIHan

TIHan commented Feb 23, 2023

Copy link
Copy Markdown
Contributor Author

@dotnet/jit-contrib @jakobbotsch this is ready again. current superpmi replay failure is unrelated

@jakobbotsch jakobbotsch left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

LGTM!

@TIHan

TIHan commented Feb 23, 2023

Copy link
Copy Markdown
Contributor Author

Dope

@TIHan
TIHan merged commit a375009 into dotnet:main Feb 23, 2023
@TIHan
TIHan deleted the x86-skip-and-negative-one branch February 23, 2023 22:09
@ghost ghost locked as resolved and limited conversation to collaborators Mar 26, 2023
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.

Labels

area-CodeGen-coreclr CLR JIT compiler in src/coreclr/src/jit and related components such as SuperPMI

Projects

None yet

Development

Successfully merging this pull request may close these issues.

4 participants