diff --git a/src/passes/9-self_michelson/self_michelson.ml b/src/passes/9-self_michelson/self_michelson.ml index 46c2b374d..742e64341 100644 --- a/src/passes/9-self_michelson/self_michelson.ml +++ b/src/passes/9-self_michelson/self_michelson.ml @@ -365,6 +365,21 @@ let rec opt_tail_fail : michelson -> michelson = Prim (l, p, List.map opt_tail_fail args, annot) | x -> x +let opt_combine_drops : peep2 = function + (* DROP ; DROP ↦ DROP 2 *) + | Prim (_, I_DROP, [], _), Prim (_, I_DROP, [], _) -> + Some [i_dropn 2] + (* DROP ; DROP m ↦ DROP 1+m *) + | Prim (_, I_DROP, [], _), Prim (_, I_DROP, [Int (_, m)], _) -> + Some [i_dropn (1 + Z.to_int m)] + (* DROP n ; DROP ↦ DROP n+1 *) + | Prim (_, I_DROP, [Int (_, n)], _), Prim (_, I_DROP, [], _) -> + Some [i_dropn (Z.to_int n + 1)] + (* DROP n ; DROP m ↦ DROP n+m *) + | Prim (_, I_DROP, [Int (_, n)], _), Prim (_, I_DROP, [Int (_, m)], _) -> + Some [i_dropn (Z.to_int n + Z.to_int m)] + | _ -> None + let optimize : michelson -> michelson = fun x -> let x = use_lambda_instr x in @@ -378,4 +393,5 @@ let optimize : michelson -> michelson = peephole @@ peep2 opt_swap2 ; ] in let x = iterate_optimizer (sequence_optimizers optimizers) x in + let x = iterate_optimizer (peephole @@ peep2 opt_combine_drops) x in x diff --git a/vendors/ligo-utils/tezos-utils/x_michelson.ml b/vendors/ligo-utils/tezos-utils/x_michelson.ml index a922fa382..e82237d00 100644 --- a/vendors/ligo-utils/tezos-utils/x_michelson.ml +++ b/vendors/ligo-utils/tezos-utils/x_michelson.ml @@ -58,6 +58,7 @@ let i_some = prim I_SOME let i_lambda arg ret body = prim ~children:[arg;ret;body] I_LAMBDA let i_empty_map src dst = prim ~children:[src;dst] I_EMPTY_MAP let i_drop = prim I_DROP +let i_dropn n = prim I_DROP ~children:[int (Z.of_int n)] let i_exec = prim I_EXEC let i_if a b = prim ~children:[seq [a] ; seq[b]] I_IF