Let inlinetoplevel use getNormalized_maybe.
[matthijs/master-project/cλash.git] / cλash / CLasH / Normalize.hs
index fa14ef65ef7dec1b818e73f86c3da4fdb5842028..91033b5b1b9bc84c928fd86129362c3d1e5b2f71 100644 (file)
@@ -338,23 +338,19 @@ inlinetoplevel, inlinetopleveltop :: Transform
 -- Any system name is candidate for inlining. Never inline user-defined
 -- functions, to preserve structure.
 inlinetoplevel expr@(Var f) | not $ isUserDefined f = do
-  norm <- isNormalizeable f
-  -- See if this is a top level binding for which we have a body
-  body_maybe <- Trans.lift $ getGlobalBind f
-  if norm && Maybe.isJust body_maybe
-    then do
-      -- Get the normalized version
-      norm <- Trans.lift $ getNormalized f
-      if needsInline norm 
-        then do
-          -- Regenerate all uniques in the to-be-inlined expression
-          norm_uniqued <- Trans.lift $ genUniques norm
-          change norm_uniqued
-        else
-          return expr
-    else
+  norm_maybe <- Trans.lift $ getNormalized_maybe f
+  case norm_maybe of
       -- No body or not normalizeable.
-      return expr
+    Nothing -> return expr
+    Just norm -> if needsInline norm then do
+        -- Regenerate all uniques in the to-be-inlined expression
+        norm_uniqued <- Trans.lift $ genUniques norm
+        -- And replace the variable reference with the unique'd body.
+        change norm_uniqued
+      else
+        -- No need to inline
+        return expr
+
 -- Leave all other expressions unchanged
 inlinetoplevel expr = return expr
 inlinetopleveltop = everywhere ("inlinetoplevel", inlinetoplevel)
@@ -734,20 +730,21 @@ getNormalized ::
 getNormalized bndr = do
   norm <- getNormalized_maybe bndr
   return $ Maybe.fromMaybe
-    (error $ "Normalize.getNormalized: Unknown function requested: " ++ show bndr)
+    (error $ "Normalize.getNormalized: Unknown or non-representable function requested: " ++ show bndr)
     norm
 
 -- | Returns the normalized version of the given function, or Nothing
--- when the binder is not a known global binder.
+-- when the binder is not a known global binder or is not normalizeable.
 getNormalized_maybe ::
   CoreBndr -- ^ The function to get
   -> TranslatorSession (Maybe CoreExpr) -- The normalized function body
 
 getNormalized_maybe bndr = do
     expr_maybe <- getGlobalBind bndr
-    if Maybe.isNothing expr_maybe
+    normalizeable <- isNormalizeable' bndr
+    if not normalizeable || Maybe.isNothing expr_maybe
       then
-        -- Binder not found
+        -- Binder not normalizeable or not found
         return Nothing
       else if is_poly (Var bndr)
         then