summaryrefslogtreecommitdiff
path: root/ghc/compiler/basicTypes/IdInfo.lhs
blob: f2084c8265835f0080d2c089c06e2161a04e9bbe (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
%
% (c) The GRASP/AQUA Project, Glasgow University, 1993-1998
%
\section[IdInfo]{@IdInfos@: Non-essential information about @Ids@}

(And a pretty good illustration of quite a few things wrong with
Haskell. [WDP 94/11])

\begin{code}
module IdInfo (
	IdInfo,		-- Abstract

	noIdInfo,
	ppIdInfo,

	-- Arity
	ArityInfo(..),
	exactArity, atLeastArity, unknownArity,
	arityInfo, setArityInfo, ppArityInfo, arityLowerBound,

	-- Strictness
	StrictnessInfo(..),				-- Non-abstract
	workerExists, mkStrictnessInfo, mkBottomStrictnessInfo, 
	noStrictnessInfo, bottomIsGuaranteed, strictnessInfo, 
	ppStrictnessInfo, setStrictnessInfo, 

	-- Unfolding
	unfoldingInfo, setUnfoldingInfo, 

	-- DemandInfo
	demandInfo, setDemandInfo, 

	-- Inline prags
	InlinePragInfo(..), OccInfo(..),
	inlinePragInfo, setInlinePragInfo, notInsideLambda,

	-- Specialisation
	IdSpecEnv, specInfo, setSpecInfo,

	-- Update
	UpdateInfo, UpdateSpec,
	mkUpdateInfo, updateInfo, updateInfoMaybe, ppUpdateInfo, setUpdateInfo,

	-- CAF info
	CafInfo(..), cafInfo, setCafInfo, ppCafInfo,
    ) where

#include "HsVersions.h"


import {-# SOURCE #-} CoreUnfold ( Unfolding, noUnfolding )
import {-# SOURCE #-} CoreSyn	 ( CoreExpr )

import SpecEnv	        ( SpecEnv, emptySpecEnv )
import Demand		( Demand,  isLazy, wwLazy, pprDemands )
import Outputable	
\end{code}

An @IdInfo@ gives {\em optional} information about an @Id@.  If
present it never lies, but it may not be present, in which case there
is always a conservative assumption which can be made.

Two @Id@s may have different info even though they have the same
@Unique@ (and are hence the same @Id@); for example, one might lack
the properties attached to the other.

The @IdInfo@ gives information about the value, or definition, of the
@Id@.  It does {\em not} contain information about the @Id@'s usage
(except for @DemandInfo@? ToDo).

\begin{code}
data IdInfo
  = IdInfo {
	arityInfo :: ArityInfo,			-- Its arity
	demandInfo :: Demand,			-- Whether or not it is definitely demanded
	specInfo :: IdSpecEnv,			-- Specialisations of this function which exist
	strictnessInfo :: StrictnessInfo,	-- Strictness properties
	unfoldingInfo :: Unfolding,		-- Its unfolding
	updateInfo :: UpdateInfo,		-- Which args should be updated
	cafInfo :: CafInfo,
	inlinePragInfo :: !InlinePragInfo	-- Inline pragmas
    }
\end{code}

Setters

\begin{code}
setUpdateInfo	  ud info = info { updateInfo = ud }
setDemandInfo	  dd info = info { demandInfo = dd }
setStrictnessInfo st info = info { strictnessInfo = st }
setSpecInfo 	  sp info = info { specInfo = sp }
setArityInfo	  ar info = info { arityInfo = ar  }
setInlinePragInfo pr info = info { inlinePragInfo = pr }
setUnfoldingInfo  uf info = info { unfoldingInfo = uf }
setCafInfo        cf info = info { cafInfo = cf }
\end{code}


\begin{code}
noIdInfo = IdInfo {
		arityInfo	= UnknownArity,
		demandInfo	= wwLazy,
		specInfo	= emptySpecEnv,
		strictnessInfo	= NoStrictnessInfo,
		unfoldingInfo	= noUnfolding,
		updateInfo	= NoUpdateInfo,
		cafInfo		= MayHaveCafRefs,
		inlinePragInfo  = NoInlinePragInfo
	   }
\end{code}

\begin{code}
ppIdInfo :: IdInfo -> SDoc
ppIdInfo (IdInfo {arityInfo, 
		  demandInfo,
		  specInfo,
		  strictnessInfo, 
		  unfoldingInfo,
		  updateInfo, 
		  cafInfo,
		  inlinePragInfo})
  = hsep [
	    ppArityInfo arityInfo,
	    ppUpdateInfo updateInfo,
	    ppStrictnessInfo strictnessInfo,
	    ppr demandInfo,
	    ppCafInfo cafInfo
	-- Inline pragma printed out with all binders; see PprCore.pprIdBndr
	]
\end{code}

%************************************************************************
%*									*
\subsection[arity-IdInfo]{Arity info about an @Id@}
%*									*
%************************************************************************

For locally-defined Ids, the code generator maintains its own notion
of their arities; so it should not be asking...	 (but other things
besides the code-generator need arity info!)

\begin{code}
data ArityInfo
  = UnknownArity	-- No idea
  | ArityExactly Int	-- Arity is exactly this
  | ArityAtLeast Int	-- Arity is this or greater

exactArity   = ArityExactly
atLeastArity = ArityAtLeast
unknownArity = UnknownArity

arityLowerBound :: ArityInfo -> Int
arityLowerBound UnknownArity     = 0
arityLowerBound (ArityAtLeast n) = n
arityLowerBound (ArityExactly n) = n


ppArityInfo UnknownArity	 = empty
ppArityInfo (ArityExactly arity) = hsep [ptext SLIT("__A"), int arity]
ppArityInfo (ArityAtLeast arity) = hsep [ptext SLIT("__AL"), int arity]
\end{code}

%************************************************************************
%*									*
\subsection{Inline-pragma information}
%*									*
%************************************************************************

\begin{code}
data InlinePragInfo
  = NoInlinePragInfo

  | IAmASpecPragmaId	-- Used for spec-pragma Ids; don't discard or inline

  | IWantToBeINLINEd	-- User INLINE pragma
  | IMustNotBeINLINEd	-- User NOINLINE pragma

  | IAmALoopBreaker	-- Used by the occurrence analyser to mark loop-breakers
			-- in a group of recursive definitions

  | ICanSafelyBeINLINEd	-- Used by the occurrence analyser to mark things
			-- that manifesly occur once, not inside SCCs, 
			-- not in constructor arguments

	OccInfo		-- Says whether the occurrence is inside a lambda
			--	If so, must only substitute WHNFs

	Bool		-- False <=> occurs in more than one case branch
			--	If so, there's a code-duplication issue

  | IAmDead		-- Marks unused variables.  Sometimes useful for
			-- lambda and case-bound variables.

  | IMustBeINLINEd	-- Absolutely must inline; used for PrimOps and
			-- constructors only.

instance Outputable InlinePragInfo where
  ppr NoInlinePragInfo  	= empty
  ppr IMustBeINLINEd    	= ptext SLIT("__UU")
  ppr IWantToBeINLINEd  	= ptext SLIT("__U")
  ppr IMustNotBeINLINEd 	= ptext SLIT("__Unot")
  ppr IAmALoopBreaker   	= ptext SLIT("__Ux")
  ppr IAmDead			= ptext SLIT("__Ud")
  ppr (ICanSafelyBeINLINEd _ _) = ptext SLIT("__Us")
  ppr IAmASpecPragmaId 		= ptext SLIT("__US")

instance Show InlinePragInfo where
  showsPrec p prag = showsPrecSDoc p (ppr prag)
\end{code}

The @IMustNotBeDiscarded@ exists only to make Ids that are
on the *LHS* of bindings created by SPECIALISE pragmas; 
eg:		s = f Int d
The SpecPragmaId is never itself mentioned; it
exists solely so that the specialiser will find
the call to f, and make specialised version of it.
The SpecPragmaId binding is discarded by the specialiser
when it gathers up overloaded calls.
Meanwhile, it is not discarded as dead code.

\begin{code}
data OccInfo
  = StrictOcc		-- Occurs syntactically strictly;
			-- i.e. in a function position or case scrutinee

  | LazyOcc		-- Not syntactically strict (*even* that of a strict function)
			-- or in a case branch where there's more than one alternative

  | InsideLam		-- Inside a non-linear lambda (that is, a lambda which
			-- is sure to be instantiated only once).
			-- Substituting a redex for this occurrence is
			-- dangerous because it might duplicate work.

instance Outputable OccInfo where
  ppr StrictOcc = text "s"
  ppr LazyOcc   = empty
  ppr InsideLam = text "l"


notInsideLambda :: OccInfo -> Bool
notInsideLambda StrictOcc = True
notInsideLambda LazyOcc   = True
notInsideLambda InsideLam = False
\end{code}

%************************************************************************
%*									*
\subsection[specialisation-IdInfo]{Specialisation info about an @Id@}
%*									*
%************************************************************************

A @IdSpecEnv@ holds details of an @Id@'s specialisations. 

\begin{code}
type IdSpecEnv = SpecEnv CoreExpr
\end{code}

For example, if \tr{f}'s @SpecEnv@ contains the mapping:
\begin{verbatim}
	[List a, b]  ===>  (\d -> f' a b)
\end{verbatim}
then when we find an application of f to matching types, we simply replace
it by the matching RHS:
\begin{verbatim}
	f (List Int) Bool ===>  (\d -> f' Int Bool)
\end{verbatim}
All the stuff about how many dictionaries to discard, and what types
to apply the specialised function to, are handled by the fact that the
SpecEnv contains a template for the result of the specialisation.

There is one more exciting case, which is dealt with in exactly the same
way.  If the specialised value is unboxed then it is lifted at its
definition site and unlifted at its uses.  For example:

	pi :: forall a. Num a => a

might have a specialisation

	[Int#] ===>  (case pi' of Lift pi# -> pi#)

where pi' :: Lift Int# is the specialised version of pi.



%************************************************************************
%*									*
\subsection[strictness-IdInfo]{Strictness info about an @Id@}
%*									*
%************************************************************************

We specify the strictness of a function by giving information about
each of the ``wrapper's'' arguments (see the description about
worker/wrapper-style transformations in the PJ/Launchbury paper on
unboxed types).

The list of @Demands@ specifies: (a)~the strictness properties
of a function's arguments; (b)~the {\em existence} of a ``worker''
version of the function; and (c)~the type signature of that worker (if
it exists); i.e. its calling convention.

\begin{code}
data StrictnessInfo
  = NoStrictnessInfo

  | BottomGuaranteed	-- This Id guarantees never to return;
			-- it is bottom regardless of its arguments.
			-- Useful for "error" and other disguised
			-- variants thereof.

  | StrictnessInfo [Demand] 
		   Bool		-- True <=> there is a worker. There might not be, even for a
				-- strict function, because:
				-- 	(a) the function might be small enough to inline, 
				--	    so no need for w/w split
				-- 	(b) the strictness info might be "SSS" or something, so no w/w split.

				-- Worker's Id, if applicable, and a list of the constructors
				-- mentioned by the wrapper.  This is necessary so that the
				-- renamer can slurp them in.  Without this info, the renamer doesn't
				-- know which data types to slurp in concretely.  Remember, for
				-- strict things we don't put the unfolding in the interface file, to save space.
				-- This constructor list allows the renamer to behave much as if the
				-- unfolding *was* in the interface file.
\end{code}

\begin{code}
mkStrictnessInfo :: [Demand] -> Bool -> StrictnessInfo

mkStrictnessInfo xs has_wrkr
  | all isLazy xs	 = NoStrictnessInfo		-- Uninteresting
  | otherwise		 = StrictnessInfo xs has_wrkr

noStrictnessInfo       = NoStrictnessInfo
mkBottomStrictnessInfo = BottomGuaranteed

bottomIsGuaranteed BottomGuaranteed = True
bottomIsGuaranteed other    	    = False

ppStrictnessInfo NoStrictnessInfo = empty
ppStrictnessInfo BottomGuaranteed = ptext SLIT("__bot")

ppStrictnessInfo (StrictnessInfo wrapper_args wrkr_maybe)
  = hsep [ptext SLIT("__S"), pprDemands wrapper_args]
\end{code}


\begin{code}
workerExists :: StrictnessInfo -> Bool
workerExists (StrictnessInfo _ worker_exists) = worker_exists
workerExists other			      = False
\end{code}


%************************************************************************
%*									*
\subsection[update-IdInfo]{Update-analysis info about an @Id@}
%*									*
%************************************************************************

\begin{code}
data UpdateInfo
  = NoUpdateInfo
  | SomeUpdateInfo UpdateSpec
  deriving (Eq, Ord)
      -- we need Eq/Ord to cross-chk update infos in interfaces

-- the form in which we pass update-analysis info between modules:
type UpdateSpec = [Int]
\end{code}

\begin{code}
mkUpdateInfo = SomeUpdateInfo

updateInfoMaybe NoUpdateInfo	    = Nothing
updateInfoMaybe (SomeUpdateInfo []) = Nothing
updateInfoMaybe (SomeUpdateInfo	 u) = Just u
\end{code}

Text instance so that the update annotations can be read in.

\begin{code}
ppUpdateInfo NoUpdateInfo	   = empty
ppUpdateInfo (SomeUpdateInfo [])   = empty
ppUpdateInfo (SomeUpdateInfo spec) = (<>) (ptext SLIT("__U ")) (hcat (map int spec))
\end{code}

%************************************************************************
%*									*
\subsection[CAF-IdInfo]{CAF-related information}
%*									*
%************************************************************************

This information is used to build Static Reference Tables (see
simplStg/ComputeSRT.lhs).

\begin{code}
data CafInfo 
	= MayHaveCafRefs		-- either:
					-- (1) A function or static constructor
					--     that refers to one or more CAFs,
					-- (2) A real live CAF

	| NoCafRefs			-- A function or static constructor
				        -- that refers to no CAFs.

-- LATER: not sure how easy this is...
--      | OneCafRef Id


ppCafInfo NoCafRefs = ptext SLIT("__C")
ppCafInfo MayHaveCafRefs = empty
\end{code}