Add optimizer rules for repeated and

This commit is contained in:
Leonardo Alt 2020-04-20 16:14:22 +02:00
parent 8c60b2c847
commit 606153ba71
3 changed files with 81 additions and 0 deletions

View File

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