LSL-PyOptimizer icon indicating copy to clipboard operation
LSL-PyOptimizer copied to clipboard

Avoid defining unnecessary variables while inlining

Open TheDirtyDemon opened this issue 3 years ago • 1 comments

At the beginning of an inlined function, all parameters get assigned to new local variables even if they are never written to and therefore could be used without creating a copy.

An example with the default settings + Manual function inlining:

foo(integer num) inline {
  llSay(num, "foo");
}

default {
  touch_start(integer num) {
    foo(num);
  }
}

Gets turned into:

default
{
    touch_start(integer LslLibrary)
    {
        {
            integer loc_num = LslLibrary;
            {
                llSay(loc_num, "foo");
            }
        }
    }
}

There does not seem to be a reason to create loc_num in this case.

TheDirtyDemon avatar Jan 11 '23 16:01 TheDirtyDemon

Thanks for the report. This is a known issue. I've tried to work on approaches to fix it but so far I've been unsuccessful. Translating to Static Single Assignment form would help, but since this is a source-source compiler, SSA is very difficult to implement. It's possible to convert expressions to three-address code for applying SSA and then re-compress them to a shorter expression, but the resulting expressions are not guaranteed to take less code space, because Mono is weird.

There have been past failed attempts at something simpler that would cover this case. https://github.com/Sei-Lisa/LSL-PyOptimizer/commit/454d44e85fb7e70d4f330da9054850f85a9482ff is such an attempt, but it had to be reverted because there wasn't proper value and control flow tracking; a blatant example was that exchanging of variables (tmp = a; a = b; b = tmp) produced incorrect code when optimized. https://github.com/Sei-Lisa/LSL-PyOptimizer/commit/42f47d38f00b4f557288d41319bcec00ed419d36

Sei-Lisa avatar Jan 11 '23 19:01 Sei-Lisa