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

xarch: Use ZF and CF flags whenever possible to eliminate test instruction #53053

Closed
kunalspathak opened this issue May 20, 2021 · 8 comments
Closed
Assignees
Labels
area-CodeGen-coreclr CLR JIT compiler in src/coreclr/src/jit and related components such as SuperPMI
Milestone

Comments

@kunalspathak
Copy link
Member

From "Appendix F Instruction Effects on RFLAGS" of AMD manual and
"Appendix A EFLAGS CROSS-REFERENCE" and "Appendix B EFLAGS CONDITION CODES" of Intel manual, there are many ALU instructions that writes ZF and CF flags. If there is a test instruction that follows such ALU instruction and it operates on same register like ALU, then we could eliminate such test instruction.

I did quick analysis and found handful of places in .NET libraries where we have such pattern and could eliminate it.

For the analysis, I just looked for following pattern:

ALU regX, regY  ; store happens in regX
TEST regX, regX
JE/JNE/JZ/JNZ label

Related discussion : #52297 (comment)

Analysis result: je_jne.txt

@dotnet-issue-labeler dotnet-issue-labeler bot added area-CodeGen-coreclr CLR JIT compiler in src/coreclr/src/jit and related components such as SuperPMI untriaged New issue has not been triaged by the area owner labels May 20, 2021
@kunalspathak
Copy link
Member Author

@dotnet/jit-contrib

@AndyAyersMS
Copy link
Member

Found 903 methods containing 1220 groups

Those are the total number of methods/occurences?

@kunalspathak
Copy link
Member Author

Those are the total number of methods/occurences?

Yes.

@EgorBo
Copy link
Member

EgorBo commented May 21, 2021

Same as #6794 ?

@tannergooding
Copy link
Member

The first step here is likely updating https://github.com/dotnet/runtime/blob/main/src/coreclr/jit/instrsxarch.h to properly track which instructions modify/read which flags.

Today, we have a flags column and that "roughly" covers which general purpose instructions read or write flags as a whole.

We could likely update this to a few helper methods and then a set of flags that track which EFLAGS bits are read/modified. In practice, this would be OF, CF, ZF, SF, and PF as those are the condition codes supported by CMOVcc, Jcc, and SETcc and so we'd use 10 flag bits, which seems reasonable overall and like it wouldn't be overly complex to update/audit.

@tannergooding
Copy link
Member

tannergooding commented May 21, 2021

I prototyped correctly tracking the read/written flags: tannergooding@80214bd

Notably, the CMOVcc, SETcc, and Jcc should track just the flag(s) they read, since we have one entry per cc variant; but it should otherwise be correct for both Intel and AMD

@kunalspathak
Copy link
Member Author

I prototyped correctly tracking the read/written flags: tannergooding@80214bd

Notably, the CMOVcc, SETcc, and Jcc should track just the flag(s) they read, since we have one entry per cc variant; but it should otherwise be correct for both Intel and AMD

Thanks @tannergooding. I will try to prototype on top of your changes.

@kunalspathak kunalspathak self-assigned this May 21, 2021
@JulieLeeMSFT JulieLeeMSFT removed the untriaged New issue has not been triaged by the area owner label Jun 3, 2021
@JulieLeeMSFT JulieLeeMSFT added this to the 6.0.0 milestone Jun 3, 2021
@kunalspathak
Copy link
Member Author

Done by #53214

@ghost ghost locked as resolved and limited conversation to collaborators Jul 7, 2021
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

No branches or pull requests

5 participants