Upvalue elimination in loops

  • From: Szabó Antal <szabo.antal.92@xxxxxxxxx>
  • To: luajit@xxxxxxxxxxxxx
  • Date: Sun, 24 Nov 2013 01:29:02 +0100

Hi Mike, list,

I have the following very stripped down test code:

local function nums()
    local i = 0
    return function()
        i = i + 1
        return i
    end
end

for v in nums() do end

On Windows x64, this produces the following loop:

->LOOP:
7fedc65fff0  addsd xmm7, xmm0
7fedc65fff4  movsd [0x30e1ced8], xmm7
7fedc65fffd  jmp 0x7fedc65fff0  ->LOOP

Now there's a move that's copying back the current value of i into the
upvalue, which I think is a dead store at all but the last iteration,
thus LuaJIT could move it after the loop (well, in this example
there's actually no "after the loop", but generally).

This *seems* easy, but I'm far from an expert in this area.

So is this something that's just not implemented because nobody ever
needed it, or is it too hard (impossible?) to do?


Antal Szabó

Other related posts: