Skip to content

Commit 90250ce

Browse files
authored
Expr: Further simplification of constPropagate (#950)
1 parent c9d7269 commit 90250ce

File tree

1 file changed

+20
-25
lines changed

1 file changed

+20
-25
lines changed

src/EVM/Expr.hs

Lines changed: 20 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ module EVM.Expr where
99
import Prelude hiding (LT, GT)
1010
import Control.Monad (unless, when)
1111
import Control.Monad.ST (ST)
12-
import Control.Monad.State (put, get, modify, execState, State)
12+
import Control.Monad.State (put, get, execState, State)
1313
import Data.Bits hiding (And, Xor)
1414
import Data.ByteString (ByteString)
1515
import Data.ByteString qualified as BS
@@ -1694,6 +1694,13 @@ constPropagate ps =
16941694
conflictState = ConstState mempty mempty mempty False
16951695
conflict = put conflictState
16961696

1697+
setExactValue :: Expr EWord -> W256 -> State ConstState ()
1698+
setExactValue e v = do
1699+
s <- get
1700+
case Map.lookup e s.values of
1701+
Just old -> when (old /= v) conflict
1702+
_ -> put s { values = Map.insert e v s.values }
1703+
16971704
updateLower :: Expr EWord -> W256 -> State ConstState ()
16981705
updateLower a l = do
16991706
s <- get
@@ -1703,12 +1710,7 @@ constPropagate ps =
17031710
if newL > currentU
17041711
then conflict
17051712
else put s { lowerBounds = Map.insert a newL s.lowerBounds }
1706-
-- Check if equal to upper, then it's a constant
1707-
when (newL == currentU) $ do
1708-
s' <- get
1709-
case Map.lookup a s'.values of
1710-
Just v -> when (v /= newL) conflict
1711-
Nothing -> put s' { values = Map.insert a newL s'.values }
1713+
when (newL == currentU) $ setExactValue a newL
17121714

17131715
updateUpper :: Expr EWord -> W256 -> State ConstState ()
17141716
updateUpper a u = do
@@ -1720,35 +1722,28 @@ constPropagate ps =
17201722
then conflict
17211723
else put s { upperBounds = Map.insert a newU s.upperBounds }
17221724
-- Check if equal to lower, then it's a constant
1723-
when (currentL == newU) $ do
1724-
s' <- get
1725-
case Map.lookup a s'.values of
1726-
Just v -> when (v /= newU) conflict
1727-
Nothing -> put s' { values = Map.insert a newU s'.values }
1728-
1729-
genericEq :: W256 -> Expr EWord -> State ConstState ()
1730-
genericEq l a = do
1731-
s <- get
1732-
case Map.lookup a s.values of
1733-
Just l2 -> unless (l == l2) conflict
1734-
Nothing -> modify (\s' -> s' { values = Map.insert a l s'.values })
1735-
updateLower a l
1736-
updateUpper a l
1725+
when (currentL == newU) $ setExactValue a newU
1726+
1727+
genericEq :: Expr EWord -> W256 -> State ConstState ()
1728+
genericEq a v = do
1729+
setExactValue a v
1730+
updateLower a v
1731+
updateUpper a v
17371732

17381733
go :: Prop -> State ConstState ()
17391734
go = \case
17401735
-- signed inequalities
17411736
PEq (Lit 1) term@(SLT a (Lit 0)) -> do
1742-
genericEq 1 term
1737+
genericEq term 1
17431738
updateLower a minLitSigned
17441739
PEq (Lit 1) term@(SLT (Lit 0) a) -> do
1745-
genericEq 1 term
1740+
genericEq term 1
17461741
updateLower a 1
17471742
updateUpper a maxLitSigned
17481743

17491744
-- normal equality propagation
1750-
PEq (Lit l) a -> genericEq l a
1751-
PEq a b@(Lit _) -> go (PEq b a)
1745+
PEq (Lit l) a -> genericEq a l
1746+
PEq a (Lit l) -> genericEq a l
17521747

17531748
PNeg (PEq (Lit l) a) -> do
17541749
s <- get

0 commit comments

Comments
 (0)