Combine adjacent DROP

This commit is contained in:
Tom Jack 2019-10-24 23:56:26 -05:00
parent 9ae39bab97
commit dae4f58297
2 changed files with 17 additions and 0 deletions

View File

@ -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

View File

@ -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