Skip to content

Improve JIT of static variables which value is already known #1432

@Symbai

Description

@Symbai

The JIT can be improved when a static variable is declared with a constant value, ideally by treating it like a constant.

Declared as constant

static class Bla
{
	public const double Value1 = 2d;
	public const double Value2 = 5d;

	[MethodImpl(MethodImplOptions.AggressiveInlining)]
	public static double BlaMethod(double val)
	{
		return val / Value1 / Value2;
	}
}
---------------------------------------------------------
vzeroupper
xchg      ax,ax
vdivsd    xmm0,xmm0,[rel 7FFA`42B2`F0A8h]
vdivsd    xmm0,xmm0,[rel 7FFA`42B2`F0B0h]
ret

Declared as static

static class Bla
{
	public static double Value1 = 2d;
	public static double Value2 = 5d;

	[MethodImpl(MethodImplOptions.AggressiveInlining)]
	public static double BlaMethod(double val)
	{
		return val / Value1 / Value2;
	}
}
---------------------------------------------------------
sub       rsp,28h
vzeroupper
vmovsd    [rsp+30h],xmm0
mov       rcx,7FFA`42B9`6A88h
mov       edx,2
call      0000`7FFA`A260`70B0h
vmovsd    xmm0,[rsp+30h]
vdivsd    xmm0,xmm0,[rel 7FFA`42B9`6AC0h]
vdivsd    xmm0,xmm0,[rel 7FFA`42B9`6AC8h]
add       rsp,28h
ret

Metadata

Metadata

Assignees

No one assigned

    Labels

    area-CodeGen-coreclrCLR JIT compiler in src/coreclr/src/jit and related components such as SuperPMIuntriagedNew issue has not been triaged by the area owner

    Type

    No type
    No fields configured for issues without a type.

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions