[project @ 1996-03-19 08:58:34 by partain]
[ghc-hetmet.git] / ghc / compiler / stranal / SaLib.lhs
1 %
2 % (c) The GRASP/AQUA Project, Glasgow University, 1993-1995
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 #include "HsVersions.h"
10
11 module SaLib (
12         AbsVal(..),
13         AnalysisKind(..),
14         AbsValEnv{-abstract-}, StrictEnv(..), AbsenceEnv(..),
15         StrAnalFlags(..), getStrAnalFlags,
16         nullAbsValEnv, addOneToAbsValEnv, growAbsValEnvList,
17         lookupAbsValEnv,
18         absValFromStrictness
19
20         -- and to make the interface self-sufficient...
21     ) where
22
23 import IdInfo
24 import Outputable
25 import Pretty
26 \end{code}
27
28 %************************************************************************
29 %*                                                                      *
30 \subsection[AbsVal-datatype]{@AbsVal@: abstract values (and @AbsValEnv@)}
31 %*                                                                      *
32 %************************************************************************
33
34 @AnalysisKind@ tells what kind of analysis is being done.
35
36 \begin{code}
37 data AnalysisKind
38   = StrAnal     -- We're doing strictness analysis
39   | AbsAnal     -- We're doing absence analysis
40   deriving Text
41 \end{code}
42
43 @AbsVal@ is the data type of HNF abstract values.
44
45 \begin{code}
46 data AbsVal
47   = AbsTop                  -- AbsTop is the completely uninformative
48                             -- value
49
50   | AbsBot                  -- An expression whose abstract value is
51                             -- AbsBot is sure to fail to terminate.
52                             -- AbsBot represents the abstract
53                             -- *function* bottom too.
54
55   | AbsProd [AbsVal]        -- (Lifted) product of abstract values
56                             -- "Lifted" means that AbsBot is *different* from
57                             --    AbsProd [AbsBot, ..., AbsBot]
58
59   | AbsFun                  -- An abstract function, with the given:
60             [Id]            -- arguments
61             CoreExpr   -- body
62             AbsValEnv       -- and environment
63
64   | AbsApproxFun            -- This is used to represent a coarse
65             [Demand]        -- approximation to a function value.  It's an
66                             -- abstract function which is strict in its i'th
67                             -- argument if the i'th element of the Demand
68                             -- list so indicates.
69                             -- The list of arguments is always non-empty.
70                             -- In effect, AbsApproxFun [] = AbsTop
71
72 instance Outputable AbsVal where
73     ppr sty AbsTop = ppStr "AbsTop"
74     ppr sty AbsBot = ppStr "AbsBot"
75     ppr sty (AbsProd prod) = ppCat [ppStr "AbsProd", ppr sty prod]
76     ppr sty (AbsFun args body env)
77       = ppCat [ppStr "AbsFun{", ppr sty args,
78                ppStr "???", -- ppStr "}{env:", ppr sty (keysFM env `zip` eltsFM env),
79                ppStr "}" ]
80     ppr sty (AbsApproxFun demands)
81       = ppCat [ppStr "AbsApprox{", ppr sty demands, ppStr "}" ]
82 \end{code}
83
84 %-----------
85
86 An @AbsValEnv@ maps @Ids@ to @AbsVals@.  Any unbound @Ids@ are
87 implicitly bound to @AbsTop@, the completely uninformative,
88 pessimistic value---see @absEval@ of a @Var@.
89
90 \begin{code}
91 data AbsValEnv = AbsValEnv StrAnalFlags (IdEnv AbsVal)
92
93 type StrAnalFlags
94   = (Bool,      -- True <=> AllStrict flag is set
95      Bool)      -- True <=> NumbersStrict flag is set
96
97 type StrictEnv  = AbsValEnv     -- Environment for strictness analysis
98 type AbsenceEnv = AbsValEnv     -- Environment for absence analysis
99
100 nullAbsValEnv flags -- this is the one and only way to create AbsValEnvs
101   = AbsValEnv flags nullIdEnv
102
103 addOneToAbsValEnv (AbsValEnv x idenv) y z = AbsValEnv x (addOneToIdEnv idenv y z)
104 growAbsValEnvList (AbsValEnv x idenv) ys  = AbsValEnv x (growIdEnvList idenv ys)
105
106 lookupAbsValEnv (AbsValEnv _ idenv) y
107   = lookupIdEnv idenv y
108
109 getStrAnalFlags (AbsValEnv flags _) = flags
110 \end{code}
111
112 \begin{code}
113 absValFromStrictness :: AnalysisKind -> StrictnessInfo -> AbsVal
114
115 absValFromStrictness anal NoStrictnessInfo             = AbsTop
116
117 absValFromStrictness StrAnal BottomGuaranteed          = AbsBot -- Guaranteed bottom
118 absValFromStrictness AbsAnal BottomGuaranteed          = AbsTop -- Check for poison in
119                                                                 -- arguments (if any)
120 absValFromStrictness anal (StrictnessInfo []        _) = AbsTop
121 absValFromStrictness anal (StrictnessInfo args_info _) = AbsApproxFun args_info
122 \end{code}