2 % (c) The GRASP/AQUA Project, Glasgow University, 1992-1998
4 \section[CoreSyn]{A data type for the Haskell compiler midsection}
8 Expr(..), Alt, Bind(..), AltCon(..), Arg, Note(..),
9 CoreExpr, CoreAlt, CoreBind, CoreArg, CoreBndr,
10 TaggedExpr, TaggedAlt, TaggedBind, TaggedArg, TaggedBndr(..),
13 mkApps, mkTyApps, mkValApps, mkVarApps,
14 mkLit, mkIntLitInt, mkIntLit,
18 isTyVar, isId, cmpAltCon, cmpAlt, ltAlt,
19 bindersOf, bindersOfBinds, rhssOfBind, rhssOfAlts,
20 collectBinders, collectTyBinders, collectValBinders, collectTyAndValBinders,
25 isValArg, isTypeArg, valArgCount, valBndrCount, isRuntimeArg, isRuntimeVar,
28 Unfolding(..), UnfoldingGuidance(..), -- Both abstract everywhere but in CoreUnfold.lhs
29 noUnfolding, evaldUnfolding, mkOtherCon,
30 unfoldingTemplate, maybeUnfoldingTemplate, otherCons,
31 isValueUnfolding, isEvaldUnfolding, isCheapUnfolding, isCompulsoryUnfolding,
32 hasUnfolding, hasSomeUnfolding, neverUnfold,
35 seqRules, seqExpr, seqExprs, seqUnfolding,
37 -- Annotated expressions
38 AnnExpr, AnnExpr'(..), AnnBind(..), AnnAlt,
39 deAnnotate, deAnnotate', deAnnAlt, collectAnnBndrs,
42 CoreRules(..), -- Representation needed by friends
43 CoreRule(..), -- CoreSubst, CoreTidy, CoreFVs, PprCore only
44 IdCoreRule(..), isOrphanRule,
46 emptyCoreRules, isEmptyCoreRules, rulesRhsFreeVars, rulesRules,
47 isBuiltinRule, ruleName
50 #include "HsVersions.h"
52 import CmdLineOpts ( opt_RuntimeTypes )
53 import CostCentre ( CostCentre, noCostCentre )
54 import Var ( Var, Id, TyVar, isTyVar, isId )
55 import Type ( Type, mkTyVarTy, seqType )
56 import Literal ( Literal, mkMachInt )
57 import DataCon ( DataCon, dataConWorkId, dataConTag )
58 import BasicTypes ( Activation )
64 %************************************************************************
66 \subsection{The main data types}
68 %************************************************************************
70 These data types are the heart of the compiler
73 infixl 8 `App` -- App brackets to the left
75 data Expr b -- "b" for the type of binders,
78 | App (Expr b) (Arg b)
80 | Let (Bind b) (Expr b)
81 | Case (Expr b) b Type [Alt b] -- Binder gets bound to value of scrutinee
82 -- Invariant: The list of alternatives is ALWAYS EXHAUSTIVE,
83 -- meaning that it covers all cases that can occur
84 -- See the example below
86 -- Invariant: The DEFAULT case must be *first*, if it occurs at all
87 -- Invariant: The remaining cases are in order of increasing
90 -- This makes finding the relevant constructor easy,
91 -- and makes comparison easier too
93 | Type Type -- This should only show up at the top
96 -- An "exhausive" case does not necessarily mention all constructors:
97 -- data Foo = Red | Green | Blue
101 -- other -> f (case x of
104 -- The inner case does not need a Red alternative, because x can't be Red at
105 -- that program point.
108 type Arg b = Expr b -- Can be a Type
110 type Alt b = (AltCon, [b], Expr b) -- (DEFAULT, [], rhs) is the default alternative
112 data AltCon = DataAlt DataCon
118 data Bind b = NonRec b (Expr b)
119 | Rec [(b, (Expr b))]
125 Type -- The to-type: type of whole coerce expression
126 Type -- The from-type: type of enclosed expression
128 | InlineCall -- Instructs simplifier to inline
131 | InlineMe -- Instructs simplifer to treat the enclosed expression
132 -- as very small, and inline it at its call sites
134 | CoreNote String -- A generic core annotation, propagated but not used by GHC
136 -- NOTE: we also treat expressions wrapped in InlineMe as
137 -- 'cheap' and 'dupable' (in the sense of exprIsCheap, exprIsDupable)
138 -- What this means is that we obediently inline even things that don't
139 -- look like valuse. This is sometimes important:
142 -- Here, f looks like a redex, and we aren't going to inline (.) because it's
143 -- inside an INLINE, so it'll stay looking like a redex. Nevertheless, we
144 -- should inline f even inside lambdas. In effect, we should trust the programmer.
149 * The RHS of a letrec, and the RHSs of all top-level lets,
150 must be of LIFTED type.
152 * The RHS of a let, may be of UNLIFTED type, but only if the expression
153 is ok-for-speculation. This means that the let can be floated around
154 without difficulty. e.g.
156 y::Int# = fac 4# not ok [use case instead]
158 * The argument of an App can be of any type.
160 * The simplifier tries to ensure that if the RHS of a let is a constructor
161 application, its arguments are trivial, so that the constructor can be
165 %************************************************************************
167 \subsection{Transformation rules}
169 %************************************************************************
171 The CoreRule type and its friends are dealt with mainly in CoreRules,
172 but CoreFVs, Subst, PprCore, CoreTidy also inspect the representation.
177 VarSet -- Locally-defined free vars of RHSs
179 emptyCoreRules :: CoreRules
180 emptyCoreRules = Rules [] emptyVarSet
182 isEmptyCoreRules :: CoreRules -> Bool
183 isEmptyCoreRules (Rules rs _) = null rs
185 rulesRhsFreeVars :: CoreRules -> VarSet
186 rulesRhsFreeVars (Rules _ fvs) = fvs
188 rulesRules :: CoreRules -> [CoreRule]
189 rulesRules (Rules rules _) = rules
193 type RuleName = FastString
194 data IdCoreRule = IdCoreRule Id -- A rule for this Id
195 Bool -- True <=> orphan rule
196 CoreRule -- The rule itself
198 isOrphanRule :: IdCoreRule -> Bool
199 isOrphanRule (IdCoreRule _ is_orphan _) = is_orphan
203 Activation -- When the rule is active
204 [CoreBndr] -- Forall'd variables
205 [CoreExpr] -- LHS args
208 | BuiltinRule -- Built-in rules are used for constant folding
209 RuleName -- and suchlike. It has no free variables.
210 ([CoreExpr] -> Maybe CoreExpr)
212 isBuiltinRule (BuiltinRule _ _) = True
213 isBuiltinRule _ = False
215 ruleName :: CoreRule -> RuleName
216 ruleName (Rule n _ _ _ _) = n
217 ruleName (BuiltinRule n _) = n
221 %************************************************************************
223 \subsection{@Unfolding@ type}
225 %************************************************************************
227 The @Unfolding@ type is declared here to avoid numerous loops, but it
228 should be abstract everywhere except in CoreUnfold.lhs
234 | OtherCon [AltCon] -- It ain't one of these
235 -- (OtherCon xs) also indicates that something has been evaluated
236 -- and hence there's no point in re-evaluating it.
237 -- OtherCon [] is used even for non-data-type values
238 -- to indicated evaluated-ness. Notably:
239 -- data C = C !(Int -> Int)
240 -- case x of { C f -> ... }
241 -- Here, f gets an OtherCon [] unfolding.
243 | CompulsoryUnfolding CoreExpr -- There is no "original" definition,
244 -- so you'd better unfold.
246 | CoreUnfolding -- An unfolding with redundant cached information
247 CoreExpr -- Template; binder-info is correct
248 Bool -- True <=> top level binding
249 Bool -- exprIsValue template (cached); it is ok to discard a `seq` on
251 Bool -- True <=> doesn't waste (much) work to expand inside an inlining
252 -- Basically it's exprIsCheap
253 UnfoldingGuidance -- Tells about the *size* of the template.
256 data UnfoldingGuidance
258 | UnfoldIfGoodArgs Int -- and "n" value args
260 [Int] -- Discount if the argument is evaluated.
261 -- (i.e., a simplification will definitely
262 -- be possible). One elt of the list per *value* arg.
264 Int -- The "size" of the unfolding; to be elaborated
267 Int -- Scrutinee discount: the discount to substract if the thing is in
268 -- a context (case (thing args) of ...),
269 -- (where there are the right number of arguments.)
271 noUnfolding = NoUnfolding
272 evaldUnfolding = OtherCon []
274 mkOtherCon = OtherCon
276 seqUnfolding :: Unfolding -> ()
277 seqUnfolding (CoreUnfolding e top b1 b2 g)
278 = seqExpr e `seq` top `seq` b1 `seq` b2 `seq` seqGuidance g
279 seqUnfolding other = ()
281 seqGuidance (UnfoldIfGoodArgs n ns a b) = n `seq` sum ns `seq` a `seq` b `seq` ()
282 seqGuidance other = ()
286 unfoldingTemplate :: Unfolding -> CoreExpr
287 unfoldingTemplate (CoreUnfolding expr _ _ _ _) = expr
288 unfoldingTemplate (CompulsoryUnfolding expr) = expr
289 unfoldingTemplate other = panic "getUnfoldingTemplate"
291 maybeUnfoldingTemplate :: Unfolding -> Maybe CoreExpr
292 maybeUnfoldingTemplate (CoreUnfolding expr _ _ _ _) = Just expr
293 maybeUnfoldingTemplate (CompulsoryUnfolding expr) = Just expr
294 maybeUnfoldingTemplate other = Nothing
296 otherCons :: Unfolding -> [AltCon]
297 otherCons (OtherCon cons) = cons
300 isValueUnfolding :: Unfolding -> Bool
301 -- Returns False for OtherCon
302 isValueUnfolding (CoreUnfolding _ _ is_evald _ _) = is_evald
303 isValueUnfolding other = False
305 isEvaldUnfolding :: Unfolding -> Bool
306 -- Returns True for OtherCon
307 isEvaldUnfolding (OtherCon _) = True
308 isEvaldUnfolding (CoreUnfolding _ _ is_evald _ _) = is_evald
309 isEvaldUnfolding other = False
311 isCheapUnfolding :: Unfolding -> Bool
312 isCheapUnfolding (CoreUnfolding _ _ _ is_cheap _) = is_cheap
313 isCheapUnfolding other = False
315 isCompulsoryUnfolding :: Unfolding -> Bool
316 isCompulsoryUnfolding (CompulsoryUnfolding _) = True
317 isCompulsoryUnfolding other = False
319 hasUnfolding :: Unfolding -> Bool
320 hasUnfolding (CoreUnfolding _ _ _ _ _) = True
321 hasUnfolding (CompulsoryUnfolding _) = True
322 hasUnfolding other = False
324 hasSomeUnfolding :: Unfolding -> Bool
325 hasSomeUnfolding NoUnfolding = False
326 hasSomeUnfolding other = True
328 neverUnfold :: Unfolding -> Bool
329 neverUnfold NoUnfolding = True
330 neverUnfold (OtherCon _) = True
331 neverUnfold (CoreUnfolding _ _ _ _ UnfoldNever) = True
332 neverUnfold other = False
336 %************************************************************************
338 \subsection{The main data type}
340 %************************************************************************
343 -- The Ord is needed for the FiniteMap used in the lookForConstructor
344 -- in SimplEnv. If you declared that lookForConstructor *ignores*
345 -- constructor-applications with LitArg args, then you could get
348 instance Outputable AltCon where
349 ppr (DataAlt dc) = ppr dc
350 ppr (LitAlt lit) = ppr lit
351 ppr DEFAULT = ptext SLIT("__DEFAULT")
353 instance Show AltCon where
354 showsPrec p con = showsPrecSDoc p (ppr con)
356 cmpAlt :: Alt b -> Alt b -> Ordering
357 cmpAlt (con1, _, _) (con2, _, _) = con1 `cmpAltCon` con2
359 ltAlt :: Alt b -> Alt b -> Bool
360 ltAlt a1 a2 = case a1 `cmpAlt` a2 of { LT -> True; other -> False }
362 cmpAltCon :: AltCon -> AltCon -> Ordering
363 -- Compares AltCons within a single list of alternatives
364 cmpAltCon DEFAULT DEFAULT = EQ
365 cmpAltCon DEFAULT con = LT
367 cmpAltCon (DataAlt d1) (DataAlt d2) = dataConTag d1 `compare` dataConTag d2
368 cmpAltCon (DataAlt _) DEFAULT = GT
369 cmpAltCon (LitAlt l1) (LitAlt l2) = l1 `compare` l2
370 cmpAltCon (LitAlt _) DEFAULT = GT
372 cmpAltCon con1 con2 = WARN( True, text "Comparing incomparable AltCons" <+>
373 ppr con1 <+> ppr con2 )
378 %************************************************************************
380 \subsection{Useful synonyms}
382 %************************************************************************
388 type CoreExpr = Expr CoreBndr
389 type CoreArg = Arg CoreBndr
390 type CoreBind = Bind CoreBndr
391 type CoreAlt = Alt CoreBndr
394 Binders are ``tagged'' with a \tr{t}:
397 data TaggedBndr t = TB CoreBndr t -- TB for "tagged binder"
399 type TaggedBind t = Bind (TaggedBndr t)
400 type TaggedExpr t = Expr (TaggedBndr t)
401 type TaggedArg t = Arg (TaggedBndr t)
402 type TaggedAlt t = Alt (TaggedBndr t)
404 instance Outputable b => Outputable (TaggedBndr b) where
405 ppr (TB b l) = char '<' <> ppr b <> comma <> ppr l <> char '>'
407 instance Outputable b => OutputableBndr (TaggedBndr b) where
408 pprBndr _ b = ppr b -- Simple
412 %************************************************************************
414 \subsection{Core-constructing functions with checking}
416 %************************************************************************
419 mkApps :: Expr b -> [Arg b] -> Expr b
420 mkTyApps :: Expr b -> [Type] -> Expr b
421 mkValApps :: Expr b -> [Expr b] -> Expr b
422 mkVarApps :: Expr b -> [Var] -> Expr b
424 mkApps f args = foldl App f args
425 mkTyApps f args = foldl (\ e a -> App e (Type a)) f args
426 mkValApps f args = foldl (\ e a -> App e a) f args
427 mkVarApps f vars = foldl (\ e a -> App e (varToCoreExpr a)) f vars
429 mkLit :: Literal -> Expr b
430 mkIntLit :: Integer -> Expr b
431 mkIntLitInt :: Int -> Expr b
432 mkConApp :: DataCon -> [Arg b] -> Expr b
433 mkLets :: [Bind b] -> Expr b -> Expr b
434 mkLams :: [b] -> Expr b -> Expr b
437 mkConApp con args = mkApps (Var (dataConWorkId con)) args
439 mkLams binders body = foldr Lam body binders
440 mkLets binds body = foldr Let body binds
442 mkIntLit n = Lit (mkMachInt n)
443 mkIntLitInt n = Lit (mkMachInt (toInteger n))
445 varToCoreExpr :: CoreBndr -> Expr b
446 varToCoreExpr v | isId v = Var v
447 | otherwise = Type (mkTyVarTy v)
451 %************************************************************************
453 \subsection{Simple access functions}
455 %************************************************************************
458 bindersOf :: Bind b -> [b]
459 bindersOf (NonRec binder _) = [binder]
460 bindersOf (Rec pairs) = [binder | (binder, _) <- pairs]
462 bindersOfBinds :: [Bind b] -> [b]
463 bindersOfBinds binds = foldr ((++) . bindersOf) [] binds
465 rhssOfBind :: Bind b -> [Expr b]
466 rhssOfBind (NonRec _ rhs) = [rhs]
467 rhssOfBind (Rec pairs) = [rhs | (_,rhs) <- pairs]
469 rhssOfAlts :: [Alt b] -> [Expr b]
470 rhssOfAlts alts = [e | (_,_,e) <- alts]
472 flattenBinds :: [Bind b] -> [(b, Expr b)] -- Get all the lhs/rhs pairs
473 flattenBinds (NonRec b r : binds) = (b,r) : flattenBinds binds
474 flattenBinds (Rec prs1 : binds) = prs1 ++ flattenBinds binds
478 We often want to strip off leading lambdas before getting down to
479 business. @collectBinders@ is your friend.
481 We expect (by convention) type-, and value- lambdas in that
485 collectBinders :: Expr b -> ([b], Expr b)
486 collectTyBinders :: CoreExpr -> ([TyVar], CoreExpr)
487 collectValBinders :: CoreExpr -> ([Id], CoreExpr)
488 collectTyAndValBinders :: CoreExpr -> ([TyVar], [Id], CoreExpr)
493 go bs (Lam b e) = go (b:bs) e
494 go bs e = (reverse bs, e)
496 collectTyAndValBinders expr
499 (tvs, body1) = collectTyBinders expr
500 (ids, body) = collectValBinders body1
502 collectTyBinders expr
505 go tvs (Lam b e) | isTyVar b = go (b:tvs) e
506 go tvs e = (reverse tvs, e)
508 collectValBinders expr
511 go ids (Lam b e) | isId b = go (b:ids) e
512 go ids body = (reverse ids, body)
516 @collectArgs@ takes an application expression, returning the function
517 and the arguments to which it is applied.
520 collectArgs :: Expr b -> (Expr b, [Arg b])
524 go (App f a) as = go f (a:as)
528 coreExprCc gets the cost centre enclosing an expression, if any.
529 It looks inside lambdas because (scc "foo" \x.e) = \x.scc "foo" e
532 coreExprCc :: Expr b -> CostCentre
533 coreExprCc (Note (SCC cc) e) = cc
534 coreExprCc (Note other_note e) = coreExprCc e
535 coreExprCc (Lam _ e) = coreExprCc e
536 coreExprCc other = noCostCentre
541 %************************************************************************
543 \subsection{Predicates}
545 %************************************************************************
547 @isRuntimeVar v@ returns if (Lam v _) really becomes a lambda at runtime,
548 i.e. if type applications are actual lambdas because types are kept around
551 Similarly isRuntimeArg.
554 isRuntimeVar :: Var -> Bool
555 isRuntimeVar | opt_RuntimeTypes = \v -> True
556 | otherwise = \v -> isId v
558 isRuntimeArg :: CoreExpr -> Bool
559 isRuntimeArg | opt_RuntimeTypes = \e -> True
560 | otherwise = \e -> isValArg e
564 isValArg (Type _) = False
565 isValArg other = True
567 isTypeArg (Type _) = True
568 isTypeArg other = False
570 valBndrCount :: [CoreBndr] -> Int
572 valBndrCount (b : bs) | isId b = 1 + valBndrCount bs
573 | otherwise = valBndrCount bs
575 valArgCount :: [Arg b] -> Int
577 valArgCount (Type _ : args) = valArgCount args
578 valArgCount (other : args) = 1 + valArgCount args
582 %************************************************************************
584 \subsection{Seq stuff}
586 %************************************************************************
589 seqExpr :: CoreExpr -> ()
590 seqExpr (Var v) = v `seq` ()
591 seqExpr (Lit lit) = lit `seq` ()
592 seqExpr (App f a) = seqExpr f `seq` seqExpr a
593 seqExpr (Lam b e) = seqBndr b `seq` seqExpr e
594 seqExpr (Let b e) = seqBind b `seq` seqExpr e
596 seqExpr (Case e b t as) = seqExpr e `seq` seqBndr b `seq` seqType t `seq` seqAlts as
597 seqExpr (Note n e) = seqNote n `seq` seqExpr e
598 seqExpr (Type t) = seqType t
601 seqExprs (e:es) = seqExpr e `seq` seqExprs es
603 seqNote (Coerce t1 t2) = seqType t1 `seq` seqType t2
604 seqNote (CoreNote s) = s `seq` ()
607 seqBndr b = b `seq` ()
610 seqBndrs (b:bs) = seqBndr b `seq` seqBndrs bs
612 seqBind (NonRec b e) = seqBndr b `seq` seqExpr e
613 seqBind (Rec prs) = seqPairs prs
616 seqPairs ((b,e):prs) = seqBndr b `seq` seqExpr e `seq` seqPairs prs
619 seqAlts ((c,bs,e):alts) = seqBndrs bs `seq` seqExpr e `seq` seqAlts alts
621 seqRules :: CoreRules -> ()
622 seqRules (Rules rules fvs) = seq_rules rules `seq` seqVarSet fvs
625 seq_rules (Rule fs _ bs es e : rules) = seqBndrs bs `seq` seqExprs (e:es) `seq` seq_rules rules
626 seq_rules (BuiltinRule _ _ : rules) = seq_rules rules
631 %************************************************************************
633 \subsection{Annotated core; annotation at every node in the tree}
635 %************************************************************************
638 type AnnExpr bndr annot = (annot, AnnExpr' bndr annot)
640 data AnnExpr' bndr annot
643 | AnnLam bndr (AnnExpr bndr annot)
644 | AnnApp (AnnExpr bndr annot) (AnnExpr bndr annot)
646 | AnnCase (AnnExpr bndr annot) bndr Type [AnnAlt bndr annot]
647 | AnnLet (AnnBind bndr annot) (AnnExpr bndr annot)
648 | AnnNote Note (AnnExpr bndr annot)
651 type AnnAlt bndr annot = (AltCon, [bndr], AnnExpr bndr annot)
653 data AnnBind bndr annot
654 = AnnNonRec bndr (AnnExpr bndr annot)
655 | AnnRec [(bndr, AnnExpr bndr annot)]
659 deAnnotate :: AnnExpr bndr annot -> Expr bndr
660 deAnnotate (_, e) = deAnnotate' e
662 deAnnotate' (AnnType t) = Type t
663 deAnnotate' (AnnVar v) = Var v
664 deAnnotate' (AnnLit lit) = Lit lit
665 deAnnotate' (AnnLam binder body) = Lam binder (deAnnotate body)
666 deAnnotate' (AnnApp fun arg) = App (deAnnotate fun) (deAnnotate arg)
667 deAnnotate' (AnnNote note body) = Note note (deAnnotate body)
669 deAnnotate' (AnnLet bind body)
670 = Let (deAnnBind bind) (deAnnotate body)
672 deAnnBind (AnnNonRec var rhs) = NonRec var (deAnnotate rhs)
673 deAnnBind (AnnRec pairs) = Rec [(v,deAnnotate rhs) | (v,rhs) <- pairs]
676 deAnnotate' (AnnCase scrut v t alts)
677 = Case (deAnnotate scrut) v t (map deAnnAlt alts)
679 deAnnAlt :: AnnAlt bndr annot -> Alt bndr
680 deAnnAlt (con,args,rhs) = (con,args,deAnnotate rhs)
684 collectAnnBndrs :: AnnExpr bndr annot -> ([bndr], AnnExpr bndr annot)
688 collect bs (_, AnnLam b body) = collect (b:bs) body
689 collect bs body = (reverse bs, body)