Skip to content

Commit

Permalink
clarify some conditions
Browse files Browse the repository at this point in the history
  • Loading branch information
charles-cooper committed Dec 11, 2024
1 parent e07d87e commit dce1935
Showing 1 changed file with 17 additions and 13 deletions.
30 changes: 17 additions & 13 deletions vyper/venom/passes/literals_codesize.py
Original file line number Diff line number Diff line change
Expand Up @@ -38,20 +38,24 @@ def _process_bb(self, bb):
ix = len(binz) - binz.rfind("1")
shl_benefit = ix - SHL_THRESHOLD * 8

if not_benefit <= 0 and shl_benefit <= 0:
# no optimization can be done here
continue

if not_benefit >= shl_benefit:
assert not_benefit > 0 # implied by previous conditions
# transform things like 0xffff...01 to (not 0xfe)
if not_benefit > 0:
inst.opcode = "not"
op.value = evm_not(val)
continue
inst.opcode = "not"
op.value = evm_not(val)
continue
else:
assert shl_benefit > 0 # implied by previous conditions
# transform things like 0x123400....000 to 0x1234 << ...
if shl_benefit > 0:
ix -= 1
# sanity check
assert (val >> ix) << ix == val, val
assert (val >> ix) & 1 == 1, val

inst.opcode = "shl"
inst.operands = [IRLiteral(val >> ix), IRLiteral(ix)]
continue
ix -= 1
# sanity check
assert (val >> ix) << ix == val, val
assert (val >> ix) & 1 == 1, val

inst.opcode = "shl"
inst.operands = [IRLiteral(val >> ix), IRLiteral(ix)]
continue

0 comments on commit dce1935

Please sign in to comment.