Skip to content

JIT: Shorten lea encoding. Prefer 'lea [reg + reg]' over 'lea [reg * 2]' - #138448

Open
BoyBaykiller wants to merge 1 commit into
dotnet:mainfrom
BoyBaykiller:shorter-lea-encoding-add-over-mul
Open

BoyBaykiller wants to merge 1 commit into
dotnet:mainfrom
BoyBaykiller:shorter-lea-encoding-add-over-mul

Conversation

@BoyBaykiller

Copy link
Copy Markdown
Contributor
static int Repro(int foo)
{
    return (foo * 2) + 4;
}
-       8D044D04000000       lea      eax, [2*rcx+0x04]
-						;; size=7 bbWeight=1 PerfScore 0.50
+       8D440904             lea      eax, [rcx+rcx+0x04]
+						;; size=4 bbWeight=1 PerfScore 1.00

I did it in codegen, alternatively it can be done in lower like this:

if ((base == nullptr) && (index != nullptr) && (scale == 2) && index->OperIs(GT_LCL_VAR))
{
    // Create copy for single-use
    GenTree* indexCopy = m_compiler->gtCloneExpr(index);
    BlockRange().InsertBefore(addr, indexCopy);
    addrMode->SetBase(index);
    addrMode->SetIndex(indexCopy);
    addrMode->SetScale(1);
    addrMode->SetOffset(static_cast<int>(offset));
}
else
{
 // everything else
}

But that has regressions in MinOpt, probably because of the gtCloneExpr. Also this would be in a common-path even though it only benefits x64 ISA (as far as I know).

@github-actions github-actions Bot added the area-CodeGen-coreclr CLR JIT compiler in src/coreclr/src/jit and related components such as SuperPMI label Nov 28, 2026
@azure-pipelines

Copy link
Copy Markdown
Azure Pipelines:
Successfully started running 5 pipeline(s).
10 pipeline(s) were filtered out due to trigger conditions.
There may be pipelines that require an authorized user to comment /azp run to run.

@dotnet-policy-service dotnet-policy-service Bot added the community-contribution Indicates that the PR has been added by a community member label Nov 28, 2026
@dotnet-policy-service

Copy link
Copy Markdown
Contributor

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

@EgorBo

EgorBo commented Nov 28, 2026

Copy link
Copy Markdown
Member
{14F13C21-A47A-4411-8A19-8F1C44E4B81F}

And it's probably correct? every case turns a two-part address (index + offset) into a three-part one (base + index + offset). On Skylake, lea with all three parts takes 3 cycles instead of 1

This branch has not been deployed

No deployments
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.

2 participants