2 % (c) The GRASP/AQUA Project, Glasgow University, 1992-1998
4 \section[ConFold]{Constant Folder}
7 check boundaries before folding, e.g. we can fold the Float addition
8 (i1 + i2) only if it results in a valid Float.
11 module PrelRules ( primOpRule, builtinRules ) where
13 #include "HsVersions.h"
16 import Rules ( ProtoCoreRule(..) )
17 import Id ( getIdUnfolding )
18 import Const ( mkMachInt, mkMachWord, Literal(..), Con(..) )
19 import PrimOp ( PrimOp(..), primOpOcc )
20 import TysWiredIn ( trueDataCon, falseDataCon )
21 import TyCon ( tyConDataCons, isEnumerationTyCon, isNewTyCon )
22 import DataCon ( dataConTag, dataConTyCon, fIRST_TAG )
23 import CoreUnfold ( maybeUnfoldingTemplate )
24 import CoreUtils ( exprIsValue, cheapEqExpr )
25 import Type ( splitTyConApp_maybe )
26 import OccName ( occNameUserString)
27 import ThinAir ( unpackCStringFoldrId )
28 import Maybes ( maybeToBool )
29 import Char ( ord, chr )
32 #if __GLASGOW_HASKELL__ >= 404
33 import GlaExts ( fromInt )
40 primOpRule :: PrimOp -> CoreRule
42 = BuiltinRule (primop_rule op)
44 op_name = _PK_ (occNameUserString (primOpOcc op))
45 op_name_case = op_name _APPEND_ SLIT("case")
47 -- ToDo: something for integer-shift ops?
49 -- Int2WordOp -- SIGH: these two cause trouble in unfoldery
50 -- Int2AddrOp -- as we can't distinguish unsigned literals in interfaces (ToDo?)
52 primop_rule SeqOp = seqRule
53 primop_rule TagToEnumOp = tagToEnumRule
54 primop_rule DataToTagOp = dataToTagRule
57 primop_rule Addr2IntOp = oneLit (addr2IntOp op_name)
60 primop_rule OrdOp = oneLit (chrOp op_name)
62 -- Int/Word operations
63 primop_rule IntAddOp = twoLits (intOp2 (+) op_name)
64 primop_rule IntSubOp = twoLits (intOp2 (-) op_name)
65 primop_rule IntMulOp = twoLits (intOp2 (*) op_name)
66 primop_rule IntQuotOp = twoLits (intOp2Z quot op_name)
67 primop_rule IntRemOp = twoLits (intOp2Z rem op_name)
68 primop_rule IntNegOp = oneLit (negOp op_name)
70 primop_rule ChrOp = oneLit (intCoerce (mkCharVal . chr) op_name)
71 primop_rule Int2FloatOp = oneLit (intCoerce mkFloatVal op_name)
72 primop_rule Int2DoubleOp = oneLit (intCoerce mkDoubleVal op_name)
73 primop_rule Word2IntOp = oneLit (intCoerce mkIntVal op_name)
74 primop_rule Int2WordOp = oneLit (intCoerce mkWordVal op_name)
77 primop_rule FloatAddOp = twoLits (floatOp2 (+) op_name)
78 primop_rule FloatSubOp = twoLits (floatOp2 (-) op_name)
79 primop_rule FloatMulOp = twoLits (floatOp2 (*) op_name)
80 primop_rule FloatDivOp = twoLits (floatOp2Z (/) op_name)
81 primop_rule FloatNegOp = oneLit (negOp op_name)
84 primop_rule DoubleAddOp = twoLits (doubleOp2 (+) op_name)
85 primop_rule DoubleSubOp = twoLits (doubleOp2 (-) op_name)
86 primop_rule DoubleMulOp = twoLits (doubleOp2 (*) op_name)
87 primop_rule DoubleDivOp = twoLits (doubleOp2Z (/) op_name)
89 -- Relational operators
90 primop_rule IntEqOp = relop (==) op_name `or_rule` litVar True op_name_case
91 primop_rule IntNeOp = relop (/=) op_name `or_rule` litVar False op_name_case
92 primop_rule CharEqOp = relop (==) op_name `or_rule` litVar True op_name_case
93 primop_rule CharNeOp = relop (/=) op_name `or_rule` litVar False op_name_case
95 primop_rule IntGtOp = relop (>) op_name
96 primop_rule IntGeOp = relop (>=) op_name
97 primop_rule IntLeOp = relop (<=) op_name
98 primop_rule IntLtOp = relop (<) op_name
100 primop_rule CharGtOp = relop (>) op_name
101 primop_rule CharGeOp = relop (>=) op_name
102 primop_rule CharLeOp = relop (<=) op_name
103 primop_rule CharLtOp = relop (<) op_name
105 primop_rule FloatGtOp = relop (>) op_name
106 primop_rule FloatGeOp = relop (>=) op_name
107 primop_rule FloatLeOp = relop (<=) op_name
108 primop_rule FloatLtOp = relop (<) op_name
109 primop_rule FloatEqOp = relop (==) op_name
110 primop_rule FloatNeOp = relop (/=) op_name
112 primop_rule DoubleGtOp = relop (>) op_name
113 primop_rule DoubleGeOp = relop (>=) op_name
114 primop_rule DoubleLeOp = relop (<=) op_name
115 primop_rule DoubleLtOp = relop (<) op_name
116 primop_rule DoubleEqOp = relop (==) op_name
117 primop_rule DoubleNeOp = relop (/=) op_name
119 primop_rule WordGtOp = relop (>) op_name
120 primop_rule WordGeOp = relop (>=) op_name
121 primop_rule WordLeOp = relop (<=) op_name
122 primop_rule WordLtOp = relop (<) op_name
123 primop_rule WordEqOp = relop (==) op_name
124 primop_rule WordNeOp = relop (/=) op_name
126 primop_rule other = \args -> Nothing
129 %************************************************************************
131 \subsection{Doing the business}
133 %************************************************************************
136 --------------------------
137 intCoerce :: Num a => (a -> CoreExpr) -> RuleName -> Literal -> Maybe (RuleName, CoreExpr)
138 intCoerce fn name (MachInt i _) = Just (name, fn (fromInteger i))
140 --------------------------
141 relop cmp name = twoLits (\l1 l2 -> Just (name, if l1 `cmp` l2 then trueVal else falseVal))
143 --------------------------
144 negOp name (MachFloat f) = Just (name, mkFloatVal (-f))
145 negOp name (MachDouble d) = Just (name, mkDoubleVal (-d))
146 negOp name (MachInt i _) = Just (name, mkIntVal (-i))
148 chrOp name (MachChar c) = Just (name, mkIntVal (fromInt (ord c)))
150 addr2IntOp name (MachAddr i) = Just (name, mkIntVal i)
152 --------------------------
153 intOp2 op name l1@(MachInt i1 s1) l2@(MachInt i2 s2)
154 | (result > fromInt maxInt) || (result < fromInt minInt)
155 -- Better tell the user that we've overflowed...
156 -- ..not that it stops us from actually folding!
157 = pprTrace "Warning:" (text "Integer overflow in expression: " <>
158 ppr name <+> ppr l1 <+> ppr l2) $
159 Just (name, mkIntVal result)
162 = ASSERT( s1 && s2 ) -- Both should be signed
163 Just (name, mkIntVal result)
167 intOp2Z op name (MachInt i1 s1) (MachInt i2 s2)
168 | i2 == 0 = Nothing -- Don't do it if the dividend < 0
169 | otherwise = Just (name, mkIntVal (i1 `op` i2))
172 --------------------------
173 floatOp2 op name (MachFloat f1) (MachFloat f2)
174 = Just (name, mkFloatVal (f1 `op` f2))
176 floatOp2Z op name (MachFloat f1) (MachFloat f2)
177 | f1 /= 0 = Just (name, mkFloatVal (f1 `op` f2))
178 | otherwise = Nothing
181 --------------------------
182 doubleOp2 op name (MachDouble f1) (MachDouble f2)
183 = Just (name, mkDoubleVal (f1 `op` f2))
185 doubleOp2Z op name (MachDouble f1) (MachDouble f2)
186 | f1 /= 0 = Just (name, mkDoubleVal (f1 `op` f2))
187 | otherwise = Nothing
190 --------------------------
198 -- This is a Good Thing, because it allows case-of case things
199 -- to happen, and case-default absorption to happen. For
202 -- if (n ==# 3#) || (n ==# 4#) then e1 else e2
208 -- (modulo the usual precautions to avoid duplicating e1)
210 litVar :: Bool -- True <=> equality, False <=> inequality
213 litVar is_eq name [Con (Literal lit) _, Var var] = do_lit_var is_eq name lit var
214 litVar is_eq name [Var var, Con (Literal lit) _] = do_lit_var is_eq name lit var
215 litVar is_eq name other = Nothing
217 do_lit_var is_eq name lit var
218 = Just (name, Case (Var var) var [(Literal lit, [], val_if_eq),
219 (DEFAULT, [], val_if_neq)])
221 val_if_eq | is_eq = trueVal
222 | otherwise = falseVal
223 val_if_neq | is_eq = falseVal
224 | otherwise = trueVal
228 %************************************************************************
230 \subsection{Vaguely generic functions
232 %************************************************************************
235 type RuleFun = [CoreExpr] -> Maybe (RuleName, CoreExpr)
237 or_rule :: RuleFun -> RuleFun -> RuleFun
238 or_rule r1 r2 args = case r1 args of
239 Just stuff -> Just stuff
242 twoLits :: (Literal -> Literal -> Maybe (RuleName, CoreExpr)) -> RuleFun
243 twoLits rule [Con (Literal l1) _, Con (Literal l2) _] = rule l1 l2
244 twoLits rule other = Nothing
246 oneLit :: (Literal -> Maybe (RuleName, CoreExpr)) -> RuleFun
247 oneLit rule [Con (Literal l1) _] = rule l1
248 oneLit rule other = Nothing
251 trueVal = Con (DataCon trueDataCon) []
252 falseVal = Con (DataCon falseDataCon) []
253 mkIntVal i = Con (Literal (mkMachInt i)) []
254 mkCharVal c = Con (Literal (MachChar c)) []
255 mkWordVal w = Con (Literal (mkMachWord w)) []
256 mkFloatVal f = Con (Literal (MachFloat f)) []
257 mkDoubleVal d = Con (Literal (MachDouble d)) []
261 %************************************************************************
263 \subsection{Special rules for seq, tagToEnum, dataToTag}
265 %************************************************************************
267 In the parallel world, we use _seq_ to control the order in which
268 certain expressions will be evaluated. Operationally, the expression
269 ``_seq_ a b'' evaluates a and then evaluates b. We have an inlining
270 for _seq_ which translates _seq_ to:
272 _seq_ = /\ a b -> \ x::a y::b -> case seq# x of { 0# -> parError#; _ -> y }
274 Now, we know that the seq# primitive will never return 0#, but we
275 don't let the simplifier know that. We also use a special error
276 value, parError#, which is *not* a bottoming Id, so as far as the
277 simplifier is concerned, we have to evaluate seq# a before we know
278 whether or not y will be evaluated.
280 If we didn't have the extra case, then after inlining the compiler might
282 f p q = case seq# p of { _ -> p+q }
284 If it sees that, it can see that f is strict in q, and hence it might
285 evaluate q before p! The "0# ->" case prevents this happening.
286 By having the parError# branch we make sure that anything in the
287 other branch stays there!
289 This is fine, but we'd like to get rid of the extraneous code. Hence,
290 we *do* let the simplifier know that seq# is strict in its argument.
291 As a result, we hope that `a' will be evaluated before seq# is called.
292 At this point, we have a very special and magical simpification which
293 says that ``seq# a'' can be immediately simplified to `1#' if we
294 know that `a' is already evaluated.
296 NB: If we ever do case-floating, we have an extra worry:
299 a' -> let b' = case seq# a of { True -> b; False -> parError# }
305 a' -> let b' = case True of { True -> b; False -> parError# }
319 The second case must never be floated outside of the first!
322 seqRule [Type ty, arg] | exprIsValue arg = Just (SLIT("Seq"), mkIntVal 1)
323 seqRule other = Nothing
328 tagToEnumRule [Type ty, Con (Literal (MachInt i _)) _]
329 = ASSERT( isEnumerationTyCon tycon )
330 Just (SLIT("TagToEnum"), Con (DataCon dc) [])
333 constrs = tyConDataCons tycon
334 (dc:_) = [ dc | dc <- constrs, tag == dataConTag dc - fIRST_TAG ]
335 (Just (tycon,_)) = splitTyConApp_maybe ty
337 tagToEnumRule other = Nothing
340 For dataToTag#, we can reduce if either
342 (a) the argument is a constructor
343 (b) the argument is a variable whose unfolding is a known constructor
346 dataToTagRule [_, val_arg]
348 Con (DataCon dc) _ -> yes dc
349 Var x -> case maybeUnfoldingTemplate (getIdUnfolding x) of
350 Just (Con (DataCon dc) _) -> yes dc
354 yes dc = ASSERT( not (isNewTyCon (dataConTyCon dc)) )
355 Just (SLIT("DataToTag"),
356 mkIntVal (toInteger (dataConTag dc - fIRST_TAG)))
358 dataToTagRule other = Nothing
361 %************************************************************************
363 \subsection{Built in rules}
365 %************************************************************************
368 builtinRules :: [ProtoCoreRule]
370 = [ ProtoCoreRule False unpackCStringFoldrId
371 (BuiltinRule match_append_lit_str)
375 -- unpack "foo" c (unpack "baz" c n) = unpack "foobaz" c n
377 match_append_lit_str [Type ty1,
378 Con (Literal (MachStr s1)) [],
380 Var unpk `App` Type ty2
381 `App` Con (Literal (MachStr s2)) []
385 | unpk == unpackCStringFoldrId &&
387 = ASSERT( ty1 == ty2 )
388 Just (SLIT("AppendLitString"),
389 Var unpk `App` Type ty1
390 `App` Con (Literal (MachStr (s1 _APPEND_ s2))) []
394 match_append_lit_str other = Nothing