813410ce330fb17d92934e34c5f8caf881bcb8ca
[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 Type             ( Type )
23 import CoreSyn          ( CoreExpr )
24 import VarEnv
25 import IdInfo           ( StrictnessInfo(..) )
26 import Demand           ( Demand, pprDemands )
27 import Outputable
28 \end{code}
29
30 %************************************************************************
31 %*                                                                      *
32 \subsection[AbsVal-datatype]{@AbsVal@: abstract values (and @AbsValEnv@)}
33 %*                                                                      *
34 %************************************************************************
35
36 @AnalysisKind@ tells what kind of analysis is being done.
37
38 \begin{code}
39 data AnalysisKind
40   = StrAnal     -- We're doing strictness analysis
41   | AbsAnal     -- We're doing absence analysis
42   deriving Text
43 \end{code}
44
45 @AbsVal@ is the data type of HNF abstract values.
46
47 \begin{code}
48 data AbsVal
49   = AbsTop                  -- AbsTop is the completely uninformative
50                             -- value
51
52   | AbsBot                  -- An expression whose abstract value is
53                             -- AbsBot is sure to fail to terminate.
54                             -- AbsBot represents the abstract
55                             -- *function* bottom too.
56
57   | AbsProd [AbsVal]        -- (Lifted) product of abstract values
58                             -- "Lifted" means that AbsBot is *different* from
59                             --    AbsProd [AbsBot, ..., AbsBot]
60
61   | AbsFun                  -- An abstract function, with the given:
62             Type                 -- Type of the *argument* to the function
63             (AbsVal -> AbsVal)  -- The function
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 bndr_ty body) = ptext SLIT("AbsFun")
85     ppr (AbsApproxFun demands val)
86       = ptext SLIT("AbsApprox") <+> brackets (interpp'SP demands) <+> ppr val
87 \end{code}
88
89 %-----------
90
91 An @AbsValEnv@ maps @Ids@ to @AbsVals@.  Any unbound @Ids@ are
92 implicitly bound to @AbsTop@, the completely uninformative,
93 pessimistic value---see @absEval@ of a @Var@.
94
95 \begin{code}
96 newtype AbsValEnv = AbsValEnv (IdEnv AbsVal)
97
98 type StrictEnv  = AbsValEnv     -- Environment for strictness analysis
99 type AbsenceEnv = AbsValEnv     -- Environment for absence analysis
100
101 nullAbsValEnv -- this is the one and only way to create AbsValEnvs
102   = AbsValEnv emptyVarEnv
103
104 addOneToAbsValEnv (AbsValEnv idenv) y z = AbsValEnv (extendVarEnv idenv y z)
105 growAbsValEnvList (AbsValEnv idenv) ys  = AbsValEnv (extendVarEnvList idenv ys)
106
107 lookupAbsValEnv (AbsValEnv idenv) y
108   = lookupVarEnv idenv y
109 \end{code}
110
111 \begin{code}
112 absValFromStrictness :: AnalysisKind -> StrictnessInfo -> AbsVal
113
114 absValFromStrictness anal NoStrictnessInfo = AbsTop
115 absValFromStrictness anal (StrictnessInfo args_info bot_result)
116   = case args_info of   -- Check the invariant that the arg list on 
117         [] -> res       -- AbsApproxFun is non-empty
118         _  -> AbsApproxFun args_info res
119   where
120     res | not bot_result = AbsTop
121         | otherwise      = case anal of
122                                 StrAnal -> AbsBot
123                                 AbsAnal -> AbsTop
124 \end{code}