Merge pull request #8709 from ethereum/repeated_and_or_rules

Add optimizer rules for repeated `and` and `or`
This commit is contained in:
chriseth 2020-04-22 11:21:40 +02:00 committed by GitHub
commit d0fcd468f6
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
4 changed files with 83 additions and 2 deletions

View File

@ -347,8 +347,8 @@ jobs:
name: Z3 python deps
command: |
apt-get -qq update
apt-get -qy install python-pip
pip install --user z3-solver
apt-get -qy install python3-pip
pip3 install --user z3-solver
- run: *run_proofs
chk_docs_pragma_min_version:

View File

@ -42,6 +42,9 @@ def ISZERO(x):
def AND(x, y):
return x & y
def OR(x, y):
return x | y
def SHL(x, y):
return y << x

View File

@ -0,0 +1,39 @@
from rule import Rule
from opcodes import *
"""
Rule:
AND(AND(X, Y), Y) -> AND(X, Y)
AND(Y, AND(X, Y)) -> AND(X, Y)
AND(AND(Y, X), Y) -> AND(Y, X)
AND(Y, AND(Y, X)) -> AND(Y, X)
Requirements:
"""
rule = Rule()
n_bits = 256
# Input vars
X = BitVec('X', n_bits)
Y = BitVec('Y', n_bits)
# Constants
BitWidth = BitVecVal(n_bits, n_bits)
# Requirements
# Non optimized result
nonopt_1 = AND(AND(X, Y), Y)
nonopt_2 = AND(Y, AND(X, Y))
nonopt_3 = AND(AND(Y, X), Y)
nonopt_4 = AND(Y, AND(Y, X))
# Optimized result
opt_1 = AND(X, Y)
opt_2 = AND(Y, X)
rule.check(nonopt_1, opt_1)
rule.check(nonopt_2, opt_1)
rule.check(nonopt_3, opt_2)
rule.check(nonopt_4, opt_2)

View File

@ -0,0 +1,39 @@
from rule import Rule
from opcodes import *
"""
Rule:
OR(OR(X, Y), Y) -> OR(X, Y)
OR(Y, OR(X, Y)) -> OR(X, Y)
OR(OR(Y, X), Y) -> OR(Y, X)
OR(Y, OR(Y, X)) -> OR(Y, X)
Requirements:
"""
rule = Rule()
n_bits = 256
# Input vars
X = BitVec('X', n_bits)
Y = BitVec('Y', n_bits)
# Constants
BitWidth = BitVecVal(n_bits, n_bits)
# Requirements
# Non optimized result
nonopt_1 = OR(OR(X, Y), Y)
nonopt_2 = OR(Y, OR(X, Y))
nonopt_3 = OR(OR(Y, X), Y)
nonopt_4 = OR(Y, OR(Y, X))
# Optimized result
opt_1 = OR(X, Y)
opt_2 = OR(Y, X)
rule.check(nonopt_1, opt_1)
rule.check(nonopt_2, opt_1)
rule.check(nonopt_3, opt_2)
rule.check(nonopt_4, opt_2)