runtime icon indicating copy to clipboard operation
runtime copied to clipboard

RyuJIT stores a value in a register only to move it to another register in the next instruction

Open mikedn opened this issue 11 years ago • 3 comments

See the Acquire method below and the generated code. The compiler decides to store the result of p + count in eax even though the result is supposed to be stored in ebx (which appears to have been allocated to the local variable n). An additional mov ebx, eax is required. Minor issue (as register to register moves are supposed to be fast) but this looks kind of odd.

using System;
using System.Runtime.CompilerServices;

unsafe struct ByteStream {
    byte* basePtr;
    int rva, rvaStart, rvaEnd;

    public ByteStream(byte* basePtr, int rvaStart, int rvaEnd) {
        this.basePtr = basePtr;
        this.rvaStart = rvaStart;
        this.rva = rvaStart;
        this.rvaEnd = rvaEnd;
    }

    [MethodImpl(MethodImplOptions.NoInlining)]
    public byte* Acquire(int count) {
        int p = rva;
        int n = p + count;
        if (n > rvaEnd)
            ThrowInvalidOperationException();
        rva = n;
        return basePtr + p;
    }

    [MethodImpl(MethodImplOptions.NoInlining)]
    private static void ThrowInvalidOperationException() {
        throw new InvalidOperationException();
    }
}

unsafe class Program {
    static int Main() {
        return (int)new ByteStream(null, 0, 42).Acquire(10);
    }
}

Code generated for Acquire:

;  V00 this         [V00,T00] (  6,   6  )   byref  ->  rsi         this
;  V01 arg1         [V01,T01] (  3,   3  )     int  ->  rdx
;  V02 loc0         [V02,T02] (  3,   3  )     int  ->  rdi
;  V03 loc1         [V03,T03] (  3,   3  )     int  ->  rbx
;  V04 OutArgs      [V04    ] (  1,   1  )  lclBlk (32) [rsp+0x00]
;
; Lcl frame size = 32

G_M17269_IG01:
       57                   push     rdi
       56                   push     rsi
       53                   push     rbx
       4883EC20             sub      rsp, 32
       488BF1               mov      rsi, rcx

G_M17269_IG02:
       8B7E08               mov      edi, dword ptr [rsi+8]
       8D0417               lea      eax, [rdi+rdx]           ; could have been stored directly in ebx
       8BD8                 mov      ebx, eax                 ; this is the only use of eax
       3B5E10               cmp      ebx, dword ptr [rsi+16]
       7E05                 jle      SHORT G_M17269_IG03
       E8B4FBFFFF           call     ByteStream:ThrowInvalidOperationException()

G_M17269_IG03:
       895E08               mov      dword ptr [rsi+8], ebx
       488B06               mov      rax, qword ptr [rsi]
       4863D7               movsxd   edx, edi
       4803C2               add      rax, rdx

G_M17269_IG04:
       4883C420             add      rsp, 32
       5B                   pop      rbx
       5E                   pop      rsi
       5F                   pop      rdi
       C3                   ret

mikedn avatar Jun 20 '15 19:06 mikedn

This is a register preferencing issue, which was recently fixed in our internal branch and yet to make it to Git. Post the fix, generated code is as follows:

IN000a: 000000 push rdi IN000b: 000001 push rsi IN000c: 000002 push rbx IN000d: 000003 sub rsp, 32 IN000e: 000007 mov rsi, rcx

G_M58229_IG02: ; offs=00000AH, size=0010H, gcrefRegs=00000000 {}, byrefRegs=00000040 {rsi}, byref, isz

IN0001: 00000A mov edi, dword ptr [rsi+8] IN0002: 00000D lea ebx, [rdi+rdx] IN0003: 000010 cmp ebx, dword ptr [rsi+16] IN0004: 000013 jle SHORT G_M58229_IG03 IN0005: 000015 call ByteStream:ThrowInvalidOperationException()

G_M58229_IG03: ; offs=00001AH, size=000CH, gcrefRegs=00000000 {}, byrefRegs=00000040 {rsi}, byref

IN0006: 00001A mov dword ptr [rsi+8], ebx IN0007: 00001D mov rax, qword ptr [rsi] IN0008: 000020 movsxd edx, edi IN0009: 000023 add rax, rdx

G_M58229_IG04: ; offs=000026H, size=0008H, epilog, nogc, emitadd

IN000f: 000026 add rsp, 32 IN0010: 00002A pop rbx IN0011: 00002B pop rsi IN0012: 00002C pop rdi IN0013: 00002D ret

sivarv avatar Jun 26 '15 17:06 sivarv

Nerd porn :)

leppie avatar Jun 26 '15 18:06 leppie

@sivarv Thank you, :+1:

mikedn avatar Jun 27 '15 09:06 mikedn