Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Browse files
Browse the repository at this point in the history
[hoopl] added a constant propagation pass which does nothing but
typechecks
- Loading branch information
Showing
5 changed files
with
100 additions
and
11 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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,63 @@ | ||
| {-# OPTIONS_GHC -Wall -fno-warn-name-shadowing #-} | ||
| {-# LANGUAGE ScopedTypeVariables, GADTs, NoMonomorphismRestriction #-} | ||
| module ConstProp (ConstFact, constLattice, initFact, varHasLit, constProp, constPropPass) where | ||
|
|
||
| --import Control.Monad | ||
| import Insn | ||
| import qualified Data.Map as Map | ||
|
|
||
| import Compiler.Hoopl | ||
| --import IR | ||
| --import OptSupport | ||
|
|
||
| -- ConstFact: | ||
| -- Not present in map => bottom | ||
| -- PElem v => variable has value v | ||
| -- Top => variable's value is not constant | ||
| -- Type and definition of the lattice | ||
|
|
||
| type ConstFact = Map.Map Int (WithTop Expr) | ||
| constLattice :: DataflowLattice ConstFact | ||
| constLattice = DataflowLattice | ||
| { fact_name = "Constant propagation" | ||
| , fact_bot = Map.empty | ||
| , fact_join = joinMaps (extendJoinDomain constFactAdd) } | ||
| where | ||
| constFactAdd _ (OldFact old) (NewFact new) | ||
| = if new == old then (NoChange, PElem new) | ||
| else (SomeChange, Top) | ||
|
|
||
| -- Initially, we assume that all variable values are unknown. | ||
|
|
||
| -- Only interesting semantic choice: values of variables are live across | ||
| -- a call site. | ||
| -- Note that we don't need a case for x := y, where y holds a constant. | ||
| -- We can write the simplest solution and rely on the interleaved optimization. | ||
| -- @ start cprop.tex | ||
| -------------------------------------------------- | ||
| -- Analysis: variable equals a literal constant | ||
| varHasLit :: FwdTransfer Insn ConstFact | ||
| varHasLit = mkFTransfer ft | ||
| where | ||
| ft :: Insn e x -> ConstFact -> Fact x ConstFact | ||
|
|
||
| ft (BifPlus reg _ _) f = Map.insert reg Top f | ||
| ft (Subcall reg _) f = Map.insert reg Top f | ||
| ft (Fetch reg _) f = Map.insert reg Top f | ||
| ft (RegSet reg constant@(Double _)) f = Map.insert reg (PElem constant) f | ||
| ft (RegSet reg _) f = Map.insert reg Top f | ||
|
|
||
| constPropPass = FwdPass | ||
| { fp_lattice = constLattice | ||
| , fp_transfer = varHasLit | ||
| , fp_rewrite = constProp } | ||
|
|
||
| initFact :: ConstFact | ||
| initFact = Map.fromList [] | ||
| -- @ start cprop.tex | ||
| -------------------------------------------------- | ||
| -- Rewriting: replace constant variables | ||
| constProp :: FuelMonad m => FwdRewrite m Insn ConstFact | ||
| constProp = mkFRewrite cp | ||
| where | ||
| cp node f = return Nothing |
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,13 @@ | ||
| {-# LANGUAGE ViewPatterns,GADTs,StandaloneDeriving,NoMonomorphismRestriction #-} | ||
| module Insn (Insn(..),Expr(..)) where | ||
| import Compiler.Hoopl | ||
| -- a side effect free expression | ||
| -- FIXME handle Box and Ann smartly | ||
| data Expr = Double Double | StrLit String | ScopedLex Expr | Reg Int | ||
| deriving (Show,Eq) | ||
|
|
||
| data Insn e x where | ||
| Fetch :: Int -> Expr -> Insn O O | ||
| Subcall :: Int -> [Expr] -> Insn O O | ||
| BifPlus :: Int -> Expr -> Expr -> Insn O O | ||
| RegSet :: Int -> Expr -> Insn O O |
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
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