Improve hashing of expressions for CSE (reduces warnings about extendCSEnv)
authorsimonpj@microsoft.com <unknown>
Wed, 25 Apr 2007 14:15:30 +0000 (14:15 +0000)
committersimonpj@microsoft.com <unknown>
Wed, 25 Apr 2007 14:15:30 +0000 (14:15 +0000)
compiler/basicTypes/Name.lhs
compiler/coreSyn/CoreUtils.lhs
compiler/simplCore/CSE.lhs

index 75198bb..883668b 100644 (file)
@@ -260,8 +260,11 @@ localiseName n = n { n_sort = Internal }
 %************************************************************************
 
 \begin{code}
-hashName :: Name -> Int
-hashName name = getKey (nameUnique name)
+hashName :: Name -> Int                -- ToDo: should really be Word
+hashName name = getKey (nameUnique name) + 1
+       -- The +1 avoids keys with lots of zeros in the ls bits, which 
+       -- interacts badly with the cheap and cheerful multiplication in
+       -- hashExpr
 \end{code}
 
 
index a43be02..d4d58df 100644 (file)
@@ -1386,10 +1386,10 @@ fast_hash_expr env other        = 1
 
 fast_hash_type :: HashEnv -> Type -> Word32
 fast_hash_type env ty 
-  | Just tv <- getTyVar_maybe ty          = hashVar env tv
-  | Just (tc,_) <- splitTyConApp_maybe ty
-                              = fromIntegral (hashName (tyConName tc))
-  | otherwise                            = 1
+  | Just tv <- getTyVar_maybe ty            = hashVar env tv
+  | Just (tc,tys) <- splitTyConApp_maybe ty = let hash_tc = fromIntegral (hashName (tyConName tc))
+                                             in foldr (\t n -> fast_hash_type env t + n) hash_tc tys
+  | otherwise                              = 1
 
 extend_env :: HashEnv -> Var -> (Int, VarEnv Int)
 extend_env (n,env) b = (n+1, extendVarEnv env b n)
index e7dd217..f8259c7 100644 (file)
@@ -315,7 +315,9 @@ extendCSEnv (CS cs in_scope sub) expr expr'
   = CS (addToUFM_C combine cs hash [(expr, expr')]) in_scope sub
   where
     hash   = hashExpr expr
-    combine old new = WARN( result `lengthExceeds` 4, text "extendCSEnv: long list:" <+> ppr result )
+    combine old new = WARN( result `lengthExceeds` 4, ((text "extendCSEnv: long list (length" <+> int (length result) <> comma 
+                                                       <+> text "hash code" <+> text (show hash) <> char ')')
+                                                       $$ nest 4 (ppr result)) )
                      result
                    where
                      result = new ++ old