mirror of
https://github.com/ethereum/solidity
synced 2023-10-03 13:03:40 +00:00
Add optimizer rules for repeated and
This commit is contained in:
parent
8c60b2c847
commit
606153ba71
@ -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
|
||||
|
||||
|
39
test/formal/repeated_and.py
Normal file
39
test/formal/repeated_and.py
Normal 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)
|
39
test/formal/repeated_or.py
Normal file
39
test/formal/repeated_or.py
Normal 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)
|
Loading…
Reference in New Issue
Block a user