Skip to content

Commit 0a48bbc

Browse files
committed
Add safe OP_NOT OP_NOT optimisations
1 parent 202ec4d commit 0a48bbc

1 file changed

Lines changed: 8 additions & 4 deletions

File tree

‎packages/utils/src/optimisations.ts‎

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -137,10 +137,6 @@ export const optimisationReplacements = [
137137
['OP_DUP OP_AND', ''],
138138
['OP_DUP OP_OR', ''],
139139

140-
// TODO: Enable this optimisation when we overhaul the type system
141-
// (right now bool(4) == true => false, but !!bool(4) == true => true) so can't replace OP_NOT OP_NOT with ''
142-
// ['OP_NOT OP_NOT', '']
143-
144140
// Invert comparison operators instead of negating them
145141
['OP_LESSTHAN OP_NOT', 'OP_GREATERTHANOREQUAL'],
146142
['OP_GREATERTHAN OP_NOT', 'OP_LESSTHANOREQUAL'],
@@ -149,4 +145,12 @@ export const optimisationReplacements = [
149145

150146
// This can get emitted by tuple destructuring
151147
['OP_TOALTSTACK OP_FROMALTSTACK', ''],
148+
149+
// unsafe_bool(4) == true => false, but !!unsafe_bool(4) == true => true) so we can't replace OP_NOT OP_NOT with ''
150+
// in the general case, but when it is followed by a consuming instruction that does not differentiate between
151+
// true and truthy values (e.g. OP_IF, OP_UNTIL, OP_VERIFY), we can replace OP_NOT OP_NOT with ''
152+
// Note that technically OP_NOT OP_NOT would also do a VM-number check, which gets removed by this optimisation
153+
['OP_NOT OP_NOT OP_UNTIL', 'OP_UNTIL'],
154+
['OP_NOT OP_NOTIF', 'OP_IF'],
155+
['OP_NOT OP_NOT OP_VERIFY', 'OP_VERIFY'],
152156
] as [string, string][];

0 commit comments

Comments
 (0)