[project @ 2003-01-13 17:01:22 by simonpj]
[ghc-hetmet.git] / ghc / compiler / simplCore / CSE.lhs
index 041fdfb..3354bf8 100644 (file)
@@ -10,8 +10,9 @@ module CSE (
 
 #include "HsVersions.h"
 
-import CmdLineOpts     ( DynFlag(..), DynFlags, dopt )
-import Id              ( Id, idType )
+import CmdLineOpts     ( DynFlag(..), DynFlags )
+import Id              ( Id, idType, idWorkerInfo )
+import IdInfo          ( workerExists )
 import CoreUtils       ( hashExpr, cheapEqExpr, exprIsBig, mkAltExpr )
 import DataCon         ( isUnboxedTupleCon )
 import Type            ( tyConAppArgs )
@@ -21,7 +22,7 @@ import CoreSyn
 import VarEnv  
 import CoreLint                ( showPass, endPass )
 import Outputable
-import Util            ( mapAccumL )
+import Util            ( mapAccumL, lengthExceeds )
 import UniqFM
 \end{code}
 
@@ -126,12 +127,23 @@ cseBind env (Rec pairs)  = let (env', pairs') = mapAccumL do_one env pairs
                           in (env', Rec pairs')
                         
 
-do_one env (id, rhs) = case lookupCSEnv env rhs' of
-                         Just other_id -> (extendSubst env' id other_id, (id', Var other_id))
-                         Nothing       -> (addCSEnvItem env' id' rhs',   (id', rhs'))
-                    where
-                       (env', id') = addBinder env id
-                       rhs'        = cseExpr env' rhs
+do_one env (id, rhs) 
+  = case lookupCSEnv env rhs' of
+       Just other_id -> (extendSubst env' id other_id, (id', Var other_id))
+       Nothing       -> (addCSEnvItem env' id' rhs',   (id', rhs'))
+  where
+    (env', id') = addBinder env id
+    rhs' | not (workerExists (idWorkerInfo id)) = cseExpr env' rhs
+
+               -- Hack alert: don't do CSE on wrapper RHSs.
+               -- Otherwise we find:
+               --      $wf = h
+               --      f = \x -> ...$wf...
+               -- ===>
+               --      f = \x -> ...h...
+               -- But the WorkerInfo for f still says $wf, which is now dead!
+         | otherwise = rhs
+
 
 tryForCSE :: CSEnv -> CoreExpr -> CoreExpr
 tryForCSE env (Type t) = Type t
@@ -227,7 +239,7 @@ extendCSEnv (CS cs in_scope sub) id expr
   = CS (addToUFM_C combine cs hash [(id, expr)]) in_scope sub
   where
     hash   = hashExpr expr
-    combine old new = WARN( length result > 4, text "extendCSEnv: long list:" <+> ppr result )
+    combine old new = WARN( result `lengthExceeds` 4, text "extendCSEnv: long list:" <+> ppr result )
                      result
                    where
                      result = new ++ old