-specialise :: ScEnv
- -> Id -- Functionn
- -> [CoreBndr] -> CoreExpr -- Its RHS
- -> ScUsage -- Info on usage
- -> UniqSM ([CoreRule], -- Rules
- [(Id,CoreExpr)]) -- Bindings
+specialise
+ :: ScEnv
+ -> IdEnv [Call] -- Info on usage
+ -> (Id, CoreExpr, [ArgOcc]) -- Original binding, plus info on how the rhs's
+ -- lambda-binders are used (includes TyVar bndrs)
+ -> UniqSM [(Id,CoreExpr)] -- Original binding (decorated with rules)
+ -- plus specialised bindings
+
+-- Note: the rhs here is the optimised version of the original rhs
+-- So when we make a specialised copy of the RHS, we're starting
+-- from an RHS whose nested functions have been optimised already.
+
+specialise env calls (fn, rhs, arg_occs)
+ | notNull arg_occs, -- Only specialise functions
+ Just all_calls <- lookupVarEnv calls fn
+ = do { mb_pats <- mapM (callToPats (scope env) arg_occs) all_calls
+
+ ; let good_pats :: [([Var], [CoreArg])]
+ good_pats = catMaybes mb_pats
+ in_scope = mkInScopeSet $ unionVarSets $
+ [ exprsFreeVars pats `delVarSetList` vs
+ | (vs,pats) <- good_pats ]
+ uniq_pats = nubBy (same_pat in_scope) good_pats
+-- ; pprTrace "specialise" (vcat [ppr fn <+> ppr arg_occs,
+-- text "calls" <+> ppr all_calls,
+-- text "good pats" <+> ppr good_pats,
+-- text "uniq pats" <+> ppr uniq_pats]) $
+-- return ()