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