X-Git-Url: http://git.megacz.com/?a=blobdiff_plain;f=compiler%2Fcmm%2FDFMonad.hs;h=263d0d4856b3ffd70a77c01f1498ed06037438c5;hb=e239aa2329416a2822fcc03c4ed486c7d28739e1;hp=0bce264de6fc510570bc9b2c6abaf6b08e86b708;hpb=e6243a818496aad82b6f47511d3bd9bc800f747d;p=ghc-hetmet.git diff --git a/compiler/cmm/DFMonad.hs b/compiler/cmm/DFMonad.hs index 0bce264..263d0d4 100644 --- a/compiler/cmm/DFMonad.hs +++ b/compiler/cmm/DFMonad.hs @@ -45,11 +45,11 @@ conjunction with the join, so we have [[fact_add_to]]: -} data DataflowLattice a = DataflowLattice { - fact_name :: String, -- documentation - fact_bot :: a, -- lattice bottom element - fact_add_to :: a -> a -> TxRes a, -- lattice join and compare + fact_name :: String, -- documentation + fact_bot :: a, -- lattice bottom element + fact_add_to :: a -> a -> TxRes a, -- lattice join and compare -- ^ compute join of two args; something changed iff join is greater than 2nd arg - fact_do_logging :: Bool -- log changes + fact_do_logging :: Bool -- log changes } @@ -59,14 +59,14 @@ data DataflowLattice a = DataflowLattice { -- case of DFM, parameterized over any monad. -- In practice, we apply DFM' to the FuelMonad, which provides optimization fuel and -- the unique supply. -data DFState f = DFState { df_rewritten :: ChangeFlag - , df_facts :: BlockEnv f - , df_exit_fact :: f - , df_last_outs :: [(BlockId, f)] - , df_facts_change :: ChangeFlag +data DFState f = DFState { df_rewritten :: !ChangeFlag + , df_facts :: !(BlockEnv f) + , df_exit_fact :: !f + , df_last_outs :: ![(BlockId, f)] + , df_facts_change :: !ChangeFlag } -newtype DFM' m fact a = DFM' (DataflowLattice fact -> DFState fact +newtype DFM' m fact a = DFM' (DataflowLattice fact -> DFState fact -> m (a, DFState fact)) type DFM fact a = DFM' FuelMonad fact a @@ -136,15 +136,11 @@ instance Monad m => DataflowAnalysis (DFM' m) where getExitFact = DFM' get where get _ s = return (df_exit_fact s, s) setExitFact a = - do old <- getExitFact - DataflowLattice { fact_add_to = add_fact - , fact_name = name, fact_do_logging = log } <- lattice - case add_fact a old of - TxRes NoChange _ -> return () - TxRes SomeChange join -> DFM' $ \_ s -> - let debug = if log then pprTrace else \_ _ a -> a - in debug name (pprSetFact "exit" old a join) $ - return ((), s { df_exit_fact = join, df_facts_change = SomeChange }) + do DataflowLattice { fact_name = name, fact_do_logging = log} <- lattice + DFM' $ \_ s -> + let debug = if log then pprTrace else \_ _ a -> a + in debug name (pprSetFact "exit" a a a) $ + return ((), s { df_exit_fact = a }) getAllFacts = DFM' f where f _ s = return (df_facts s, s) setAllFacts env = DFM' f @@ -194,8 +190,12 @@ graphWasRewritten = DFM' f instance Monad m => Monad (DFM' m f) where DFM' f >>= k = DFM' (\l s -> do (a, s') <- f l s - let DFM' f' = k a in f' l s') + s' `seq` case k a of DFM' f' -> f' l s') return a = DFM' (\_ s -> return (a, s)) + -- The `seq` is essential to ensure that entire passes of the dataflow engine + -- aren't postponed in a thunk. By making the sequence strict in the state, + -- we ensure that each action in the monad is executed immediately, preventing + -- stack overflows that previously occurred when finally forcing the old state thunks. instance FuelUsingMonad (DFM' FuelMonad f) where fuelRemaining = liftToDFM' fuelRemaining