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