9135e87ee3cbca992d34f2969e9be50d0e48ca06
[ghc-hetmet.git] / ghc / compiler / stranal / SaLib.lhs
1 %
2 % (c) The GRASP/AQUA Project, Glasgow University, 1993-1998
3 %
4 \section[SaLib]{Basic datatypes, functions for the strictness analyser}
5
6 See also: the ``library'' for the ``back end'' (@SaBackLib@).
7
8 \begin{code}
9 module SaLib (
10         AbsVal(..),
11         AnalysisKind(..),
12         AbsValEnv{-abstract-}, StrictEnv, AbsenceEnv,
13         mkAbsApproxFun,
14         nullAbsValEnv, addOneToAbsValEnv, growAbsValEnvList,
15         lookupAbsValEnv,
16         absValFromStrictness
17     ) where
18
19 #include "HsVersions.h"
20
21 import Id               ( Id )
22 import CoreSyn          ( CoreExpr )
23 import VarEnv
24 import IdInfo           ( StrictnessInfo(..) )
25 import Demand           ( Demand, pprDemands )
26 import Outputable
27 \end{code}
28
29 %************************************************************************
30 %*                                                                      *
31 \subsection[AbsVal-datatype]{@AbsVal@: abstract values (and @AbsValEnv@)}
32 %*                                                                      *
33 %************************************************************************
34
35 @AnalysisKind@ tells what kind of analysis is being done.
36
37 \begin{code}
38 data AnalysisKind
39   = StrAnal     -- We're doing strictness analysis
40   | AbsAnal     -- We're doing absence analysis
41   deriving Text
42 \end{code}
43
44 @AbsVal@ is the data type of HNF abstract values.
45
46 \begin{code}
47 data AbsVal
48   = AbsTop                  -- AbsTop is the completely uninformative
49                             -- value
50
51   | AbsBot                  -- An expression whose abstract value is
52                             -- AbsBot is sure to fail to terminate.
53                             -- AbsBot represents the abstract
54                             -- *function* bottom too.
55
56   | AbsProd [AbsVal]        -- (Lifted) product of abstract values
57                             -- "Lifted" means that AbsBot is *different* from
58                             --    AbsProd [AbsBot, ..., AbsBot]
59
60   | AbsFun                  -- An abstract function, with the given:
61             Id              -- argument
62             CoreExpr        -- body
63             AbsValEnv       -- and environment
64
65   | AbsApproxFun            -- This is used to represent a coarse
66             [Demand]        -- approximation to a function value.  It's an
67             AbsVal          -- abstract function which is strict in its
68                             -- arguments if the  Demand so indicates.
69         -- INVARIANT: the [Demand] is non-empty
70
71         -- AbsApproxFun has to take a *list* of demands, no just one,
72         -- because function spaces are now lifted.  Hence, (f bot top)
73         -- might be bot, but the partial application (f bot) is a *function*,
74         -- not bot.
75
76 mkAbsApproxFun :: Demand -> AbsVal -> AbsVal
77 mkAbsApproxFun d (AbsApproxFun ds val) = AbsApproxFun (d:ds) val
78 mkAbsApproxFun d val                   = AbsApproxFun [d]    val
79
80 instance Outputable AbsVal where
81     ppr AbsTop = ptext SLIT("AbsTop")
82     ppr AbsBot = ptext SLIT("AbsBot")
83     ppr (AbsProd prod) = hsep [ptext SLIT("AbsProd"), ppr prod]
84     ppr (AbsFun arg body env)
85       = hsep [ptext SLIT("AbsFun{"), ppr arg,
86                ptext SLIT("???"), -- text "}{env:", ppr (keysFM env `zip` eltsFM env),
87                char '}' ]
88     ppr (AbsApproxFun demands val)
89       = hsep [ptext SLIT("AbsApprox "), hcat (map ppr demands), ppr val]
90 \end{code}
91
92 %-----------
93
94 An @AbsValEnv@ maps @Ids@ to @AbsVals@.  Any unbound @Ids@ are
95 implicitly bound to @AbsTop@, the completely uninformative,
96 pessimistic value---see @absEval@ of a @Var@.
97
98 \begin{code}
99 newtype AbsValEnv = AbsValEnv (IdEnv AbsVal)
100
101 type StrictEnv  = AbsValEnv     -- Environment for strictness analysis
102 type AbsenceEnv = AbsValEnv     -- Environment for absence analysis
103
104 nullAbsValEnv -- this is the one and only way to create AbsValEnvs
105   = AbsValEnv emptyVarEnv
106
107 addOneToAbsValEnv (AbsValEnv idenv) y z = AbsValEnv (extendVarEnv idenv y z)
108 growAbsValEnvList (AbsValEnv idenv) ys  = AbsValEnv (extendVarEnvList idenv ys)
109
110 lookupAbsValEnv (AbsValEnv idenv) y
111   = lookupVarEnv idenv y
112 \end{code}
113
114 \begin{code}
115 absValFromStrictness :: AnalysisKind -> StrictnessInfo -> AbsVal
116
117 absValFromStrictness anal NoStrictnessInfo = AbsTop
118 absValFromStrictness anal (StrictnessInfo args_info bot_result _)
119   = case args_info of   -- Check the invariant that the arg list on 
120         [] -> res       -- AbsApproxFun is non-empty
121         _  -> AbsApproxFun args_info res
122   where
123     res | not bot_result = AbsTop
124         | otherwise      = case anal of
125                                 StrAnal -> AbsBot
126                                 AbsAnal -> AbsTop
127 \end{code}