[project @ 1998-02-03 17:49:21 by simonm]
[ghc-hetmet.git] / ghc / compiler / simplCore / SimplVar.lhs
1 %
2 % (c) The AQUA Project, Glasgow University, 1993-1996
3 %
4 \section[SimplVar]{Simplifier stuff related to variables}
5
6 \begin{code}
7 module SimplVar (
8         completeVar
9     ) where
10
11 #include "HsVersions.h"
12
13 import {-# SOURCE #-} Simplify ( simplExpr )
14
15 import CmdLineOpts      ( switchIsOn, SimplifierSwitch(..) )
16 import CoreSyn
17 import CoreUnfold       ( Unfolding(..), UnfoldingGuidance(..), 
18                           SimpleUnfolding(..),
19                           FormSummary, whnfOrBottom,
20                           smallEnoughToInline )
21 import BinderInfo       ( BinderInfo, noBinderInfo, okToInline )
22
23 import CostCentre       ( CostCentre, isCurrentCostCentre )
24 import Id               ( idType, getIdInfo, getIdUnfolding, getIdSpecialisation,
25                           idMustBeINLINEd, GenId{-instance Outputable-}
26                         )
27 import SpecEnv          ( matchSpecEnv )
28 import Literal          ( isNoRepLit )
29 import MagicUFs         ( applyMagicUnfoldingFun, MagicUnfoldingFun )
30 import PprType          ( GenType{-instance Outputable-} )
31 import SimplEnv
32 import SimplMonad
33 import TyCon            ( tyConFamilySize )
34 import Maybes           ( maybeToBool )
35 import Outputable
36 \end{code}
37
38 %************************************************************************
39 %*                                                                      *
40 \subsection[Simplify-var]{Completing variables}
41 %*                                                                      *
42 %************************************************************************
43
44 This where all the heavy-duty unfolding stuff comes into its own.
45
46 \begin{code}
47 completeVar env var args result_ty
48
49   | maybeToBool maybe_magic_result
50   = tick MagicUnfold    `thenSmpl_`
51     magic_result
52
53         -- If there's an InUnfolding it means that there's no
54         -- let-binding left for the thing, so we'd better inline it!
55   | must_unfold
56   = let
57         Just (_, _, InUnfolding rhs_env rhs) = info_from_env
58     in
59     unfold var rhs_env rhs args result_ty
60
61
62         -- Conditional unfolding. There's a binding for the
63         -- thing, but perhaps we want to inline it anyway
64   | (  maybeToBool maybe_unfolding_info
65     && (not essential_unfoldings_only || idMustBeINLINEd var)
66         -- If "essential_unfoldings_only" is true we do no inlinings at all,
67         -- EXCEPT for things that absolutely have to be done
68         -- (see comments with idMustBeINLINEd)
69     && ok_to_inline
70     && costCentreOk (getEnclosingCC env) (getEnclosingCC unf_env)
71     )
72   = unfold var unf_env unf_template args result_ty
73
74
75   | maybeToBool maybe_specialisation
76   = tick SpecialisationDone     `thenSmpl_`
77     simplExpr (extendTyEnvEnv env spec_bindings) 
78               spec_template
79               remaining_args
80               result_ty
81
82   | otherwise
83   = returnSmpl (mkGenApp (Var var) args)
84
85   where
86     info_from_env     = lookupOutIdEnv env var
87     unfolding_from_id = getIdUnfolding var
88
89         ---------- Magic unfolding stuff
90     maybe_magic_result  = case unfolding_from_id of
91                                 MagicUnfolding _ magic_fn -> applyMagicUnfoldingFun magic_fn 
92                                                                                     env args
93                                 other                     -> Nothing
94     (Just magic_result) = maybe_magic_result
95
96         ---------- Unfolding stuff
97     must_unfold = case info_from_env of
98                         Just (_, _, InUnfolding _ _) -> True
99                         other                        -> False
100
101     maybe_unfolding_info 
102         = case (info_from_env, unfolding_from_id) of
103
104              (Just (_, occ_info, OutUnfolding enc_cc unf), _)
105                 -> Just (occ_info, setEnclosingCC env enc_cc, unf)      
106
107              (_, CoreUnfolding unf)
108                 -> Just (noBinderInfo, env, unf)
109
110              other -> Nothing
111
112     Just (occ_info, unf_env, simple_unfolding) = maybe_unfolding_info
113     SimpleUnfolding form guidance unf_template = simple_unfolding
114
115         ---------- Specialisation stuff
116     (ty_args, remaining_args) = initialTyArgs args
117     maybe_specialisation = matchSpecEnv (getIdSpecialisation var) ty_args
118     Just (spec_bindings, spec_template) = maybe_specialisation
119
120
121         ---------- Switches
122     sw_chkr                   = getSwitchChecker env
123     essential_unfoldings_only = switchIsOn sw_chkr EssentialUnfoldingsOnly
124     is_case_scrutinee         = switchIsOn sw_chkr SimplCaseScrutinee
125     ok_to_inline              = okToInline (whnfOrBottom form) small_enough occ_info 
126     small_enough              = smallEnoughToInline arg_evals is_case_scrutinee guidance
127     arg_evals                 = [is_evald arg | arg <- args, isValArg arg]
128
129     is_evald (VarArg v) = isEvaluated (lookupRhsInfo env v)
130     is_evald (LitArg l) = True
131
132
133 -- Perform the unfolding
134 unfold var unf_env unf_template args result_ty
135  =
136 {-
137     simplCount          `thenSmpl` \ n ->
138     (if n > 1000 then
139         pprTrace "Ticks > 1000 and unfolding" (sep [space, int n, ppr var])
140     else
141         id
142     )
143     (if n>4000 then
144        returnSmpl (mkGenApp (Var var) args)
145     else
146 -}
147     tickUnfold var              `thenSmpl_`
148     simplExpr unf_env unf_template args result_ty
149
150
151 -- costCentreOk checks that it's ok to inline this thing
152 -- The time it *isn't* is this:
153 --
154 --      f x = let y = E in
155 --            scc "foo" (...y...)
156 --
157 -- Here y has a "current cost centre", and we can't inline it inside "foo",
158 -- regardless of whether E is a WHNF or not.
159
160 costCentreOk cc_encl cc_rhs
161   = isCurrentCostCentre cc_encl || not (isCurrentCostCentre cc_rhs)
162 \end{code}                 
163