escalier-lang / escalier-next

Improved type safety with tight TypeScript interop
https://escalier-lang.github.io/escalier-next/
MIT License
14 stars 0 forks source link

Optimize codegen output by renaming temporary variables #307

Closed kevinbarabash closed 4 months ago

kevinbarabash commented 4 months ago

kira posted the following code:

local result__10
local var__11
local var__12
local var__13
var__11 = function (a__15, b__16)
  local result__14
  local arg__17
  arg__17 = a__15
  local arg__18
  arg__18 = b__16
  result__14 = arg__17 + arg__18
  return result__14
end
var__12 = 1
var__13 = 2
local arg__19
arg__19 = var__12
local arg__20
arg__20 = var__13
local fn__21
fn__21 = var__11
result__10 = fn__21(arg__19, arg__20)
return result__10

and icefox replied with:

Congratulations, you've reinvented static single-assignment form! :3 this is generally a good thing. Idk what the operation would be called, but I thiiiiiink you can take out the redundant variables via renaming them. So whenever you see arg19 = var12 in a function you just go through the rest of the function's body and if you see arg19 just replace it with var12. Then when it results in var12 = var12 you can just remove that. Do that for every variable in the program and you should reduce it to the minimum number of variables needed? I think it will work.

kevinbarabash commented 4 months ago

Since this would require introducing an SSA IR with Phi nodes, it's easier to let terser (or some other minified) optimize the generated JavaScript.