Michelson level optimization
Non exhaustive list of rewrites:
-
{}
=> empty -
IF_* seq seq
=>seq
-
SWAP; op
=>op
whenop
is commutative, ex.ADD
-
SWAP; COMPARE; cond
=>COMPARE; cond'
wherecond'
is opposite ofcond
, exLE
forGE
. -
PUSH bool t; IF seq_true seq_false
=>seq_t
=> can be done by partial eval -
NOT; IF seq1 seq2
=>IF seq2 seq1
-
PAIR; UNPAIR
=> empty -
PUSH (set ty) {}
=>EMPTY_SET ty
. Same for Map and BigMap. -
PUSH _ (Pair a b); UNPAIR
=>PUSH _ b; PUSH _ a
-
IF { PUSH _ False } { PUSH _ True }
=>NOT
(found in ml_match_when.ml) -
IF { PUSH _ True } { PUSH _ False }
=> empty -
IF_LEFT { DROP; ops } { DROP; ops }
=>ops
(found in ml_match.ml)
Note: comments must be preserved
The followings can be done by a more general stack operation optimizations:
-
DIG 1
=>SWAP
-
SWAP; SWAP
=> empty-
SWAP; DIG 1
=> empty
-
-
DIP seq; DROP
=>DROP; seq
-
DIP { DROP }
=>SWAP; DROP
-
DROP x; DROP y
=>DROP (x+y)
-
(DIP x { DROP y })*
=> ??? when x >= 0 && y > 0. For example,DIP 1 { DROP }; DROP 3
=>DROP 4
-
PUSH tya a; PUSH tyb b; SWAP
=>PUSH tyb b; PUSH tya a
-
PUSH _ a; PUSH _ b; SWAP
=>PUSH _ b; PUSH _ a
Edited by Jun Furuse