vnmakarov / mir

A lightweight JIT compiler based on MIR (Medium Internal Representation) and C11 JIT compiler and interpreter based on MIR
MIT License
2.24k stars 147 forks source link

Many missed optimizations. #309

Open remy-luisant opened 1 year ago

remy-luisant commented 1 year ago

Highly redundant MIR is optimized into this.

    7ff13cd670f0:   48 83 ec 18             sub    $0x18,%rsp
    7ff13cd670f4:   48 89 1c 24             mov    %rbx,(%rsp)
    7ff13cd670f8:   48 8b c7                mov    %rdi,%rax
    7ff13cd670fb:   33 c9                   xor    %ecx,%ecx
    7ff13cd670fd:   48 c7 00 1c 00 00 00    movq   $0x1c,(%rax)
    7ff13cd67104:   c7 40 08 ff ff 01 00    movl   $0x1ffff,0x8(%rax)
    7ff13cd6710b:   c7 40 44 00 00 04 00    movl   $0x40000,0x44(%rax)
    7ff13cd67112:   c6 40 51 00             movb   $0x0,0x51(%rax)
    7ff13cd67116:   40 88 48 52             rex mov %cl,0x52(%rax)
    7ff13cd6711a:   40 88 48 53             rex mov %cl,0x53(%rax)
    7ff13cd6711e:   c6 40 54 00             movb   $0x0,0x54(%rax)
    7ff13cd67122:   c6 40 55 00             movb   $0x0,0x55(%rax)
    7ff13cd67126:   48 8b 1c 24             mov    (%rsp),%rbx
    7ff13cd6712a:   48 83 c4 18             add    $0x18,%rsp
    7ff13cd6712e:   c3                      ret

Of note is that a stack frame is being made, without need for it. Also, a register is being spilled when it is not being used. This is due to the hard register information being computed before other passes remove the need to have the register be spilled.

Also of note is the use of RAX instead of RDI, for the first parameter.

Finally, the constant zero is written to RCX, when it could have been merged with the two moves at offsets 52 and 53.

Many of these strike me as low hanging fruit. I'm on the bbv branch. Input code available on request, by email.

vnmakarov commented 1 year ago

Sorry for delay with the answer and thank you for reporting this problem.

I am really interesting in the source code. Could you, please, post it here. I'll try to investigate it and write when and what could be fixed after that.

remy-luisant commented 1 year ago

Gladly. The code is very long, and I do not wish to share it here. Do you have a mail address that I could use?

vnmakarov commented 1 year ago

Gladly. The code is very long, and I do not wish to share it here. Do you have a mail address that I could use?

vmakarov@redhat.com