+
+ returnDs ((poly_tup_id, poly_tup_expr) : (concat export_binds_s ++ rest))
+
+dsSpec :: [TyVar] -> [DictId] -> [TyVar]
+ -> Id -> Id -- Global, local
+ -> CoreBind -> Prag
+ -> DsM (Maybe ((Id,CoreExpr), -- Binding for specialised Id
+ CoreRule)) -- Rule for the Global Id
+
+-- Example:
+-- f :: (Eq a, Ix b) => a -> b -> b
+-- {-# SPECIALISE f :: Ix b => Int -> b -> b #-}
+--
+-- AbsBinds [ab] [d1,d2] [([ab], f, f_mono, prags)] binds
+--
+-- SpecPrag (/\b.\(d:Ix b). f Int b dInt d)
+-- (forall b. Ix b => Int -> b -> b)
+--
+-- Rule: forall b,(d:Ix b). f Int b dInt d = f_spec b d
+--
+-- Spec bind: f_spec = Let f = /\ab \(d1:Eq a)(d2:Ix b). let binds in f_mono
+-- /\b.\(d:Ix b). in f Int b dInt d
+-- The idea is that f occurs just once, so it'll be
+-- inlined and specialised
+
+dsSpec all_tvs dicts tvs poly_id mono_id mono_bind (InlinePrag {})
+ = return Nothing
+
+dsSpec all_tvs dicts tvs poly_id mono_id mono_bind
+ (SpecPrag spec_expr spec_ty const_dicts inl)
+ = do { let poly_name = idName poly_id
+ ; spec_name <- newLocalName poly_name
+ ; ds_spec_expr <- dsExpr spec_expr
+ ; let (bndrs, body) = collectBinders ds_spec_expr
+ mb_lhs = decomposeRuleLhs (bndrs ++ const_dicts) body
+
+ ; case mb_lhs of
+ Nothing -> do { dsWarn msg; return Nothing }
+
+ Just (bndrs', var, args) -> return (Just (addInlineInfo inl spec_id spec_rhs, rule))
+ where
+ local_poly = setIdNotExported poly_id
+ -- Very important to make the 'f' non-exported,
+ -- else it won't be inlined!
+ spec_id = mkLocalId spec_name spec_ty
+ spec_rhs = Let (NonRec local_poly poly_f_body) ds_spec_expr
+ poly_f_body = mkLams (tvs ++ dicts) $
+ fix_up (Let mono_bind (Var mono_id))
+
+ -- Quantify over constant dicts on the LHS, since
+ -- their value depends only on their type
+ -- The ones we are interested in may even be imported
+ -- e.g. GHC.Base.dEqInt
+
+ rule = mkLocalRule (mkFastString ("SPEC " ++ showSDoc (ppr poly_name)))
+ AlwaysActive poly_name
+ bndrs' -- Includes constant dicts
+ args
+ (mkVarApps (Var spec_id) bndrs)
+ }
+ where
+ -- Bind to voidTy any of all_ptvs that aren't
+ -- relevant for this particular function
+ fix_up body | null void_tvs = body
+ | otherwise = mkTyApps (mkLams void_tvs body)
+ (map (const voidTy) void_tvs)
+ void_tvs = all_tvs \\ tvs
+
+ msg = hang (ptext SLIT("Specialisation too complicated to desugar; ignored"))
+ 2 (ppr spec_expr)
+\end{code}
+
+
+%************************************************************************
+%* *
+\subsection{Adding inline pragmas}
+%* *
+%************************************************************************
+
+\begin{code}
+decomposeRuleLhs :: [Var] -> CoreExpr -> Maybe ([Var], Id, [CoreExpr])
+-- Returns Nothing if the LHS isn't of the expected shape
+-- The argument 'all_bndrs' includes the "constant dicts" of the LHS,
+-- and they may be GlobalIds, which we can't forall-ify.
+-- So we substitute them out instead
+decomposeRuleLhs all_bndrs lhs
+ = go init_env (occurAnalyseExpr lhs) -- Occurrence analysis sorts out the dict
+ -- bindings so we know if they are recursive
+ where
+
+ -- all_bndrs may include top-level imported dicts,
+ -- imported things with a for-all.
+ -- So we localise them and subtitute them out
+ bndr_prs = [ (id, Var (localise id)) | id <- all_bndrs, isGlobalId id ]
+ localise d = mkLocalId (idName d) (idType d)
+
+ init_env = mkVarEnv bndr_prs
+ all_bndrs' = map subst_bndr all_bndrs
+ subst_bndr bndr = case lookupVarEnv init_env bndr of
+ Just (Var bndr') -> bndr'
+ Just other -> panic "decomposeRuleLhs"
+ Nothing -> bndr
+
+ -- Substitute dicts in the LHS args, so that there
+ -- aren't any lets getting in the way
+ -- Note that we substitute the function too; we might have this as
+ -- a LHS: let f71 = M.f Int in f71
+ go env (Let (NonRec dict rhs) body)
+ = go (extendVarEnv env dict (simpleSubst env rhs)) body
+ go env body
+ = case collectArgs (simpleSubst env body) of
+ (Var fn, args) -> Just (all_bndrs', fn, args)
+ other -> Nothing
+
+simpleSubst :: IdEnv CoreExpr -> CoreExpr -> CoreExpr
+-- Similar to CoreSubst.substExpr, except that
+-- (a) takes no account of capture; dictionary bindings use new names
+-- (b) can have a GlobalId (imported) in its domain
+-- (c) Ids only; no types are substituted
+
+simpleSubst subst expr
+ = go expr
+ where
+ go (Var v) = lookupVarEnv subst v `orElse` Var v
+ go (Type ty) = Type ty
+ go (Lit lit) = Lit lit
+ go (App fun arg) = App (go fun) (go arg)
+ go (Note note e) = Note note (go e)
+ go (Lam bndr body) = Lam bndr (go body)
+ go (Let (NonRec bndr rhs) body) = Let (NonRec bndr (go rhs)) (go body)
+ go (Let (Rec pairs) body) = Let (Rec (mapSnd go pairs)) (go body)
+ go (Case scrut bndr ty alts) = Case (go scrut) bndr ty
+ [(c,bs,go r) | (c,bs,r) <- alts]
+
+addLocalInlines exports core_prs
+ = map add_inline core_prs
+ where
+ add_inline (bndr,rhs) | Just inl <- lookupVarEnv inline_env bndr
+ = addInlineInfo inl bndr rhs
+ | otherwise
+ = (bndr,rhs)
+ inline_env = mkVarEnv [(mono_id, prag)
+ | (_, _, mono_id, prags) <- exports,
+ InlinePrag prag <- prags]
+
+addInlineInfo :: InlineSpec -> Id -> CoreExpr -> (Id,CoreExpr)
+addInlineInfo (Inline phase is_inline) bndr rhs
+ = (attach_phase bndr phase, wrap_inline is_inline rhs)
+ where
+ attach_phase bndr phase
+ | isAlwaysActive phase = bndr -- Default phase
+ | otherwise = bndr `setInlinePragma` phase
+
+ wrap_inline True body = mkInlineMe body
+ wrap_inline False body = body