-
Notifications
You must be signed in to change notification settings - Fork 51
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Simplifies arithmetic expressions in the Core optimization phase, changing e.g. `(x - 1) + 1` to `x`. Such expressions appear as a result of compiling pattern matching on natural numbers.
- Loading branch information
Showing
5 changed files
with
89 additions
and
2 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
64 changes: 64 additions & 0 deletions
64
src/Juvix/Compiler/Core/Transformation/Optimize/SimplifyArithmetic.hs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,64 @@ | ||
module Juvix.Compiler.Core.Transformation.Optimize.SimplifyArithmetic (simplifyArithmetic) where | ||
|
||
import Juvix.Compiler.Core.Extra | ||
import Juvix.Compiler.Core.Transformation.Base | ||
|
||
convertNode :: Node -> Node | ||
convertNode = dmap go | ||
where | ||
go :: Node -> Node | ||
go node = case node of | ||
NBlt BuiltinApp {..} | ||
| _builtinAppOp == OpIntAdd, | ||
[NBlt blt', n] <- _builtinAppArgs, | ||
blt' ^. builtinAppOp == OpIntSub, | ||
[x, m] <- blt' ^. builtinAppArgs, | ||
m == n -> | ||
x | ||
NBlt BuiltinApp {..} | ||
| _builtinAppOp == OpIntSub, | ||
[NBlt blt', n] <- _builtinAppArgs, | ||
blt' ^. builtinAppOp == OpIntAdd, | ||
[x, m] <- blt' ^. builtinAppArgs -> | ||
if | ||
| m == n -> | ||
x | ||
| x == n -> | ||
m | ||
| otherwise -> | ||
node | ||
NBlt BuiltinApp {..} | ||
| _builtinAppOp == OpIntAdd, | ||
[n, NBlt blt'] <- _builtinAppArgs, | ||
blt' ^. builtinAppOp == OpIntSub, | ||
[x, m] <- blt' ^. builtinAppArgs, | ||
m == n -> | ||
x | ||
NBlt BuiltinApp {..} | ||
| _builtinAppOp == OpIntAdd || _builtinAppOp == OpIntSub, | ||
[x, NCst (Constant _ (ConstInteger 0))] <- _builtinAppArgs -> | ||
x | ||
NBlt BuiltinApp {..} | ||
| _builtinAppOp == OpIntAdd, | ||
[NCst (Constant _ (ConstInteger 0)), x] <- _builtinAppArgs -> | ||
x | ||
NBlt BuiltinApp {..} | ||
| _builtinAppOp == OpIntMul, | ||
[_, c@(NCst (Constant _ (ConstInteger 0)))] <- _builtinAppArgs -> | ||
c | ||
NBlt BuiltinApp {..} | ||
| _builtinAppOp == OpIntMul, | ||
[c@(NCst (Constant _ (ConstInteger 0))), _] <- _builtinAppArgs -> | ||
c | ||
NBlt BuiltinApp {..} | ||
| _builtinAppOp == OpIntMul, | ||
[x, NCst (Constant _ (ConstInteger 1))] <- _builtinAppArgs -> | ||
x | ||
NBlt BuiltinApp {..} | ||
| _builtinAppOp == OpIntMul, | ||
[NCst (Constant _ (ConstInteger 1)), x] <- _builtinAppArgs -> | ||
x | ||
_ -> node | ||
|
||
simplifyArithmetic :: InfoTable -> InfoTable | ||
simplifyArithmetic = mapAllNodes convertNode |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
42 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,15 @@ | ||
-- Arithmetic simplification | ||
module test065; | ||
|
||
import Stdlib.Prelude open; | ||
|
||
{-# inline: false #-} | ||
f (x : Int) : Int := | ||
(x + fromNat 1 - fromNat 1) * fromNat 1 | ||
+ fromNat 0 * x | ||
+ (fromNat 10 + (x - fromNat 10)) | ||
+ (fromNat 10 + x - fromNat 10) | ||
+ (fromNat 11 + (fromNat 11 - x)) | ||
+ fromNat 1 * x * fromNat 0 * fromNat 1; | ||
|
||
main : Int := f (fromNat 10); |