Skip to content

Instantly share code, notes, and snippets.

@tsafin
Last active August 6, 2021 22:21
Show Gist options
  • Save tsafin/024d6a47a3693d40078514fa6577c1c3 to your computer and use it in GitHub Desktop.
Save tsafin/024d6a47a3693d40078514fa6577c1c3 to your computer and use it in GitHub Desktop.
There is no even simple peephole optimizations in LuaJIT
01:20 $ luajit -jdump -O3 -Ohotloop=9 -Ocse -Ofold ../days_ffi.lua
---- TRACE 1 start days_ffi.lua:14
0015 MOV 9 3
0016 MOV 10 2
0017 CALL 9 2 2
0000 . FUNCF 3 ; days_ffi.lua:9
0001 . GGET 1 0 ; "tonumber"
0002 . TGETS 2 0 1 ; "secs"
0003 . CALL 1 2 2
0000 . . FUNCC ; tonumber
0004 . TGETS 2 0 2 ; "nsec"
0005 . DIVVN 2 2 0 ; 1000000000
0006 . ADDVV 1 1 2
0007 . DIVVN 1 1 1 ; 3600
0008 . DIVVN 1 1 2 ; 24
0009 . RET1 1 2
0018 MOV 4 9
0019 FORL 5 => 0015
---- TRACE 1 IR
0001 int SLOAD #6 CI
0002 > fun SLOAD #4 T
0003 > tab SLOAD #3 T
0004 > fun EQ 0002 days_ffi.lua:9
0005 tab FLOAD days_ffi.lua:9 func.env
0006 int FLOAD 0005 tab.hmask
0007 > int EQ 0006 +63
0008 p32 FLOAD 0005 tab.node
0009 > p32 HREFK 0008 "tonumber" @8
0010 > fun HLOAD 0009
0011 int FLOAD 0003 tab.hmask
0012 > int EQ 0011 +1
0013 p32 FLOAD 0003 tab.node
0014 > p32 HREFK 0013 "secs" @0
0015 > num HLOAD 0014
0016 > fun EQ 0010 tonumber
0017 > p32 HREFK 0013 "nsec" @1
0018 > num HLOAD 0017
0019 num DIV 0018 +1000000000
0020 num ADD 0019 0015
0021 num DIV 0020 +3600
0022 num DIV 0021 +24
0023 + int ADD 0001 +1
0024 > int LE 0023 +60
0025 ------ LOOP ------------
0026 + int ADD 0023 +1
0027 > int LE 0026 +60
0028 int PHI 0023 0026
---- TRACE 1 mcode 278
5560db79fee7 mov dword [0x41453410], 0x1
5560db79fef2 mov esi, edx
5560db79fef4 movsd xmm3, [0x41464718]
5560db79fefd movsd xmm2, [0x41464700]
5560db79ff06 movsd xmm1, [0x414646e8]
5560db79ff0f cvttsd2si ebp, [rsi+0x28]
5560db79ff14 cmp dword [rsi+0x1c], -0x09
5560db79ff18 jnz 0x5560db790010 ->0
5560db79ff1e cmp dword [rsi+0x14], -0x0c
5560db79ff22 jnz 0x5560db790010 ->0
5560db79ff28 mov edx, [rsi+0x10]
5560db79ff2b cmp dword [rsi+0x18], 0x4145df58
5560db79ff32 jnz 0x5560db790010 ->0
5560db79ff38 mov ebx, [0x4145df60]
5560db79ff3f cmp dword [rbx+0x1c], +0x3f
5560db79ff43 jnz 0x5560db790010 ->0
5560db79ff49 mov ecx, [rbx+0x14]
5560db79ff4c mov rdi, 0xfffffffb41456380
5560db79ff56 cmp rdi, [rcx+0xc8]
5560db79ff5d jnz 0x5560db790010 ->0
5560db79ff63 cmp dword [rcx+0xc4], -0x09
5560db79ff6a jnz 0x5560db790010 ->0
5560db79ff70 cmp dword [rdx+0x1c], +0x01
5560db79ff74 jnz 0x5560db790010 ->0
5560db79ff7a mov eax, [rdx+0x14]
5560db79ff7d mov rdi, 0xfffffffb4145d8e8
5560db79ff87 cmp rdi, [rax+0x8]
5560db79ff8b jnz 0x5560db790010 ->0
5560db79ff91 cmp dword [rax+0x4], 0xfffeffff
5560db79ff98 jnb 0x5560db790010 ->0
5560db79ff9e cmp dword [rcx+0xc0], 0x41456358
5560db79ffa8 jnz 0x5560db790010 ->0
5560db79ffae mov rdi, 0xfffffffb41456df8
5560db79ffb8 cmp rdi, [rax+0x20]
5560db79ffbc jnz 0x5560db790010 ->0
5560db79ffc2 cmp dword [rax+0x1c], 0xfffeffff
5560db79ffc9 jnb 0x5560db790010 ->0
5560db79ffcf movsd xmm0, [rax+0x18]
5560db79ffd4 divsd xmm0, xmm3
5560db79ffd8 addsd xmm0, [rax]
5560db79ffdc divsd xmm0, xmm2
5560db79ffe0 divsd xmm0, xmm1
5560db79ffe4 add ebp, +0x01
5560db79ffe7 cmp ebp, +0x3c
5560db79ffea jg 0x5560db790014 ->1
->LOOP:
5560db79fff0 add ebp, +0x01
5560db79fff3 cmp ebp, +0x3c
5560db79fff6 jle 0x5560db79fff0 ->LOOP
5560db79fff8 jmp 0x5560db79001c ->3
---- TRACE 1 stop -> loop
local ffi = require 'ffi'
local date_mt = {
secs = 0,
nsec = 0,
}
local date = setmetatable({ secs = 123, nsec = 456}, date_mt)
local days = function(self)
return (tonumber(self.secs) + self.nsec / 1e9) / (60 * 60) / 24
end
local foo
for _=1,10 do
foo = days(date)
end
luajit -jdump -O3 -Ohotloop=9 -Ocse -Ofold ../days_ffi.lua
# / (60 * 60) / 24 == / (60 * 60 * 24)
# but nobody folded this expression
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment