Skip to content

Casting via generic math doesn't always inline as expected #78648

@stephentoub

Description

@stephentoub

SharpLab

public class C
{
    public uint M0() => uint.CreateTruncating(42u);
    public uint M1() => uint.CreateTruncating((byte)42);
    public uint M2() => uint.CreateTruncating(42UL);
    public uint M3() => uint.CreateTruncating(42);
    public uint M4() => uint.CreateTruncating((sbyte)42);
    
    public int M5() => int.CreateTruncating(42);
    public int M6() => int.CreateTruncating((sbyte)42);
    public int M7() => int.CreateTruncating(42L);
    public int M8() => int.CreateTruncating(42u);
    public int M9() => int.CreateTruncating((byte)42);
}
; Core CLR 7.0.22.51805 on x64

C.M0()
    L0000: mov eax, 0x2a
    L0005: ret

C.M1()
    L0000: push rax
    L0001: mov dword ptr [rsp], 0x2a
    L0008: mov eax, [rsp]
    L000b: add rsp, 8
    L000f: ret

C.M2()
    L0000: push rax
    L0001: mov dword ptr [rsp], 0x2a
    L0008: mov eax, [rsp]
    L000b: add rsp, 8
    L000f: ret

C.M3()
    L0000: sub rsp, 0x28
    L0004: xor edx, edx
    L0006: mov [rsp+0x20], edx
    L000a: lea rdx, [rsp+0x20]
    L000f: mov ecx, 0x2a
    L0014: mov rax, 0x7ff8905995e8
    L001e: call qword ptr [rax]
    L0020: test eax, eax
    L0022: je short L002d
    L0024: mov eax, [rsp+0x20]
    L0028: add rsp, 0x28
    L002c: ret
    L002d: mov rax, 0x7ff8836b1870
    L0037: call qword ptr [rax]
    L0039: int3

C.M4()
    L0000: sub rsp, 0x28
    L0004: xor edx, edx
    L0006: mov [rsp+0x20], edx
    L000a: lea rdx, [rsp+0x20]
    L000f: mov ecx, 0x2a
    L0014: mov rax, 0x7ff89059bf60
    L001e: call qword ptr [rax]
    L0020: test eax, eax
    L0022: je short L002d
    L0024: mov eax, [rsp+0x20]
    L0028: add rsp, 0x28
    L002c: ret
    L002d: mov rax, 0x7ff8836b1870
    L0037: call qword ptr [rax]
    L0039: int3

C.M5()
    L0000: mov eax, 0x2a
    L0005: ret

C.M6()
    L0000: sub rsp, 0x28
    L0004: lea rdx, [rsp+0x20]
    L0009: mov ecx, 0x2a
    L000e: mov rax, 0x7ff89059dcf0
    L0018: call qword ptr [rax]
    L001a: test eax, eax
    L001c: jne short L0038
    L001e: lea rdx, [rsp+0x20]
    L0023: mov ecx, 0x2a
    L0028: mov rax, 0x7ff89059dd20
    L0032: call qword ptr [rax]
    L0034: test eax, eax
    L0036: je short L0041
    L0038: mov eax, [rsp+0x20]
    L003c: add rsp, 0x28
    L0040: ret
    L0041: mov rax, 0x7ff8836b1870
    L004b: call qword ptr [rax]
    L004d: int3

C.M7()
    L0000: sub rsp, 0x28
    L0004: lea rdx, [rsp+0x20]
    L0009: mov ecx, 0x2a
    L000e: mov rax, 0x7ff89059dd50
    L0018: call qword ptr [rax]
    L001a: test eax, eax
    L001c: jne short L0038
    L001e: lea rdx, [rsp+0x20]
    L0023: mov ecx, 0x2a
    L0028: mov rax, 0x7ff89059dd80
    L0032: call qword ptr [rax]
    L0034: test eax, eax
    L0036: je short L0041
    L0038: mov eax, [rsp+0x20]
    L003c: add rsp, 0x28
    L0040: ret
    L0041: mov rax, 0x7ff8836b1870
    L004b: call qword ptr [rax]
    L004d: int3

C.M8()
    L0000: sub rsp, 0x28
    L0004: lea rdx, [rsp+0x20]
    L0009: mov ecx, 0x2a
    L000e: mov rax, 0x7ff89059d5e8
    L0018: call qword ptr [rax]
    L001a: test eax, eax
    L001c: jne short L0038
    L001e: lea rdx, [rsp+0x20]
    L0023: mov ecx, 0x2a
    L0028: mov rax, 0x7ff89059d630
    L0032: call qword ptr [rax]
    L0034: test eax, eax
    L0036: je short L0041
    L0038: mov eax, [rsp+0x20]
    L003c: add rsp, 0x28
    L0040: ret
    L0041: mov rax, 0x7ff8836b1870
    L004b: call qword ptr [rax]
    L004d: int3

C.M9()
    L0000: sub rsp, 0x28
    L0004: lea rdx, [rsp+0x20]
    L0009: mov ecx, 0x2a
    L000e: mov rax, 0x7ff89059dc30
    L0018: call qword ptr [rax]
    L001a: test eax, eax
    L001c: jne short L0038
    L001e: lea rdx, [rsp+0x20]
    L0023: mov ecx, 0x2a
    L0028: mov rax, 0x7ff89059dc60
    L0032: call qword ptr [rax]
    L0034: test eax, eax
    L0036: je short L0041
    L0038: mov eax, [rsp+0x20]
    L003c: add rsp, 0x28
    L0040: ret
    L0041: mov rax, 0x7ff8836b1870
    L004b: call qword ptr [rax]
    L004d: int3

cc: @EgorBo, @tannergooding

Metadata

Metadata

Assignees

Labels

area-CodeGen-coreclrCLR JIT compiler in src/coreclr/src/jit and related components such as SuperPMItenet-performancePerformance related issue

Type

No type

Projects

No projects

Milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions