Repository: dotnet/runtime
File: src/coreclr/jit/regallocwasm.cpp (C++)

Code before:
// IdentifyCandidates: Identify locals eligible for register allocation.
//
// Also allocate them to virtual registers.
//
void WasmRegAlloc::IdentifyCandidates()
{
    m_spVarDsc = m_compiler->lvaGetDesc(m_compiler->lvaWasmSpArg);

    bool anyFrameLocals = false;
    for (unsigned lclNum = 0; lclNum < m_compiler->lvaCount; lclNum++)
    {
        LclVarDsc* varDsc = m_compiler->lvaGetDesc(lclNum);
        varDsc->SetRegNum(REG_STK);

        if (isRegCandidate(varDsc))
        {
            JITDUMP("RA candidate: V%02u\n", lclNum);
            InitializeCandidate(varDsc);
        }
        else if (varDsc->lvRefCnt() != 0)
        {
            anyFrameLocals = true;
        }
    }

    if (anyFrameLocals || m_codeGen->isFramePointerRequired())
    {
        AllocateFramePointer();
    }
}

//------------------------------------------------------------------------
// InitializeCandidate: Initialize the candidate local.
//
// Allocates a virtual register for this local.
//
// Arguments:
//    varDsc - The local's descriptor
//
void WasmRegAlloc::InitializeCandidate(LclVarDsc* varDsc)
{
    var_types type = genActualType(varDsc->GetRegisterType());
    regNumber reg  = AllocateVirtualRegister(type);
    varDsc->SetRegNum(reg);
    varDsc->lvLRACandidate = true;
}

//------------------------------------------------------------------------
// AllocateStackPointer: Allocate a virtual register for the SP.
//
regNumber WasmRegAlloc::AllocateStackPointer()

Review comment (suggestion):
I think it would be better to use the narrower `compLocallocUsed` here instead of `isFramePointerRequired`, with a comment to the effect of "LCLHEAP codegen uses FP for unwind info establishment..." (we wouldn't need the FP with a different scheme).

`isFramePointerRequired` is set with lots of conditions that are deeply irrelevant for WASM and probably should be ifdef-ed out. There are no FP chains on WASM.

Code after:
// IdentifyCandidates: Identify locals eligible for register allocation.
//
// Also allocate them to virtual registers.
//
void WasmRegAlloc::IdentifyCandidates()
{
    InitializeStackPointer();

    bool anyFrameLocals = false;
    for (unsigned lclNum = 0; lclNum < m_compiler->lvaCount; lclNum++)
    {
        LclVarDsc* varDsc = m_compiler->lvaGetDesc(lclNum);
        varDsc->SetRegNum(REG_STK);

        if (isRegCandidate(varDsc))
        {
            JITDUMP("RA candidate: V%02u\n", lclNum);
            InitializeCandidate(varDsc);
        }
        else if (varDsc->lvRefCnt() != 0)
        {
            anyFrameLocals = true;
        }
    }

    if (anyFrameLocals || m_compiler->compLocallocUsed)
    {
        AllocateFramePointer();
    }
}

//------------------------------------------------------------------------
// InitializeCandidate: Initialize the candidate local.
//
// Allocates a virtual register for this local.
//
// Arguments:
//    varDsc - The local's descriptor
//
void WasmRegAlloc::InitializeCandidate(LclVarDsc* varDsc)
{
    var_types type = genActualType(varDsc->GetRegisterType());
    regNumber reg  = AllocateVirtualRegister(type);
    varDsc->SetRegNum(reg);
    varDsc->lvLRACandidate = true;
}

//------------------------------------------------------------------------
// InitializeStackPointer: Initialize the stack pointer local.
//
// The stack pointer (as referenced in IR) presents a bit of a problem for