Skip to content

Commit

Permalink
detect constant side conditions
Browse files Browse the repository at this point in the history
  • Loading branch information
lukaszcz committed Oct 29, 2024
1 parent 962face commit 59062d5
Show file tree
Hide file tree
Showing 2 changed files with 43 additions and 1 deletion.
1 change: 0 additions & 1 deletion src/Juvix/Compiler/Core/Transformation/ComputeCaseANF.hs
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,6 @@ module Juvix.Compiler.Core.Transformation.ComputeCaseANF (computeCaseANF) where
-- ```
-- let z := f x in case z of { c y := y + x; d y := y }
-- ```
-- This transformation is needed for the Nockma backend.

import Juvix.Compiler.Core.Data.BinderList qualified as BL
import Juvix.Compiler.Core.Extra
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
module Juvix.Compiler.Core.Transformation.DetectConstantSideConditions
( detectConstantSideConditions,
)
where

import Juvix.Compiler.Core.Extra
import Juvix.Compiler.Core.Transformation.Base

detectConstantSideConditions :: Module -> Module
detectConstantSideConditions md = mapAllNodes (umap go) md
where
boolSym = lookupConstructorInfo md (BuiltinTag TagTrue) ^. constructorInductive

go :: Node -> Node
go node = case node of
NMatch m -> NMatch (over matchBranches (concatMap convertMatchBranch) m)
_ -> node

convertMatchBranch :: MatchBranch -> [MatchBranch]
convertMatchBranch br@MatchBranch {..} =
case _matchBranchRhs of
MatchBranchRhsExpression {} ->
[br]
MatchBranchRhsIfs ifs ->
case ifs1 of
[] ->
case nonEmpty ifs0 of
Nothing -> []
Just ifs0' -> [set matchBranchRhs (MatchBranchRhsIfs ifs0') br]
SideIfBranch {..} : ifs1' ->
let ifsBody = mkIfs boolSym (map (\(SideIfBranch i c b) -> (i, c, b)) ifs0) _sideIfBranchBody
in set matchBranchRhs (MatchBranchRhsExpression ifsBody) br
:
-- All branches after the first true branch are redundant
-- and can be removed. We leave one of the redundant
-- branches to make redundant pattern detection work in this
-- case.
case ifs1' of
[] -> []
if1 : _ -> [set matchBranchRhs (MatchBranchRhsExpression (if1 ^. sideIfBranchBody)) br]
where
ifs' = filter (not . isFalseConstr . (^. sideIfBranchCondition)) (toList ifs)
(ifs0, ifs1) = span (not . isTrueConstr . (^. sideIfBranchCondition)) ifs'

0 comments on commit 59062d5

Please sign in to comment.