Merge branch 'cλash' of http://git.stderr.nl/matthijs/projects/master-project
authorChristiaan Baaij <christiaan.baaij@gmail.com>
Mon, 17 Aug 2009 10:18:11 +0000 (12:18 +0200)
committerChristiaan Baaij <christiaan.baaij@gmail.com>
Mon, 17 Aug 2009 10:18:11 +0000 (12:18 +0200)
* 'cλash' of http://git.stderr.nl/matthijs/projects/master-project:
  Make letflat work for nonrec lets in rec lets as well.
  Revert "Limit flattenLets to non-recursive lets only."
  Disable letmerge.
  Make letremoveunused work for recursive lets again.
  Make letflat work for recursive lets again.
  Turn of Normalize debugging again, accidentally committed.

cλash/CLasH/Normalize.hs
cλash/CLasH/Normalize/NormalizeTools.hs
cλash/CLasH/Utils/Core/CoreTools.hs

index 107876bf0f88a9358c5223b104b85231fccedeef..5d2b3a67852d94820ff589d72697eb8b110974aa 100644 (file)
@@ -205,8 +205,23 @@ letsimpltop = everywhere ("letsimpl", letsimpl)
 -- to:
 -- let b' = expr' in (let b = res' in res)
 letflat, letflattop :: Transform
-letflat (Let (NonRec b (Let (NonRec b' expr')  res')) res) = 
-  change $ Let (NonRec b' expr') (Let (NonRec b res') res)
+-- Turn a nonrec let that binds a let into two nested lets.
+letflat (Let (NonRec b (Let binds  res')) res) = 
+  change $ Let binds (Let (NonRec b res') res)
+letflat (Let (Rec binds) expr) = do
+  -- Flatten each binding.
+  binds' <- Utils.concatM $ Monad.mapM flatbind binds
+  -- Return the new let. We don't use change here, since possibly nothing has
+  -- changed. If anything has changed, flatbind has already flagged that
+  -- change.
+  return $ Let (Rec binds') expr
+  where
+    -- Turns a binding of a let into a multiple bindings, or any other binding
+    -- into a list with just that binding
+    flatbind :: (CoreBndr, CoreExpr) -> TransformMonad [(CoreBndr, CoreExpr)]
+    flatbind (b, Let (Rec binds) expr) = change ((b, expr):binds)
+    flatbind (b, Let (NonRec b' expr') expr) = change [(b, expr), (b', expr')]
+    flatbind (b, expr) = return [(b, expr)]
 -- Leave all other expressions unchanged
 letflat expr = return expr
 -- Perform this transform everywhere
@@ -234,13 +249,17 @@ letremovesimpletop = everywhere ("letremovesimple", inlinebind (\(b, e) -> Trans
 -- Unused let binding removal
 --------------------------------
 letremoveunused, letremoveunusedtop :: Transform
-letremoveunused expr@(Let _ _) = do
+letremoveunused expr@(Let (NonRec b bound) res) = do
+  let used = expr_uses_binders [b] res
+  if used
+    then return expr
+    else change res
+letremoveunused expr@(Let (Rec binds) res) = do
   -- Filter out all unused binds.
   let binds' = filter dobind binds
   -- Only set the changed flag if binds got removed
-  changeif (length binds' /= length binds) (mkNonRecLets binds' res)
+  changeif (length binds' /= length binds) (Let (Rec binds') res)
     where
-      (binds, res) = flattenLets expr
       bound_exprs = map snd binds
       -- For each bind check if the bind is used by res or any of the bound
       -- expressions
@@ -249,6 +268,7 @@ letremoveunused expr@(Let _ _) = do
 letremoveunused expr = return expr
 letremoveunusedtop = everywhere ("letremoveunused", letremoveunused)
 
+{-
 --------------------------------
 -- Identical let binding merging
 --------------------------------
@@ -279,7 +299,8 @@ letmerge expr@(Let _ _) = do
 -- Leave all other expressions unchanged
 letmerge expr = return expr
 letmergetop = everywhere ("letmerge", letmerge)
-    
+-}
+
 --------------------------------
 -- Function inlining
 --------------------------------
@@ -596,7 +617,7 @@ funextracttop = everywhere ("funextract", funextract)
 
 
 -- What transforms to run?
-transforms = [argproptop, funextracttop, etatop, betatop, castproptop, letremovesimpletop, letderectop, letremovetop, letsimpltop, letflattop, scrutsimpltop, casesimpltop, caseremovetop, inlinenonreptop, appsimpltop, letmergetop, letremoveunusedtop, castsimpltop, lambdasimpltop]
+transforms = [argproptop, funextracttop, etatop, betatop, castproptop, letremovesimpletop, letderectop, letremovetop, letsimpltop, letflattop, scrutsimpltop, casesimpltop, caseremovetop, inlinenonreptop, appsimpltop, letremoveunusedtop, castsimpltop, lambdasimpltop]
 
 -- | Returns the normalized version of the given function.
 getNormalized ::
index 8d4cd08c09db48aef5b3edead9207e11c7b18747..116c84742f45f7953b7fcea588820c8d7e305da3 100644 (file)
@@ -48,8 +48,8 @@ applyboth first (name, second) expr  = do
 --        trace ("Trying to apply transform " ++ name ++ " to:\n" ++ showSDoc (nest 4 $ ppr expr') ++ "\nType: \n" ++ (showSDoc $ nest 4 $ ppr $ CoreUtils.exprType expr') ++ "\n") $
         changed 
     then 
-      trace ("Applying transform " ++ name ++ " to:\n" ++ showSDoc (nest 4 $ ppr expr') ++ "\nType: \n" ++ (showSDoc $ nest 4 $ ppr $ CoreUtils.exprType expr') ++ "\n") $
-      trace ("Result of applying " ++ name ++ ":\n" ++ showSDoc (nest 4 $ ppr expr'') ++ "\n" ++ "Type: \n" ++ (showSDoc $ nest 4 $ ppr $ CoreUtils.exprType expr'') ++ "\n" ) $
+--      trace ("Applying transform " ++ name ++ " to:\n" ++ showSDoc (nest 4 $ ppr expr') ++ "\nType: \n" ++ (showSDoc $ nest 4 $ ppr $ CoreUtils.exprType expr') ++ "\n") $
+--      trace ("Result of applying " ++ name ++ ":\n" ++ showSDoc (nest 4 $ ppr expr'') ++ "\n" ++ "Type: \n" ++ (showSDoc $ nest 4 $ ppr $ CoreUtils.exprType expr'') ++ "\n" ) $
       applyboth first (name, second) $
         expr'' 
     else 
index 3ba262251c54131b6028b216e7ff248bc09eb18d..094b70294ceabcca23e722df69c98e93e1bd85db 100644 (file)
@@ -299,17 +299,19 @@ hasStateType expr = case getType expr of
   Just ty -> isStateType ty
 
 
--- | Flattens nested non-recursive lets into a single list of bindings. The
--- expression passed does not have to be a let expression, if it isn't an
--- empty list of bindings is returned.
+-- | Flattens nested lets into a single list of bindings. The expression
+--   passed does not have to be a let expression, if it isn't an empty list of
+--   bindings is returned.
 flattenLets ::
   CoreSyn.CoreExpr -- ^ The expression to flatten.
   -> ([Binding], CoreSyn.CoreExpr) -- ^ The bindings and resulting expression.
-flattenLets (CoreSyn.Let (CoreSyn.NonRec bndr expr) res) =
-  ((bndr, expr):bindings, res')
+flattenLets (CoreSyn.Let binds expr) = 
+  (bindings ++ bindings', expr')
   where
     -- Recursively flatten the contained expression
-    (bindings, res') = flattenLets res
+    (bindings', expr') =flattenLets expr
+    -- Flatten our own bindings to remove the Rec / NonRec constructors
+    bindings = CoreSyn.flattenBinds [binds]
 flattenLets expr = ([], expr)
 
 -- | Create bunch of nested non-recursive let expressions from the given