root/compiler/basicTypes/Unique.lhs

Revision 1469f1eb7817fbc46b17e994498450a9a6b12ea7, 12.1 KB (checked in by Simon Marlow <marlowsd@…>, 6 months ago)

More changes aimed at improving call stacks.

  • Attach a SrcSpan? to every CostCentre?. This had the side effect that CostCentres? that used to be merged because they had the same name are now considered distinct; so I had to add a Unique to CostCentre? to give them distinct object-code symbols.
  • New flag: -fprof-auto-calls. This flag adds an automatic SCC to every call site (application, to be precise). This is typically more useful for call stacks than annotating whole functions.

Various tidy-ups at the same time: removed unused NoCostCentre?
constructor, and refactored a bit in Coverage.lhs.

The call stack we get from traceStack now looks like this:

Stack trace:

Main.CAF (<entire-module>)
Main.main.xs (callstack002.hs:18:12-24)
Main.map (callstack002.hs:13:12-16)
Main.map.go (callstack002.hs:15:21-34)
Main.map.go (callstack002.hs:15:21-23)
Main.f (callstack002.hs:10:7-43)

  • Property mode set to 100644
Line 
1%
2% (c) The University of Glasgow 2006
3% (c) The GRASP/AQUA Project, Glasgow University, 1992-1998
4%
5
6@Uniques@ are used to distinguish entities in the compiler (@Ids@,
7@Classes@, etc.) from each other.  Thus, @Uniques@ are the basic
8comparison key in the compiler.
9
10If there is any single operation that needs to be fast, it is @Unique@
11comparison.  Unsurprisingly, there is quite a bit of huff-and-puff
12directed to that end.
13
14Some of the other hair in this code is to be able to use a
15``splittable @UniqueSupply@'' if requested/possible (not standard
16Haskell).
17
18\begin{code}
19{-# LANGUAGE BangPatterns #-}
20
21{-# OPTIONS -fno-warn-tabs #-}
22-- The above warning supression flag is a temporary kludge.
23-- While working on this module you are encouraged to remove it and
24-- detab the module (please do the detabbing in a separate patch). See
25--     http://hackage.haskell.org/trac/ghc/wiki/Commentary/CodingStyle#TabsvsSpaces
26-- for details
27
28module Unique (
29        -- * Main data types
30        Unique, Uniquable(..), 
31       
32        -- ** Constructors, desctructors and operations on 'Unique's
33        hasKey,
34
35        pprUnique, 
36
37        mkUniqueGrimily,                -- Used in UniqSupply only!
38        getKey, getKeyFastInt,          -- Used in Var, UniqFM, Name only!
39        mkUnique, unpkUnique,           -- Used in BinIface only
40
41        incrUnique,                     -- Used for renumbering
42        deriveUnique,                   -- Ditto
43        newTagUnique,                   -- Used in CgCase
44        initTyVarUnique,
45
46        -- ** Making built-in uniques
47
48        -- now all the built-in Uniques (and functions to make them)
49        -- [the Oh-So-Wonderful Haskell module system wins again...]
50        mkAlphaTyVarUnique,
51        mkPrimOpIdUnique,
52        mkTupleTyConUnique, mkTupleDataConUnique,
53        mkPreludeMiscIdUnique, mkPreludeDataConUnique,
54        mkPreludeTyConUnique, mkPreludeClassUnique,
55        mkPArrDataConUnique,
56
57    mkVarOccUnique, mkDataOccUnique, mkTvOccUnique, mkTcOccUnique,
58        mkRegSingleUnique, mkRegPairUnique, mkRegClassUnique, mkRegSubUnique,
59        mkCostCentreUnique,
60
61        mkBuiltinUnique,
62        mkPseudoUniqueD,
63        mkPseudoUniqueE,
64        mkPseudoUniqueH
65    ) where
66
67#include "HsVersions.h"
68
69import BasicTypes
70import FastTypes
71import FastString
72import Outputable
73-- import StaticFlags
74
75#if defined(__GLASGOW_HASKELL__)
76--just for implementing a fast [0,61) -> Char function
77import GHC.Exts (indexCharOffAddr#, Char(..))
78#else
79import Data.Array
80#endif
81import Data.Char        ( chr, ord )
82\end{code}
83
84%************************************************************************
85%*                                                                      *
86\subsection[Unique-type]{@Unique@ type and operations}
87%*                                                                      *
88%************************************************************************
89
90The @Chars@ are ``tag letters'' that identify the @UniqueSupply@.
91Fast comparison is everything on @Uniques@:
92
93\begin{code}
94--why not newtype Int?
95
96-- | The type of unique identifiers that are used in many places in GHC
97-- for fast ordering and equality tests. You should generate these with
98-- the functions from the 'UniqSupply' module
99data Unique = MkUnique FastInt
100\end{code}
101
102Now come the functions which construct uniques from their pieces, and vice versa.
103The stuff about unique *supplies* is handled further down this module.
104
105\begin{code}
106unpkUnique      :: Unique -> (Char, Int)        -- The reverse
107
108mkUniqueGrimily :: Int -> Unique                -- A trap-door for UniqSupply
109getKey          :: Unique -> Int                -- for Var
110getKeyFastInt   :: Unique -> FastInt            -- for Var
111
112incrUnique      :: Unique -> Unique
113deriveUnique    :: Unique -> Int -> Unique
114newTagUnique    :: Unique -> Char -> Unique
115\end{code}
116
117
118\begin{code}
119mkUniqueGrimily x = MkUnique (iUnbox x)
120
121{-# INLINE getKey #-}
122getKey (MkUnique x) = iBox x
123{-# INLINE getKeyFastInt #-}
124getKeyFastInt (MkUnique x) = x
125
126incrUnique (MkUnique i) = MkUnique (i +# _ILIT(1))
127
128-- deriveUnique uses an 'X' tag so that it won't clash with
129-- any of the uniques produced any other way
130deriveUnique (MkUnique i) delta = mkUnique 'X' (iBox i + delta)
131
132-- newTagUnique changes the "domain" of a unique to a different char
133newTagUnique u c = mkUnique c i where (_,i) = unpkUnique u
134
135-- pop the Char in the top 8 bits of the Unique(Supply)
136
137-- No 64-bit bugs here, as long as we have at least 32 bits. --JSM
138
139-- and as long as the Char fits in 8 bits, which we assume anyway!
140
141mkUnique :: Char -> Int -> Unique       -- Builds a unique from pieces
142-- NOT EXPORTED, so that we can see all the Chars that
143--               are used in this one module
144mkUnique c i
145  = MkUnique (tag `bitOrFastInt` bits)
146  where
147    !tag  = fastOrd (cUnbox c) `shiftLFastInt` _ILIT(24)
148    !bits = iUnbox i `bitAndFastInt` _ILIT(16777215){-``0x00ffffff''-}
149
150unpkUnique (MkUnique u)
151  = let
152        -- as long as the Char may have its eighth bit set, we
153        -- really do need the logical right-shift here!
154        tag = cBox (fastChr (u `shiftRLFastInt` _ILIT(24)))
155        i   = iBox (u `bitAndFastInt` _ILIT(16777215){-``0x00ffffff''-})
156    in
157    (tag, i)
158\end{code}
159
160
161
162%************************************************************************
163%*                                                                      *
164\subsection[Uniquable-class]{The @Uniquable@ class}
165%*                                                                      *
166%************************************************************************
167
168\begin{code}
169-- | Class of things that we can obtain a 'Unique' from
170class Uniquable a where
171    getUnique :: a -> Unique
172
173hasKey          :: Uniquable a => a -> Unique -> Bool
174x `hasKey` k    = getUnique x == k
175
176instance Uniquable FastString where
177 getUnique fs = mkUniqueGrimily (iBox (uniqueOfFS fs))
178
179instance Uniquable Int where
180 getUnique i = mkUniqueGrimily i
181
182instance Uniquable n => Uniquable (IPName n) where
183  getUnique (IPName n) = getUnique n
184\end{code}
185
186
187%************************************************************************
188%*                                                                      *
189\subsection[Unique-instances]{Instance declarations for @Unique@}
190%*                                                                      *
191%************************************************************************
192
193And the whole point (besides uniqueness) is fast equality.  We don't
194use `deriving' because we want {\em precise} control of ordering
195(equality on @Uniques@ is v common).
196
197\begin{code}
198eqUnique, ltUnique, leUnique :: Unique -> Unique -> Bool
199eqUnique (MkUnique u1) (MkUnique u2) = u1 ==# u2
200ltUnique (MkUnique u1) (MkUnique u2) = u1 <#  u2
201leUnique (MkUnique u1) (MkUnique u2) = u1 <=# u2
202
203cmpUnique :: Unique -> Unique -> Ordering
204cmpUnique (MkUnique u1) (MkUnique u2)
205  = if u1 ==# u2 then EQ else if u1 <# u2 then LT else GT
206
207instance Eq Unique where
208    a == b = eqUnique a b
209    a /= b = not (eqUnique a b)
210
211instance Ord Unique where
212    a  < b = ltUnique a b
213    a <= b = leUnique a b
214    a  > b = not (leUnique a b)
215    a >= b = not (ltUnique a b)
216    compare a b = cmpUnique a b
217
218-----------------
219instance Uniquable Unique where
220    getUnique u = u
221\end{code}
222
223We do sometimes make strings with @Uniques@ in them:
224\begin{code}
225pprUnique :: Unique -> SDoc
226pprUnique uniq
227--   | opt_SuppressUniques
228--  = empty     -- Used exclusively to suppress uniques so you
229--  | otherwise -- can compare output easily
230  = case unpkUnique uniq of
231      (tag, u) -> finish_ppr tag u (text (iToBase62 u))
232
233#ifdef UNUSED
234pprUnique10 :: Unique -> SDoc
235pprUnique10 uniq        -- in base-10, dudes
236  = case unpkUnique uniq of
237      (tag, u) -> finish_ppr tag u (int u)
238#endif
239
240finish_ppr :: Char -> Int -> SDoc -> SDoc
241finish_ppr 't' u _pp_u | u < 26
242  =     -- Special case to make v common tyvars, t1, t2, ...
243        -- come out as a, b, ... (shorter, easier to read)
244    char (chr (ord 'a' + u))
245finish_ppr tag _ pp_u = char tag <> pp_u
246
247instance Outputable Unique where
248    ppr u = pprUnique u
249
250instance Show Unique where
251    showsPrec p uniq = showsPrecSDoc p (pprUnique uniq)
252\end{code}
253
254%************************************************************************
255%*                                                                      *
256\subsection[Utils-base62]{Base-62 numbers}
257%*                                                                      *
258%************************************************************************
259
260A character-stingy way to read/write numbers (notably Uniques).
261The ``62-its'' are \tr{[0-9a-zA-Z]}.  We don't handle negative Ints.
262Code stolen from Lennart.
263
264\begin{code}
265iToBase62 :: Int -> String
266iToBase62 n_
267  = ASSERT(n_ >= 0) go (iUnbox n_) ""
268  where
269    go n cs | n <# _ILIT(62)
270             = case chooseChar62 n of { c -> c `seq` (c : cs) }
271             | otherwise
272             =  case (quotRem (iBox n) 62) of { (q_, r_) ->
273                case iUnbox q_ of { q -> case iUnbox r_ of { r ->
274                case (chooseChar62 r) of { c -> c `seq`
275                (go q (c : cs)) }}}}
276
277    chooseChar62 :: FastInt -> Char
278    {-# INLINE chooseChar62 #-}
279#if defined(__GLASGOW_HASKELL__)
280    --then FastInt == Int#
281    chooseChar62 n = C# (indexCharOffAddr# chars62 n)
282    !chars62 = "0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ"#
283#else
284    --Haskell98 arrays are portable
285    chooseChar62 n = (!) chars62 n
286    chars62 = listArray (0,61) "0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ"
287#endif
288\end{code}
289
290%************************************************************************
291%*                                                                      *
292\subsection[Uniques-prelude]{@Uniques@ for wired-in Prelude things}
293%*                                                                      *
294%************************************************************************
295
296Allocation of unique supply characters:
297        v,t,u : for renumbering value-, type- and usage- vars.
298        B:   builtin
299        C-E: pseudo uniques     (used in native-code generator)
300        X:   uniques derived by deriveUnique
301        _:   unifiable tyvars   (above)
302        0-9: prelude things below
303             (no numbers left any more..)
304        ::   (prelude) parallel array data constructors
305
306        other a-z: lower case chars for unique supplies.  Used so far:
307
308        d       desugarer
309        f       AbsC flattener
310        g       SimplStg
311        n       Native codegen
312        r       Hsc name cache
313        s       simplifier
314
315\begin{code}
316mkAlphaTyVarUnique     :: Int -> Unique
317mkPreludeClassUnique   :: Int -> Unique
318mkPreludeTyConUnique   :: Int -> Unique
319mkTupleTyConUnique     :: TupleSort -> Int -> Unique
320mkPreludeDataConUnique :: Int -> Unique
321mkTupleDataConUnique   :: TupleSort -> Int -> Unique
322mkPrimOpIdUnique       :: Int -> Unique
323mkPreludeMiscIdUnique  :: Int -> Unique
324mkPArrDataConUnique    :: Int -> Unique
325
326mkAlphaTyVarUnique i            = mkUnique '1' i
327
328mkPreludeClassUnique i          = mkUnique '2' i
329
330-- Prelude type constructors occupy *three* slots.
331-- The first is for the tycon itself; the latter two
332-- are for the generic to/from Ids.  See TysWiredIn.mk_tc_gen_info.
333
334mkPreludeTyConUnique i          = mkUnique '3' (3*i)
335mkTupleTyConUnique BoxedTuple   a       = mkUnique '4' (3*a)
336mkTupleTyConUnique UnboxedTuple a       = mkUnique '5' (3*a)
337mkTupleTyConUnique ConstraintTuple a    = mkUnique 'k' (3*a)
338
339-- Data constructor keys occupy *two* slots.  The first is used for the
340-- data constructor itself and its wrapper function (the function that
341-- evaluates arguments as necessary and calls the worker). The second is
342-- used for the worker function (the function that builds the constructor
343-- representation).
344
345mkPreludeDataConUnique i        = mkUnique '6' (2*i)    -- Must be alphabetic
346mkTupleDataConUnique BoxedTuple   a = mkUnique '7' (2*a)        -- ditto (*may* be used in C labels)
347mkTupleDataConUnique UnboxedTuple    a = mkUnique '8' (2*a)
348mkTupleDataConUnique ConstraintTuple a = mkUnique 'h' (2*a)
349
350mkPrimOpIdUnique op         = mkUnique '9' op
351mkPreludeMiscIdUnique  i    = mkUnique '0' i
352
353-- No numbers left anymore, so I pick something different for the character tag
354mkPArrDataConUnique a           = mkUnique ':' (2*a)
355
356-- The "tyvar uniques" print specially nicely: a, b, c, etc.
357-- See pprUnique for details
358
359initTyVarUnique :: Unique
360initTyVarUnique = mkUnique 't' 0
361
362mkPseudoUniqueD, mkPseudoUniqueE, mkPseudoUniqueH,
363   mkBuiltinUnique :: Int -> Unique
364
365mkBuiltinUnique i = mkUnique 'B' i
366mkPseudoUniqueD i = mkUnique 'D' i -- used in NCG for getUnique on RealRegs
367mkPseudoUniqueE i = mkUnique 'E' i -- used in NCG spiller to create spill VirtualRegs
368mkPseudoUniqueH i = mkUnique 'H' i -- used in NCG spiller to create spill VirtualRegs
369
370mkRegSingleUnique, mkRegPairUnique, mkRegSubUnique, mkRegClassUnique :: Int -> Unique
371mkRegSingleUnique = mkUnique 'R'
372mkRegSubUnique    = mkUnique 'S'
373mkRegPairUnique   = mkUnique 'P'
374mkRegClassUnique  = mkUnique 'L'
375
376mkCostCentreUnique :: Int -> Unique
377mkCostCentreUnique = mkUnique 'C'
378
379mkVarOccUnique, mkDataOccUnique, mkTvOccUnique, mkTcOccUnique :: FastString -> Unique
380-- See Note [The Unique of an OccName] in OccName
381mkVarOccUnique  fs = mkUnique 'i' (iBox (uniqueOfFS fs))
382mkDataOccUnique fs = mkUnique 'd' (iBox (uniqueOfFS fs))
383mkTvOccUnique   fs = mkUnique 'v' (iBox (uniqueOfFS fs))
384mkTcOccUnique   fs = mkUnique 'c' (iBox (uniqueOfFS fs))
385\end{code}
Note: See TracBrowser for help on using the browser.