Skip to content

[JIT] X64 - Centralize peephole optimization for removing redundant mov instructions - #85780

Merged
TIHan merged 10 commits into
dotnet:mainfrom
TIHan:movzx-movsx-elim
May 11, 2023
Merged

[JIT] X64 - Centralize peephole optimization for removing redundant mov instructions#85780
TIHan merged 10 commits into
dotnet:mainfrom
TIHan:movzx-movsx-elim

Conversation

@TIHan

@TIHanTIHan commented May 4, 2023

Copy link
Copy Markdown
Contributor

Description

This centralizes checking for redundant mov instructions.

Depending on what we do for #85734 , we need to counter some of the regressions that may occur as a result of not removing CAST nodes.

This also includes more peephole optimizations for movzx, movsx and movsxd.

@ghostghost added the area-CodeGen-coreclr CLR JIT compiler in src/coreclr/src/jit and related components such as SuperPMI label May 4, 2023
@ghostghost assigned TIHanMay 4, 2023
@ghost

ghost commented May 4, 2023

Copy link
Copy Markdown

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

Issue Details

Description

Depending on what we do for #85734 , we need to counter some of the regressions that may occur as a result of not removing CAST nodes. For x64, we can do this by looking for redundant movzx and movsx instructions.

Author:TIHan
Assignees:TIHan
Labels:

area-CodeGen-coreclr

Milestone:-

@TIHanTIHan changed the title Added AreUpperBitsZero and AreUpperBitsSignExtended[JIT] X64 - Peephole optimization for removing redundant movzx and movsx instructionsMay 4, 2023
@TIHanTIHan changed the title [JIT] X64 - Peephole optimization for removing redundant movzx and movsx instructions[JIT] X64 - Centralization peephole optimization for removing redundant mov instructionsMay 4, 2023
@TIHanTIHan changed the title [JIT] X64 - Centralization peephole optimization for removing redundant mov instructions[JIT] X64 - Centralize peephole optimization for removing redundant mov instructionsMay 4, 2023
@TIHan

TIHan commented May 5, 2023

Copy link
Copy Markdown
ContributorAuthor

/azp run runtime-coreclr gcstress0x3-gcstress0xc

@TIHan

TIHan commented May 5, 2023

Copy link
Copy Markdown
ContributorAuthor

/azp run Fuzzlyn

@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 marked this pull request as ready for review May 5, 2023 01:12
@TIHan

TIHan commented May 5, 2023

Copy link
Copy Markdown
ContributorAuthor

/azp run Fuzzlyn

@azure-pipelines

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

@TIHan

TIHan commented May 5, 2023

Copy link
Copy Markdown
ContributorAuthor

@dotnet/jit-contrib this is ready. PTAL @BruceForstall - Diffs

All diff improvements, and we get a TP win for x64.
There is a significant TP regression for x86 which is likely because we never actually did any of these optimizations for x86 - we are now eliminating redundant movsx and movzx instructions.

@TIHan

TIHan commented May 5, 2023

Copy link
Copy Markdown
ContributorAuthor

Thinking about it, I'm not sure why there are TP regressions for x86 for MinOpts. These optimizations only kick in when optimizations are on.

Comment threadsrc/coreclr/jit/emitxarch.h
Comment threadsrc/coreclr/jit/emitxarch.cpp
Comment threadsrc/coreclr/jit/emitxarch.cpp
Comment threadsrc/coreclr/jit/emitxarch.cpp Outdated
{
result = (id->idOpSize() <= size);
}
#ifdef TARGET_64BIT

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

On 32-bit will EA_4BYTE ever be passed? If not, we don't technically don't need the ifdef?

Copy link
Copy Markdown
ContributorAuthor

Choose a reason for hiding this comment

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

size can be EA_4BYTE on 32-bit - ifdef'ing this only for 64-bit just does less work on 32-bit.

result = (id->idOpSize() <= size);
}
#ifdef TARGET_64BIT
// movsx/movsxd always sign extends to 8 bytes. W-bit is set.

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Why does the W-bit matter here?

@TIHanTIHanMay 6, 2023

Copy link
Copy Markdown
ContributorAuthor

Choose a reason for hiding this comment

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

W-bit on the encoding is what makes movsx and movsxd sign-extend to 8 bytes. We always default to using the W-bit. Technically, we could have a movsx or movsxd instruction not set the W-bit which would not sign-extend to 8 bytes (that would be a problem and make this optimization not safe), but we never actually emit those instructions that way.

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

This function cares about instructions and sizes, but not encoding, though.

Copy link
Copy Markdown
ContributorAuthor

Choose a reason for hiding this comment

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

The comment W-bit is set itself is just meant to inform the reason why this optimization is safe to do. It was already there before.

@ghostghost added needs-author-action An issue or pull request that requires more info or actions from the author. and removed needs-author-action An issue or pull request that requires more info or actions from the author. labels May 6, 2023
@TIHan

TIHan commented May 8, 2023

Copy link
Copy Markdown
ContributorAuthor

@BruceForstall This is ready again.

@TIHan
TIHanforce-pushed the movzx-movsx-elim branch from 9f90e74 to 708ea25CompareMay 10, 2023 03:57
@runfoapprunfoappBot mentioned this pull request May 10, 2023
@TIHan

Copy link
Copy Markdown
ContributorAuthor

@BruceForstall This is ready again. The optimizations only happen for X64, which was the case before, so no TP regressions for X86.

@BruceForstall

Copy link
Copy Markdown
Contributor

Diffs

@TIHan
TIHan merged commit e87dafa into dotnet:mainMay 11, 2023
@ghostghost locked as resolved and limited conversation to collaborators Jun 10, 2023
Sign up for freeto subscribe to this conversation on GitHub. Already have an account? Sign in.

Labels

area-CodeGen-coreclrCLR 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.

3 participants

@TIHan@BruceForstall@kunalspathak