Skip to content

Michelson optimization: factor out common last actions in conditionals

E. Rivas requested to merge er433/opt/cond into dev

type:internal

The output of:

function main(const _param : unit; var s : int) : list (operation) * int is
  block {
    var tet_ops : list (operation) := nil;
    if 1 >= 0 then {
      s := s;    
      tet_ops := tet_ops;
    } else s := s
  } with (tet_ops, s)

is:

{ parameter unit ;
  storage int ;
  code { CDR ;
         NIL operation ;
         PUSH int 0 ;
         PUSH int 1 ;
         COMPARE ;
         GE ;
         IF { SWAP ; PAIR } { SWAP ; PAIR } ;
         DUP ;
         CAR ;
         SWAP ;
         CDR ;
         PAIR } }

There are optimizations not firing because of conditional in the middle.

This MR proposes to factor out common last actions in conditionals: IF { SWAP ; PAIR } { SWAP ; PAIR } becomes IF { SWAP } { SWAP } ; PAIR. Applying this one more time, and some other opt. firing, we get the following code:

{ parameter unit ;      
  storage int ;
  code { CDR ;
         NIL operation ;
         PUSH int 0 ;
         PUSH int 1 ;
         COMPARE ;
         GE ;
         IF {} {} ;
         PAIR } }

Changelog details: A new optimization has been added in contract compilation. By default, it will not use type information (for better compilation performance), but it can be enabled using --enable-michelson-typed-opt.

Edited by E. Rivas

Merge request reports