Regression in the morley optimizer due to dipDrop2SwapDrop
Description
There's an unfortunate interaction between dip unrolling and dipDrop2SwapDrop rule. Consider that the main optimizer stage is currently unrolling DIP n
into a sequence of nested DIP
s. Then consider that dipDrop2SwapDrop
replaces DIP DROP
with SWAP; DROP
. Now realize this means DIP n DROP
will invariably turn into DIP (n-1) {SWAP; DROP}
which is generally not what we want.
That being said, in some cases it is what we want, if it opens up additional optimization possibilities, e.g. consider this contrived example:
DIP 3 { SWAP };
DIP 4 { DROP };
One way to handle this that immediately comes to mind is to convert DIP n { SWAP; DROP }
back into DIP (n+1) { DROP }
at the last stage. This is not necessarily the silver bullet, however, but at least better than what we have currently.
Steps to reproduce
Try to optimize the following contract:
parameter bool;
storage unit;
code { CAR;
DIP { UNIT; UNIT; UNIT; UNIT };
IF { UNIT; FAILWITH } { };
DIP 3 { DROP };
NIL operation;
DIP { DROP 2 };
PAIR };
Expected behaviour
Contract is unchanged
Actual behaviour
parameter bool;
storage unit;
code { CAR;
DIP { UNIT; UNIT; UNIT; UNIT };
IF { UNIT; FAILWITH } { };
DIP 2 { SWAP; DROP };
NIL operation;
DIP { DROP 2 };
PAIR };
Environment
- <replace with branch/revision>