papyrus-compiler icon indicating copy to clipboard operation
papyrus-compiler copied to clipboard

Papyrinth Optimizations

Open erri120 opened this issue 2 years ago • 1 comments

From @cadpnq

  • When two labels are next to one another all branches pointing at the first can be rewritten to point to the second.
  • When a label is followed by a jump all branches to the label can be rewritten to point to the target of the jump.
  • A label with no branches targeting it can be removed.
  • All code between a jump or return and the next label is dead and can be removed.
  • Branches followed by their target can be removed.
  • A conditional branch pointing at an identical conditional branch can be written to point at the target of the second one.
  • A conditional branch pointing at an opposite conditional branch can be written to point at just after the second branch.
  • Assigning or casting something to itself can be removed.
  • With the exception of function calls, any instruction storing to ::nonevar can be removed.
  • A cast instruction where dest and source are of the same type can be turned into an assign instruction.
  • Any instruction except function calls that store a value that is not used can be removed.
  • Two groups of temporaries can be combined if they are of the same type and none of them interfere with each other.
  • When a value is not'ed and only used in a branch the not can be removed and the branch can be rewritten to use the opposite condition.
  • Anything storing to a temp that is only used in an assign to another variable can be rewritten to store directly to that variable.
  • Any assign to a temporary that is only used in the next instruction can be removed and the next instruction rewritten to use the source of the assign.

erri120 avatar Jun 19 '22 15:06 erri120

It's worth noting that on some of these referencing labels we're talking about branch destinations. You will likely have something different, but during script loading in papyrith I convert branch offsets into references to uniquely named psuedo-instructions that I inserted into the bytecode.

cadpnq avatar Jun 19 '22 19:06 cadpnq