babfe4bedf537bd2ae9de0a3cb875518403f1573
[ghc.git] / compiler / prelude / PrelRules.hs
1 {-
2 (c) The GRASP/AQUA Project, Glasgow University, 1992-1998
3
4 \section[ConFold]{Constant Folder}
5
6 Conceptually, constant folding should be parameterized with the kind
7 of target machine to get identical behaviour during compilation time
8 and runtime. We cheat a little bit here...
9
10 ToDo:
11 check boundaries before folding, e.g. we can fold the Float addition
12 (i1 + i2) only if it results in a valid Float.
13 -}
14
15 {-# LANGUAGE CPP, RankNTypes #-}
16 {-# OPTIONS_GHC -optc-DNON_POSIX_SOURCE #-}
17
18 module PrelRules
19 ( primOpRules
20 , builtinRules
21 , caseRules
22 )
23 where
24
25 #include "HsVersions.h"
26 #include "../includes/MachDeps.h"
27
28 import GhcPrelude
29
30 import {-# SOURCE #-} MkId ( mkPrimOpId, magicDictId )
31
32 import CoreSyn
33 import MkCore
34 import Id
35 import Literal
36 import CoreOpt ( exprIsLiteral_maybe )
37 import PrimOp ( PrimOp(..), tagToEnumKey )
38 import TysWiredIn
39 import TysPrim
40 import TyCon ( tyConDataCons_maybe, isEnumerationTyCon, isNewTyCon
41 , unwrapNewTyCon_maybe, tyConDataCons )
42 import DataCon ( DataCon, dataConTagZ, dataConTyCon, dataConWorkId )
43 import CoreUtils ( cheapEqExpr, exprIsHNF )
44 import CoreUnfold ( exprIsConApp_maybe )
45 import Type
46 import OccName ( occNameFS )
47 import PrelNames
48 import Maybes ( orElse )
49 import Name ( Name, nameOccName )
50 import Outputable
51 import FastString
52 import BasicTypes
53 import DynFlags
54 import Platform
55 import Util
56 import Coercion (mkUnbranchedAxInstCo,mkSymCo,Role(..))
57
58 import Control.Applicative ( Alternative(..) )
59
60 import Control.Monad
61 import qualified Control.Monad.Fail as MonadFail
62 import Data.Bits as Bits
63 import qualified Data.ByteString as BS
64 import Data.Int
65 import Data.Ratio
66 import Data.Word
67
68 {-
69 Note [Constant folding]
70 ~~~~~~~~~~~~~~~~~~~~~~~
71 primOpRules generates a rewrite rule for each primop
72 These rules do what is often called "constant folding"
73 E.g. the rules for +# might say
74 4 +# 5 = 9
75 Well, of course you'd need a lot of rules if you did it
76 like that, so we use a BuiltinRule instead, so that we
77 can match in any two literal values. So the rule is really
78 more like
79 (Lit x) +# (Lit y) = Lit (x+#y)
80 where the (+#) on the rhs is done at compile time
81
82 That is why these rules are built in here.
83 -}
84
85 primOpRules :: Name -> PrimOp -> Maybe CoreRule
86 -- ToDo: something for integer-shift ops?
87 -- NotOp
88 primOpRules nm TagToEnumOp = mkPrimOpRule nm 2 [ tagToEnumRule ]
89 primOpRules nm DataToTagOp = mkPrimOpRule nm 2 [ dataToTagRule ]
90
91 -- Int operations
92 primOpRules nm IntAddOp = mkPrimOpRule nm 2 [ binaryLit (intOp2 (+))
93 , identityDynFlags zeroi ]
94 primOpRules nm IntSubOp = mkPrimOpRule nm 2 [ binaryLit (intOp2 (-))
95 , rightIdentityDynFlags zeroi
96 , equalArgs >> retLit zeroi ]
97 primOpRules nm IntMulOp = mkPrimOpRule nm 2 [ binaryLit (intOp2 (*))
98 , zeroElem zeroi
99 , identityDynFlags onei ]
100 primOpRules nm IntQuotOp = mkPrimOpRule nm 2 [ nonZeroLit 1 >> binaryLit (intOp2 quot)
101 , leftZero zeroi
102 , rightIdentityDynFlags onei
103 , equalArgs >> retLit onei ]
104 primOpRules nm IntRemOp = mkPrimOpRule nm 2 [ nonZeroLit 1 >> binaryLit (intOp2 rem)
105 , leftZero zeroi
106 , do l <- getLiteral 1
107 dflags <- getDynFlags
108 guard (l == onei dflags)
109 retLit zeroi
110 , equalArgs >> retLit zeroi
111 , equalArgs >> retLit zeroi ]
112 primOpRules nm AndIOp = mkPrimOpRule nm 2 [ binaryLit (intOp2 (.&.))
113 , idempotent
114 , zeroElem zeroi ]
115 primOpRules nm OrIOp = mkPrimOpRule nm 2 [ binaryLit (intOp2 (.|.))
116 , idempotent
117 , identityDynFlags zeroi ]
118 primOpRules nm XorIOp = mkPrimOpRule nm 2 [ binaryLit (intOp2 xor)
119 , identityDynFlags zeroi
120 , equalArgs >> retLit zeroi ]
121 primOpRules nm NotIOp = mkPrimOpRule nm 1 [ unaryLit complementOp
122 , inversePrimOp NotIOp ]
123 primOpRules nm IntNegOp = mkPrimOpRule nm 1 [ unaryLit negOp
124 , inversePrimOp IntNegOp ]
125 primOpRules nm ISllOp = mkPrimOpRule nm 2 [ binaryLit (intOp2 Bits.shiftL)
126 , rightIdentityDynFlags zeroi ]
127 primOpRules nm ISraOp = mkPrimOpRule nm 2 [ binaryLit (intOp2 Bits.shiftR)
128 , rightIdentityDynFlags zeroi ]
129 primOpRules nm ISrlOp = mkPrimOpRule nm 2 [ binaryLit (intOp2' shiftRightLogical)
130 , rightIdentityDynFlags zeroi ]
131
132 -- Word operations
133 primOpRules nm WordAddOp = mkPrimOpRule nm 2 [ binaryLit (wordOp2 (+))
134 , identityDynFlags zerow ]
135 primOpRules nm WordSubOp = mkPrimOpRule nm 2 [ binaryLit (wordOp2 (-))
136 , rightIdentityDynFlags zerow
137 , equalArgs >> retLit zerow ]
138 primOpRules nm WordMulOp = mkPrimOpRule nm 2 [ binaryLit (wordOp2 (*))
139 , identityDynFlags onew ]
140 primOpRules nm WordQuotOp = mkPrimOpRule nm 2 [ nonZeroLit 1 >> binaryLit (wordOp2 quot)
141 , rightIdentityDynFlags onew ]
142 primOpRules nm WordRemOp = mkPrimOpRule nm 2 [ nonZeroLit 1 >> binaryLit (wordOp2 rem)
143 , leftZero zerow
144 , do l <- getLiteral 1
145 dflags <- getDynFlags
146 guard (l == onew dflags)
147 retLit zerow
148 , equalArgs >> retLit zerow ]
149 primOpRules nm AndOp = mkPrimOpRule nm 2 [ binaryLit (wordOp2 (.&.))
150 , idempotent
151 , zeroElem zerow ]
152 primOpRules nm OrOp = mkPrimOpRule nm 2 [ binaryLit (wordOp2 (.|.))
153 , idempotent
154 , identityDynFlags zerow ]
155 primOpRules nm XorOp = mkPrimOpRule nm 2 [ binaryLit (wordOp2 xor)
156 , identityDynFlags zerow
157 , equalArgs >> retLit zerow ]
158 primOpRules nm NotOp = mkPrimOpRule nm 1 [ unaryLit complementOp
159 , inversePrimOp NotOp ]
160 primOpRules nm SllOp = mkPrimOpRule nm 2 [ wordShiftRule (const Bits.shiftL) ]
161 primOpRules nm SrlOp = mkPrimOpRule nm 2 [ wordShiftRule shiftRightLogical ]
162
163 -- coercions
164 primOpRules nm Word2IntOp = mkPrimOpRule nm 1 [ liftLitDynFlags word2IntLit
165 , inversePrimOp Int2WordOp ]
166 primOpRules nm Int2WordOp = mkPrimOpRule nm 1 [ liftLitDynFlags int2WordLit
167 , inversePrimOp Word2IntOp ]
168 primOpRules nm Narrow8IntOp = mkPrimOpRule nm 1 [ liftLit narrow8IntLit
169 , subsumedByPrimOp Narrow8IntOp
170 , Narrow8IntOp `subsumesPrimOp` Narrow16IntOp
171 , Narrow8IntOp `subsumesPrimOp` Narrow32IntOp ]
172 primOpRules nm Narrow16IntOp = mkPrimOpRule nm 1 [ liftLit narrow16IntLit
173 , subsumedByPrimOp Narrow8IntOp
174 , subsumedByPrimOp Narrow16IntOp
175 , Narrow16IntOp `subsumesPrimOp` Narrow32IntOp ]
176 primOpRules nm Narrow32IntOp = mkPrimOpRule nm 1 [ liftLit narrow32IntLit
177 , subsumedByPrimOp Narrow8IntOp
178 , subsumedByPrimOp Narrow16IntOp
179 , subsumedByPrimOp Narrow32IntOp
180 , removeOp32 ]
181 primOpRules nm Narrow8WordOp = mkPrimOpRule nm 1 [ liftLit narrow8WordLit
182 , subsumedByPrimOp Narrow8WordOp
183 , Narrow8WordOp `subsumesPrimOp` Narrow16WordOp
184 , Narrow8WordOp `subsumesPrimOp` Narrow32WordOp ]
185 primOpRules nm Narrow16WordOp = mkPrimOpRule nm 1 [ liftLit narrow16WordLit
186 , subsumedByPrimOp Narrow8WordOp
187 , subsumedByPrimOp Narrow16WordOp
188 , Narrow16WordOp `subsumesPrimOp` Narrow32WordOp ]
189 primOpRules nm Narrow32WordOp = mkPrimOpRule nm 1 [ liftLit narrow32WordLit
190 , subsumedByPrimOp Narrow8WordOp
191 , subsumedByPrimOp Narrow16WordOp
192 , subsumedByPrimOp Narrow32WordOp
193 , removeOp32 ]
194 primOpRules nm OrdOp = mkPrimOpRule nm 1 [ liftLit char2IntLit
195 , inversePrimOp ChrOp ]
196 primOpRules nm ChrOp = mkPrimOpRule nm 1 [ do [Lit lit] <- getArgs
197 guard (litFitsInChar lit)
198 liftLit int2CharLit
199 , inversePrimOp OrdOp ]
200 primOpRules nm Float2IntOp = mkPrimOpRule nm 1 [ liftLit float2IntLit ]
201 primOpRules nm Int2FloatOp = mkPrimOpRule nm 1 [ liftLit int2FloatLit ]
202 primOpRules nm Double2IntOp = mkPrimOpRule nm 1 [ liftLit double2IntLit ]
203 primOpRules nm Int2DoubleOp = mkPrimOpRule nm 1 [ liftLit int2DoubleLit ]
204 -- SUP: Not sure what the standard says about precision in the following 2 cases
205 primOpRules nm Float2DoubleOp = mkPrimOpRule nm 1 [ liftLit float2DoubleLit ]
206 primOpRules nm Double2FloatOp = mkPrimOpRule nm 1 [ liftLit double2FloatLit ]
207
208 -- Float
209 primOpRules nm FloatAddOp = mkPrimOpRule nm 2 [ binaryLit (floatOp2 (+))
210 , identity zerof ]
211 primOpRules nm FloatSubOp = mkPrimOpRule nm 2 [ binaryLit (floatOp2 (-))
212 , rightIdentity zerof ]
213 primOpRules nm FloatMulOp = mkPrimOpRule nm 2 [ binaryLit (floatOp2 (*))
214 , identity onef
215 , strengthReduction twof FloatAddOp ]
216 -- zeroElem zerof doesn't hold because of NaN
217 primOpRules nm FloatDivOp = mkPrimOpRule nm 2 [ guardFloatDiv >> binaryLit (floatOp2 (/))
218 , rightIdentity onef ]
219 primOpRules nm FloatNegOp = mkPrimOpRule nm 1 [ unaryLit negOp
220 , inversePrimOp FloatNegOp ]
221
222 -- Double
223 primOpRules nm DoubleAddOp = mkPrimOpRule nm 2 [ binaryLit (doubleOp2 (+))
224 , identity zerod ]
225 primOpRules nm DoubleSubOp = mkPrimOpRule nm 2 [ binaryLit (doubleOp2 (-))
226 , rightIdentity zerod ]
227 primOpRules nm DoubleMulOp = mkPrimOpRule nm 2 [ binaryLit (doubleOp2 (*))
228 , identity oned
229 , strengthReduction twod DoubleAddOp ]
230 -- zeroElem zerod doesn't hold because of NaN
231 primOpRules nm DoubleDivOp = mkPrimOpRule nm 2 [ guardDoubleDiv >> binaryLit (doubleOp2 (/))
232 , rightIdentity oned ]
233 primOpRules nm DoubleNegOp = mkPrimOpRule nm 1 [ unaryLit negOp
234 , inversePrimOp DoubleNegOp ]
235
236 -- Relational operators
237
238 primOpRules nm IntEqOp = mkRelOpRule nm (==) [ litEq True ]
239 primOpRules nm IntNeOp = mkRelOpRule nm (/=) [ litEq False ]
240 primOpRules nm CharEqOp = mkRelOpRule nm (==) [ litEq True ]
241 primOpRules nm CharNeOp = mkRelOpRule nm (/=) [ litEq False ]
242
243 primOpRules nm IntGtOp = mkRelOpRule nm (>) [ boundsCmp Gt ]
244 primOpRules nm IntGeOp = mkRelOpRule nm (>=) [ boundsCmp Ge ]
245 primOpRules nm IntLeOp = mkRelOpRule nm (<=) [ boundsCmp Le ]
246 primOpRules nm IntLtOp = mkRelOpRule nm (<) [ boundsCmp Lt ]
247
248 primOpRules nm CharGtOp = mkRelOpRule nm (>) [ boundsCmp Gt ]
249 primOpRules nm CharGeOp = mkRelOpRule nm (>=) [ boundsCmp Ge ]
250 primOpRules nm CharLeOp = mkRelOpRule nm (<=) [ boundsCmp Le ]
251 primOpRules nm CharLtOp = mkRelOpRule nm (<) [ boundsCmp Lt ]
252
253 primOpRules nm FloatGtOp = mkFloatingRelOpRule nm (>)
254 primOpRules nm FloatGeOp = mkFloatingRelOpRule nm (>=)
255 primOpRules nm FloatLeOp = mkFloatingRelOpRule nm (<=)
256 primOpRules nm FloatLtOp = mkFloatingRelOpRule nm (<)
257 primOpRules nm FloatEqOp = mkFloatingRelOpRule nm (==)
258 primOpRules nm FloatNeOp = mkFloatingRelOpRule nm (/=)
259
260 primOpRules nm DoubleGtOp = mkFloatingRelOpRule nm (>)
261 primOpRules nm DoubleGeOp = mkFloatingRelOpRule nm (>=)
262 primOpRules nm DoubleLeOp = mkFloatingRelOpRule nm (<=)
263 primOpRules nm DoubleLtOp = mkFloatingRelOpRule nm (<)
264 primOpRules nm DoubleEqOp = mkFloatingRelOpRule nm (==)
265 primOpRules nm DoubleNeOp = mkFloatingRelOpRule nm (/=)
266
267 primOpRules nm WordGtOp = mkRelOpRule nm (>) [ boundsCmp Gt ]
268 primOpRules nm WordGeOp = mkRelOpRule nm (>=) [ boundsCmp Ge ]
269 primOpRules nm WordLeOp = mkRelOpRule nm (<=) [ boundsCmp Le ]
270 primOpRules nm WordLtOp = mkRelOpRule nm (<) [ boundsCmp Lt ]
271 primOpRules nm WordEqOp = mkRelOpRule nm (==) [ litEq True ]
272 primOpRules nm WordNeOp = mkRelOpRule nm (/=) [ litEq False ]
273
274 primOpRules nm AddrAddOp = mkPrimOpRule nm 2 [ rightIdentityDynFlags zeroi ]
275
276 primOpRules nm SeqOp = mkPrimOpRule nm 4 [ seqRule ]
277 primOpRules nm SparkOp = mkPrimOpRule nm 4 [ sparkRule ]
278
279 primOpRules _ _ = Nothing
280
281 {-
282 ************************************************************************
283 * *
284 \subsection{Doing the business}
285 * *
286 ************************************************************************
287 -}
288
289 -- useful shorthands
290 mkPrimOpRule :: Name -> Int -> [RuleM CoreExpr] -> Maybe CoreRule
291 mkPrimOpRule nm arity rules = Just $ mkBasicRule nm arity (msum rules)
292
293 mkRelOpRule :: Name -> (forall a . Ord a => a -> a -> Bool)
294 -> [RuleM CoreExpr] -> Maybe CoreRule
295 mkRelOpRule nm cmp extra
296 = mkPrimOpRule nm 2 $
297 binaryCmpLit cmp : equal_rule : extra
298 where
299 -- x `cmp` x does not depend on x, so
300 -- compute it for the arbitrary value 'True'
301 -- and use that result
302 equal_rule = do { equalArgs
303 ; dflags <- getDynFlags
304 ; return (if cmp True True
305 then trueValInt dflags
306 else falseValInt dflags) }
307
308 {- Note [Rules for floating-point comparisons]
309 ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
310 We need different rules for floating-point values because for floats
311 it is not true that x = x (for NaNs); so we do not want the equal_rule
312 rule that mkRelOpRule uses.
313
314 Note also that, in the case of equality/inequality, we do /not/
315 want to switch to a case-expression. For example, we do not want
316 to convert
317 case (eqFloat# x 3.8#) of
318 True -> this
319 False -> that
320 to
321 case x of
322 3.8#::Float# -> this
323 _ -> that
324 See Trac #9238. Reason: comparing floating-point values for equality
325 delicate, and we don't want to implement that delicacy in the code for
326 case expressions. So we make it an invariant of Core that a case
327 expression never scrutinises a Float# or Double#.
328
329 This transformation is what the litEq rule does;
330 see Note [The litEq rule: converting equality to case].
331 So we /refrain/ from using litEq for mkFloatingRelOpRule.
332 -}
333
334 mkFloatingRelOpRule :: Name -> (forall a . Ord a => a -> a -> Bool)
335 -> Maybe CoreRule
336 -- See Note [Rules for floating-point comparisons]
337 mkFloatingRelOpRule nm cmp
338 = mkPrimOpRule nm 2 [binaryCmpLit cmp]
339
340 -- common constants
341 zeroi, onei, zerow, onew :: DynFlags -> Literal
342 zeroi dflags = mkMachInt dflags 0
343 onei dflags = mkMachInt dflags 1
344 zerow dflags = mkMachWord dflags 0
345 onew dflags = mkMachWord dflags 1
346
347 zerof, onef, twof, zerod, oned, twod :: Literal
348 zerof = mkMachFloat 0.0
349 onef = mkMachFloat 1.0
350 twof = mkMachFloat 2.0
351 zerod = mkMachDouble 0.0
352 oned = mkMachDouble 1.0
353 twod = mkMachDouble 2.0
354
355 cmpOp :: DynFlags -> (forall a . Ord a => a -> a -> Bool)
356 -> Literal -> Literal -> Maybe CoreExpr
357 cmpOp dflags cmp = go
358 where
359 done True = Just $ trueValInt dflags
360 done False = Just $ falseValInt dflags
361
362 -- These compares are at different types
363 go (MachChar i1) (MachChar i2) = done (i1 `cmp` i2)
364 go (MachInt i1) (MachInt i2) = done (i1 `cmp` i2)
365 go (MachInt64 i1) (MachInt64 i2) = done (i1 `cmp` i2)
366 go (MachWord i1) (MachWord i2) = done (i1 `cmp` i2)
367 go (MachWord64 i1) (MachWord64 i2) = done (i1 `cmp` i2)
368 go (MachFloat i1) (MachFloat i2) = done (i1 `cmp` i2)
369 go (MachDouble i1) (MachDouble i2) = done (i1 `cmp` i2)
370 go _ _ = Nothing
371
372 --------------------------
373
374 negOp :: DynFlags -> Literal -> Maybe CoreExpr -- Negate
375 negOp _ (MachFloat 0.0) = Nothing -- can't represent -0.0 as a Rational
376 negOp dflags (MachFloat f) = Just (mkFloatVal dflags (-f))
377 negOp _ (MachDouble 0.0) = Nothing
378 negOp dflags (MachDouble d) = Just (mkDoubleVal dflags (-d))
379 negOp dflags (MachInt i) = intResult dflags (-i)
380 negOp _ _ = Nothing
381
382 complementOp :: DynFlags -> Literal -> Maybe CoreExpr -- Binary complement
383 complementOp dflags (MachWord i) = wordResult dflags (complement i)
384 complementOp dflags (MachInt i) = intResult dflags (complement i)
385 complementOp _ _ = Nothing
386
387 --------------------------
388 intOp2 :: (Integral a, Integral b)
389 => (a -> b -> Integer)
390 -> DynFlags -> Literal -> Literal -> Maybe CoreExpr
391 intOp2 = intOp2' . const
392
393 intOp2' :: (Integral a, Integral b)
394 => (DynFlags -> a -> b -> Integer)
395 -> DynFlags -> Literal -> Literal -> Maybe CoreExpr
396 intOp2' op dflags (MachInt i1) (MachInt i2) =
397 let o = op dflags
398 in intResult dflags (fromInteger i1 `o` fromInteger i2)
399 intOp2' _ _ _ _ = Nothing -- Could find LitLit
400
401 shiftRightLogical :: DynFlags -> Integer -> Int -> Integer
402 -- Shift right, putting zeros in rather than sign-propagating as Bits.shiftR would do
403 -- Do this by converting to Word and back. Obviously this won't work for big
404 -- values, but its ok as we use it here
405 shiftRightLogical dflags x n
406 | wordSizeInBits dflags == 32 = fromIntegral (fromInteger x `shiftR` n :: Word32)
407 | wordSizeInBits dflags == 64 = fromIntegral (fromInteger x `shiftR` n :: Word64)
408 | otherwise = panic "shiftRightLogical: unsupported word size"
409
410 --------------------------
411 retLit :: (DynFlags -> Literal) -> RuleM CoreExpr
412 retLit l = do dflags <- getDynFlags
413 return $ Lit $ l dflags
414
415 wordOp2 :: (Integral a, Integral b)
416 => (a -> b -> Integer)
417 -> DynFlags -> Literal -> Literal -> Maybe CoreExpr
418 wordOp2 op dflags (MachWord w1) (MachWord w2)
419 = wordResult dflags (fromInteger w1 `op` fromInteger w2)
420 wordOp2 _ _ _ _ = Nothing -- Could find LitLit
421
422 wordShiftRule :: (DynFlags -> Integer -> Int -> Integer) -> RuleM CoreExpr
423 -- Shifts take an Int; hence third arg of op is Int
424 -- See Note [Guarding against silly shifts]
425 wordShiftRule shift_op
426 = do { dflags <- getDynFlags
427 ; [e1, Lit (MachInt shift_len)] <- getArgs
428 ; case e1 of
429 _ | shift_len == 0
430 -> return e1
431 | shift_len < 0 || wordSizeInBits dflags < shift_len
432 -> return (mkRuntimeErrorApp rUNTIME_ERROR_ID wordPrimTy
433 ("Bad shift length" ++ show shift_len))
434 Lit (MachWord x)
435 -> let op = shift_op dflags
436 in liftMaybe $ wordResult dflags (x `op` fromInteger shift_len)
437 -- Do the shift at type Integer, but shift length is Int
438 _ -> mzero }
439
440 wordSizeInBits :: DynFlags -> Integer
441 wordSizeInBits dflags = toInteger (platformWordSize (targetPlatform dflags) `shiftL` 3)
442
443 --------------------------
444 floatOp2 :: (Rational -> Rational -> Rational)
445 -> DynFlags -> Literal -> Literal
446 -> Maybe (Expr CoreBndr)
447 floatOp2 op dflags (MachFloat f1) (MachFloat f2)
448 = Just (mkFloatVal dflags (f1 `op` f2))
449 floatOp2 _ _ _ _ = Nothing
450
451 --------------------------
452 doubleOp2 :: (Rational -> Rational -> Rational)
453 -> DynFlags -> Literal -> Literal
454 -> Maybe (Expr CoreBndr)
455 doubleOp2 op dflags (MachDouble f1) (MachDouble f2)
456 = Just (mkDoubleVal dflags (f1 `op` f2))
457 doubleOp2 _ _ _ _ = Nothing
458
459 --------------------------
460 {- Note [The litEq rule: converting equality to case]
461 ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
462 This stuff turns
463 n ==# 3#
464 into
465 case n of
466 3# -> True
467 m -> False
468
469 This is a Good Thing, because it allows case-of case things
470 to happen, and case-default absorption to happen. For
471 example:
472
473 if (n ==# 3#) || (n ==# 4#) then e1 else e2
474 will transform to
475 case n of
476 3# -> e1
477 4# -> e1
478 m -> e2
479 (modulo the usual precautions to avoid duplicating e1)
480 -}
481
482 litEq :: Bool -- True <=> equality, False <=> inequality
483 -> RuleM CoreExpr
484 litEq is_eq = msum
485 [ do [Lit lit, expr] <- getArgs
486 dflags <- getDynFlags
487 do_lit_eq dflags lit expr
488 , do [expr, Lit lit] <- getArgs
489 dflags <- getDynFlags
490 do_lit_eq dflags lit expr ]
491 where
492 do_lit_eq dflags lit expr = do
493 guard (not (litIsLifted lit))
494 return (mkWildCase expr (literalType lit) intPrimTy
495 [(DEFAULT, [], val_if_neq),
496 (LitAlt lit, [], val_if_eq)])
497 where
498 val_if_eq | is_eq = trueValInt dflags
499 | otherwise = falseValInt dflags
500 val_if_neq | is_eq = falseValInt dflags
501 | otherwise = trueValInt dflags
502
503
504 -- | Check if there is comparison with minBound or maxBound, that is
505 -- always true or false. For instance, an Int cannot be smaller than its
506 -- minBound, so we can replace such comparison with False.
507 boundsCmp :: Comparison -> RuleM CoreExpr
508 boundsCmp op = do
509 dflags <- getDynFlags
510 [a, b] <- getArgs
511 liftMaybe $ mkRuleFn dflags op a b
512
513 data Comparison = Gt | Ge | Lt | Le
514
515 mkRuleFn :: DynFlags -> Comparison -> CoreExpr -> CoreExpr -> Maybe CoreExpr
516 mkRuleFn dflags Gt (Lit lit) _ | isMinBound dflags lit = Just $ falseValInt dflags
517 mkRuleFn dflags Le (Lit lit) _ | isMinBound dflags lit = Just $ trueValInt dflags
518 mkRuleFn dflags Ge _ (Lit lit) | isMinBound dflags lit = Just $ trueValInt dflags
519 mkRuleFn dflags Lt _ (Lit lit) | isMinBound dflags lit = Just $ falseValInt dflags
520 mkRuleFn dflags Ge (Lit lit) _ | isMaxBound dflags lit = Just $ trueValInt dflags
521 mkRuleFn dflags Lt (Lit lit) _ | isMaxBound dflags lit = Just $ falseValInt dflags
522 mkRuleFn dflags Gt _ (Lit lit) | isMaxBound dflags lit = Just $ falseValInt dflags
523 mkRuleFn dflags Le _ (Lit lit) | isMaxBound dflags lit = Just $ trueValInt dflags
524 mkRuleFn _ _ _ _ = Nothing
525
526 isMinBound :: DynFlags -> Literal -> Bool
527 isMinBound _ (MachChar c) = c == minBound
528 isMinBound dflags (MachInt i) = i == tARGET_MIN_INT dflags
529 isMinBound _ (MachInt64 i) = i == toInteger (minBound :: Int64)
530 isMinBound _ (MachWord i) = i == 0
531 isMinBound _ (MachWord64 i) = i == 0
532 isMinBound _ _ = False
533
534 isMaxBound :: DynFlags -> Literal -> Bool
535 isMaxBound _ (MachChar c) = c == maxBound
536 isMaxBound dflags (MachInt i) = i == tARGET_MAX_INT dflags
537 isMaxBound _ (MachInt64 i) = i == toInteger (maxBound :: Int64)
538 isMaxBound dflags (MachWord i) = i == tARGET_MAX_WORD dflags
539 isMaxBound _ (MachWord64 i) = i == toInteger (maxBound :: Word64)
540 isMaxBound _ _ = False
541
542 -- | Create an Int literal expression while ensuring the given Integer is in the
543 -- target Int range
544 intResult :: DynFlags -> Integer -> Maybe CoreExpr
545 intResult dflags result = Just (Lit (mkMachIntWrap dflags result))
546
547 -- | Create a Word literal expression while ensuring the given Integer is in the
548 -- target Word range
549 wordResult :: DynFlags -> Integer -> Maybe CoreExpr
550 wordResult dflags result = Just (Lit (mkMachWordWrap dflags result))
551
552 inversePrimOp :: PrimOp -> RuleM CoreExpr
553 inversePrimOp primop = do
554 [Var primop_id `App` e] <- getArgs
555 matchPrimOpId primop primop_id
556 return e
557
558 subsumesPrimOp :: PrimOp -> PrimOp -> RuleM CoreExpr
559 this `subsumesPrimOp` that = do
560 [Var primop_id `App` e] <- getArgs
561 matchPrimOpId that primop_id
562 return (Var (mkPrimOpId this) `App` e)
563
564 subsumedByPrimOp :: PrimOp -> RuleM CoreExpr
565 subsumedByPrimOp primop = do
566 [e@(Var primop_id `App` _)] <- getArgs
567 matchPrimOpId primop primop_id
568 return e
569
570 idempotent :: RuleM CoreExpr
571 idempotent = do [e1, e2] <- getArgs
572 guard $ cheapEqExpr e1 e2
573 return e1
574
575 {-
576 Note [Guarding against silly shifts]
577 ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
578 Consider this code:
579
580 import Data.Bits( (.|.), shiftL )
581 chunkToBitmap :: [Bool] -> Word32
582 chunkToBitmap chunk = foldr (.|.) 0 [ 1 `shiftL` n | (True,n) <- zip chunk [0..] ]
583
584 This optimises to:
585 Shift.$wgo = \ (w_sCS :: GHC.Prim.Int#) (w1_sCT :: [GHC.Types.Bool]) ->
586 case w1_sCT of _ {
587 [] -> 0##;
588 : x_aAW xs_aAX ->
589 case x_aAW of _ {
590 GHC.Types.False ->
591 case w_sCS of wild2_Xh {
592 __DEFAULT -> Shift.$wgo (GHC.Prim.+# wild2_Xh 1) xs_aAX;
593 9223372036854775807 -> 0## };
594 GHC.Types.True ->
595 case GHC.Prim.>=# w_sCS 64 of _ {
596 GHC.Types.False ->
597 case w_sCS of wild3_Xh {
598 __DEFAULT ->
599 case Shift.$wgo (GHC.Prim.+# wild3_Xh 1) xs_aAX of ww_sCW { __DEFAULT ->
600 GHC.Prim.or# (GHC.Prim.narrow32Word#
601 (GHC.Prim.uncheckedShiftL# 1## wild3_Xh))
602 ww_sCW
603 };
604 9223372036854775807 ->
605 GHC.Prim.narrow32Word#
606 !!!!--> (GHC.Prim.uncheckedShiftL# 1## 9223372036854775807)
607 };
608 GHC.Types.True ->
609 case w_sCS of wild3_Xh {
610 __DEFAULT -> Shift.$wgo (GHC.Prim.+# wild3_Xh 1) xs_aAX;
611 9223372036854775807 -> 0##
612 } } } }
613
614 Note the massive shift on line "!!!!". It can't happen, because we've checked
615 that w < 64, but the optimiser didn't spot that. We DO NO want to constant-fold this!
616 Moreover, if the programmer writes (n `uncheckedShiftL` 9223372036854775807), we
617 can't constant fold it, but if it gets to the assember we get
618 Error: operand type mismatch for `shl'
619
620 So the best thing to do is to rewrite the shift with a call to error,
621 when the second arg is stupid.
622
623 ************************************************************************
624 * *
625 \subsection{Vaguely generic functions}
626 * *
627 ************************************************************************
628 -}
629
630 mkBasicRule :: Name -> Int -> RuleM CoreExpr -> CoreRule
631 -- Gives the Rule the same name as the primop itself
632 mkBasicRule op_name n_args rm
633 = BuiltinRule { ru_name = occNameFS (nameOccName op_name),
634 ru_fn = op_name,
635 ru_nargs = n_args,
636 ru_try = \ dflags in_scope _ -> runRuleM rm dflags in_scope }
637
638 newtype RuleM r = RuleM
639 { runRuleM :: DynFlags -> InScopeEnv -> [CoreExpr] -> Maybe r }
640
641 instance Functor RuleM where
642 fmap = liftM
643
644 instance Applicative RuleM where
645 pure x = RuleM $ \_ _ _ -> Just x
646 (<*>) = ap
647
648 instance Monad RuleM where
649 RuleM f >>= g = RuleM $ \dflags iu e -> case f dflags iu e of
650 Nothing -> Nothing
651 Just r -> runRuleM (g r) dflags iu e
652 fail = MonadFail.fail
653
654 instance MonadFail.MonadFail RuleM where
655 fail _ = mzero
656
657 instance Alternative RuleM where
658 empty = RuleM $ \_ _ _ -> Nothing
659 RuleM f1 <|> RuleM f2 = RuleM $ \dflags iu args ->
660 f1 dflags iu args <|> f2 dflags iu args
661
662 instance MonadPlus RuleM
663
664 instance HasDynFlags RuleM where
665 getDynFlags = RuleM $ \dflags _ _ -> Just dflags
666
667 liftMaybe :: Maybe a -> RuleM a
668 liftMaybe Nothing = mzero
669 liftMaybe (Just x) = return x
670
671 liftLit :: (Literal -> Literal) -> RuleM CoreExpr
672 liftLit f = liftLitDynFlags (const f)
673
674 liftLitDynFlags :: (DynFlags -> Literal -> Literal) -> RuleM CoreExpr
675 liftLitDynFlags f = do
676 dflags <- getDynFlags
677 [Lit lit] <- getArgs
678 return $ Lit (f dflags lit)
679
680 removeOp32 :: RuleM CoreExpr
681 removeOp32 = do
682 dflags <- getDynFlags
683 if wordSizeInBits dflags == 32
684 then do
685 [e] <- getArgs
686 return e
687 else mzero
688
689 getArgs :: RuleM [CoreExpr]
690 getArgs = RuleM $ \_ _ args -> Just args
691
692 getInScopeEnv :: RuleM InScopeEnv
693 getInScopeEnv = RuleM $ \_ iu _ -> Just iu
694
695 -- return the n-th argument of this rule, if it is a literal
696 -- argument indices start from 0
697 getLiteral :: Int -> RuleM Literal
698 getLiteral n = RuleM $ \_ _ exprs -> case drop n exprs of
699 (Lit l:_) -> Just l
700 _ -> Nothing
701
702 unaryLit :: (DynFlags -> Literal -> Maybe CoreExpr) -> RuleM CoreExpr
703 unaryLit op = do
704 dflags <- getDynFlags
705 [Lit l] <- getArgs
706 liftMaybe $ op dflags (convFloating dflags l)
707
708 binaryLit :: (DynFlags -> Literal -> Literal -> Maybe CoreExpr) -> RuleM CoreExpr
709 binaryLit op = do
710 dflags <- getDynFlags
711 [Lit l1, Lit l2] <- getArgs
712 liftMaybe $ op dflags (convFloating dflags l1) (convFloating dflags l2)
713
714 binaryCmpLit :: (forall a . Ord a => a -> a -> Bool) -> RuleM CoreExpr
715 binaryCmpLit op = do
716 dflags <- getDynFlags
717 binaryLit (\_ -> cmpOp dflags op)
718
719 leftIdentity :: Literal -> RuleM CoreExpr
720 leftIdentity id_lit = leftIdentityDynFlags (const id_lit)
721
722 rightIdentity :: Literal -> RuleM CoreExpr
723 rightIdentity id_lit = rightIdentityDynFlags (const id_lit)
724
725 identity :: Literal -> RuleM CoreExpr
726 identity lit = leftIdentity lit `mplus` rightIdentity lit
727
728 leftIdentityDynFlags :: (DynFlags -> Literal) -> RuleM CoreExpr
729 leftIdentityDynFlags id_lit = do
730 dflags <- getDynFlags
731 [Lit l1, e2] <- getArgs
732 guard $ l1 == id_lit dflags
733 return e2
734
735 rightIdentityDynFlags :: (DynFlags -> Literal) -> RuleM CoreExpr
736 rightIdentityDynFlags id_lit = do
737 dflags <- getDynFlags
738 [e1, Lit l2] <- getArgs
739 guard $ l2 == id_lit dflags
740 return e1
741
742 identityDynFlags :: (DynFlags -> Literal) -> RuleM CoreExpr
743 identityDynFlags lit = leftIdentityDynFlags lit `mplus` rightIdentityDynFlags lit
744
745 leftZero :: (DynFlags -> Literal) -> RuleM CoreExpr
746 leftZero zero = do
747 dflags <- getDynFlags
748 [Lit l1, _] <- getArgs
749 guard $ l1 == zero dflags
750 return $ Lit l1
751
752 rightZero :: (DynFlags -> Literal) -> RuleM CoreExpr
753 rightZero zero = do
754 dflags <- getDynFlags
755 [_, Lit l2] <- getArgs
756 guard $ l2 == zero dflags
757 return $ Lit l2
758
759 zeroElem :: (DynFlags -> Literal) -> RuleM CoreExpr
760 zeroElem lit = leftZero lit `mplus` rightZero lit
761
762 equalArgs :: RuleM ()
763 equalArgs = do
764 [e1, e2] <- getArgs
765 guard $ e1 `cheapEqExpr` e2
766
767 nonZeroLit :: Int -> RuleM ()
768 nonZeroLit n = getLiteral n >>= guard . not . isZeroLit
769
770 -- When excess precision is not requested, cut down the precision of the
771 -- Rational value to that of Float/Double. We confuse host architecture
772 -- and target architecture here, but it's convenient (and wrong :-).
773 convFloating :: DynFlags -> Literal -> Literal
774 convFloating dflags (MachFloat f) | not (gopt Opt_ExcessPrecision dflags) =
775 MachFloat (toRational (fromRational f :: Float ))
776 convFloating dflags (MachDouble d) | not (gopt Opt_ExcessPrecision dflags) =
777 MachDouble (toRational (fromRational d :: Double))
778 convFloating _ l = l
779
780 guardFloatDiv :: RuleM ()
781 guardFloatDiv = do
782 [Lit (MachFloat f1), Lit (MachFloat f2)] <- getArgs
783 guard $ (f1 /=0 || f2 > 0) -- see Note [negative zero]
784 && f2 /= 0 -- avoid NaN and Infinity/-Infinity
785
786 guardDoubleDiv :: RuleM ()
787 guardDoubleDiv = do
788 [Lit (MachDouble d1), Lit (MachDouble d2)] <- getArgs
789 guard $ (d1 /=0 || d2 > 0) -- see Note [negative zero]
790 && d2 /= 0 -- avoid NaN and Infinity/-Infinity
791 -- Note [negative zero] Avoid (0 / -d), otherwise 0/(-1) reduces to
792 -- zero, but we might want to preserve the negative zero here which
793 -- is representable in Float/Double but not in (normalised)
794 -- Rational. (#3676) Perhaps we should generate (0 :% (-1)) instead?
795
796 strengthReduction :: Literal -> PrimOp -> RuleM CoreExpr
797 strengthReduction two_lit add_op = do -- Note [Strength reduction]
798 arg <- msum [ do [arg, Lit mult_lit] <- getArgs
799 guard (mult_lit == two_lit)
800 return arg
801 , do [Lit mult_lit, arg] <- getArgs
802 guard (mult_lit == two_lit)
803 return arg ]
804 return $ Var (mkPrimOpId add_op) `App` arg `App` arg
805
806 -- Note [Strength reduction]
807 -- ~~~~~~~~~~~~~~~~~~~~~~~~~
808 --
809 -- This rule turns floating point multiplications of the form 2.0 * x and
810 -- x * 2.0 into x + x addition, because addition costs less than multiplication.
811 -- See #7116
812
813 -- Note [What's true and false]
814 -- ~~~~~~~~~~~~~~~~~~~~~~~~~~~~
815 --
816 -- trueValInt and falseValInt represent true and false values returned by
817 -- comparison primops for Char, Int, Word, Integer, Double, Float and Addr.
818 -- True is represented as an unboxed 1# literal, while false is represented
819 -- as 0# literal.
820 -- We still need Bool data constructors (True and False) to use in a rule
821 -- for constant folding of equal Strings
822
823 trueValInt, falseValInt :: DynFlags -> Expr CoreBndr
824 trueValInt dflags = Lit $ onei dflags -- see Note [What's true and false]
825 falseValInt dflags = Lit $ zeroi dflags
826
827 trueValBool, falseValBool :: Expr CoreBndr
828 trueValBool = Var trueDataConId -- see Note [What's true and false]
829 falseValBool = Var falseDataConId
830
831 ltVal, eqVal, gtVal :: Expr CoreBndr
832 ltVal = Var ltDataConId
833 eqVal = Var eqDataConId
834 gtVal = Var gtDataConId
835
836 mkIntVal :: DynFlags -> Integer -> Expr CoreBndr
837 mkIntVal dflags i = Lit (mkMachInt dflags i)
838 mkFloatVal :: DynFlags -> Rational -> Expr CoreBndr
839 mkFloatVal dflags f = Lit (convFloating dflags (MachFloat f))
840 mkDoubleVal :: DynFlags -> Rational -> Expr CoreBndr
841 mkDoubleVal dflags d = Lit (convFloating dflags (MachDouble d))
842
843 matchPrimOpId :: PrimOp -> Id -> RuleM ()
844 matchPrimOpId op id = do
845 op' <- liftMaybe $ isPrimOpId_maybe id
846 guard $ op == op'
847
848 {-
849 ************************************************************************
850 * *
851 \subsection{Special rules for seq, tagToEnum, dataToTag}
852 * *
853 ************************************************************************
854
855 Note [tagToEnum#]
856 ~~~~~~~~~~~~~~~~~
857 Nasty check to ensure that tagToEnum# is applied to a type that is an
858 enumeration TyCon. Unification may refine the type later, but this
859 check won't see that, alas. It's crude but it works.
860
861 Here's are two cases that should fail
862 f :: forall a. a
863 f = tagToEnum# 0 -- Can't do tagToEnum# at a type variable
864
865 g :: Int
866 g = tagToEnum# 0 -- Int is not an enumeration
867
868 We used to make this check in the type inference engine, but it's quite
869 ugly to do so, because the delayed constraint solving means that we don't
870 really know what's going on until the end. It's very much a corner case
871 because we don't expect the user to call tagToEnum# at all; we merely
872 generate calls in derived instances of Enum. So we compromise: a
873 rewrite rule rewrites a bad instance of tagToEnum# to an error call,
874 and emits a warning.
875 -}
876
877 tagToEnumRule :: RuleM CoreExpr
878 -- If data T a = A | B | C
879 -- then tag2Enum# (T ty) 2# --> B ty
880 tagToEnumRule = do
881 [Type ty, Lit (MachInt i)] <- getArgs
882 case splitTyConApp_maybe ty of
883 Just (tycon, tc_args) | isEnumerationTyCon tycon -> do
884 let tag = fromInteger i
885 correct_tag dc = (dataConTagZ dc) == tag
886 (dc:rest) <- return $ filter correct_tag (tyConDataCons_maybe tycon `orElse` [])
887 ASSERT(null rest) return ()
888 return $ mkTyApps (Var (dataConWorkId dc)) tc_args
889
890 -- See Note [tagToEnum#]
891 _ -> WARN( True, text "tagToEnum# on non-enumeration type" <+> ppr ty )
892 return $ mkRuntimeErrorApp rUNTIME_ERROR_ID ty "tagToEnum# on non-enumeration type"
893
894 {-
895 For dataToTag#, we can reduce if either
896
897 (a) the argument is a constructor
898 (b) the argument is a variable whose unfolding is a known constructor
899 -}
900
901 dataToTagRule :: RuleM CoreExpr
902 dataToTagRule = a `mplus` b
903 where
904 a = do
905 [Type ty1, Var tag_to_enum `App` Type ty2 `App` tag] <- getArgs
906 guard $ tag_to_enum `hasKey` tagToEnumKey
907 guard $ ty1 `eqType` ty2
908 return tag -- dataToTag (tagToEnum x) ==> x
909 b = do
910 dflags <- getDynFlags
911 [_, val_arg] <- getArgs
912 in_scope <- getInScopeEnv
913 (dc,_,_) <- liftMaybe $ exprIsConApp_maybe in_scope val_arg
914 ASSERT( not (isNewTyCon (dataConTyCon dc)) ) return ()
915 return $ mkIntVal dflags (toInteger (dataConTagZ dc))
916
917 {-
918 ************************************************************************
919 * *
920 \subsection{Rules for seq# and spark#}
921 * *
922 ************************************************************************
923 -}
924
925 -- seq# :: forall a s . a -> State# s -> (# State# s, a #)
926 seqRule :: RuleM CoreExpr
927 seqRule = do
928 [Type ty_a, Type ty_s, a, s] <- getArgs
929 guard $ exprIsHNF a
930 return $ mkCoreUbxTup [mkStatePrimTy ty_s, ty_a] [s, a]
931
932 -- spark# :: forall a s . a -> State# s -> (# State# s, a #)
933 sparkRule :: RuleM CoreExpr
934 sparkRule = seqRule -- reduce on HNF, just the same
935 -- XXX perhaps we shouldn't do this, because a spark eliminated by
936 -- this rule won't be counted as a dud at runtime?
937
938 {-
939 ************************************************************************
940 * *
941 \subsection{Built in rules}
942 * *
943 ************************************************************************
944
945 Note [Scoping for Builtin rules]
946 ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
947 When compiling a (base-package) module that defines one of the
948 functions mentioned in the RHS of a built-in rule, there's a danger
949 that we'll see
950
951 f = ...(eq String x)....
952
953 ....and lower down...
954
955 eqString = ...
956
957 Then a rewrite would give
958
959 f = ...(eqString x)...
960 ....and lower down...
961 eqString = ...
962
963 and lo, eqString is not in scope. This only really matters when we get to code
964 generation. With -O we do a GlomBinds step that does a new SCC analysis on the whole
965 set of bindings, which sorts out the dependency. Without -O we don't do any rule
966 rewriting so again we are fine.
967
968 (This whole thing doesn't show up for non-built-in rules because their dependencies
969 are explicit.)
970 -}
971
972 builtinRules :: [CoreRule]
973 -- Rules for non-primops that can't be expressed using a RULE pragma
974 builtinRules
975 = [BuiltinRule { ru_name = fsLit "AppendLitString",
976 ru_fn = unpackCStringFoldrName,
977 ru_nargs = 4, ru_try = match_append_lit },
978 BuiltinRule { ru_name = fsLit "EqString", ru_fn = eqStringName,
979 ru_nargs = 2, ru_try = match_eq_string },
980 BuiltinRule { ru_name = fsLit "Inline", ru_fn = inlineIdName,
981 ru_nargs = 2, ru_try = \_ _ _ -> match_inline },
982 BuiltinRule { ru_name = fsLit "MagicDict", ru_fn = idName magicDictId,
983 ru_nargs = 4, ru_try = \_ _ _ -> match_magicDict },
984 mkBasicRule divIntName 2 $ msum
985 [ nonZeroLit 1 >> binaryLit (intOp2 div)
986 , leftZero zeroi
987 , do
988 [arg, Lit (MachInt d)] <- getArgs
989 Just n <- return $ exactLog2 d
990 dflags <- getDynFlags
991 return $ Var (mkPrimOpId ISraOp) `App` arg `App` mkIntVal dflags n
992 ],
993 mkBasicRule modIntName 2 $ msum
994 [ nonZeroLit 1 >> binaryLit (intOp2 mod)
995 , leftZero zeroi
996 , do
997 [arg, Lit (MachInt d)] <- getArgs
998 Just _ <- return $ exactLog2 d
999 dflags <- getDynFlags
1000 return $ Var (mkPrimOpId AndIOp)
1001 `App` arg `App` mkIntVal dflags (d - 1)
1002 ]
1003 ]
1004 ++ builtinIntegerRules
1005 {-# NOINLINE builtinRules #-}
1006 -- there is no benefit to inlining these yet, despite this, GHC produces
1007 -- unfoldings for this regardless since the floated list entries look small.
1008
1009 builtinIntegerRules :: [CoreRule]
1010 builtinIntegerRules =
1011 [rule_IntToInteger "smallInteger" smallIntegerName,
1012 rule_WordToInteger "wordToInteger" wordToIntegerName,
1013 rule_Int64ToInteger "int64ToInteger" int64ToIntegerName,
1014 rule_Word64ToInteger "word64ToInteger" word64ToIntegerName,
1015 rule_convert "integerToWord" integerToWordName mkWordLitWord,
1016 rule_convert "integerToInt" integerToIntName mkIntLitInt,
1017 rule_convert "integerToWord64" integerToWord64Name (\_ -> mkWord64LitWord64),
1018 rule_convert "integerToInt64" integerToInt64Name (\_ -> mkInt64LitInt64),
1019 rule_binop "plusInteger" plusIntegerName (+),
1020 rule_binop "minusInteger" minusIntegerName (-),
1021 rule_binop "timesInteger" timesIntegerName (*),
1022 rule_unop "negateInteger" negateIntegerName negate,
1023 rule_binop_Prim "eqInteger#" eqIntegerPrimName (==),
1024 rule_binop_Prim "neqInteger#" neqIntegerPrimName (/=),
1025 rule_unop "absInteger" absIntegerName abs,
1026 rule_unop "signumInteger" signumIntegerName signum,
1027 rule_binop_Prim "leInteger#" leIntegerPrimName (<=),
1028 rule_binop_Prim "gtInteger#" gtIntegerPrimName (>),
1029 rule_binop_Prim "ltInteger#" ltIntegerPrimName (<),
1030 rule_binop_Prim "geInteger#" geIntegerPrimName (>=),
1031 rule_binop_Ordering "compareInteger" compareIntegerName compare,
1032 rule_encodeFloat "encodeFloatInteger" encodeFloatIntegerName mkFloatLitFloat,
1033 rule_convert "floatFromInteger" floatFromIntegerName (\_ -> mkFloatLitFloat),
1034 rule_encodeFloat "encodeDoubleInteger" encodeDoubleIntegerName mkDoubleLitDouble,
1035 rule_decodeDouble "decodeDoubleInteger" decodeDoubleIntegerName,
1036 rule_convert "doubleFromInteger" doubleFromIntegerName (\_ -> mkDoubleLitDouble),
1037 rule_rationalTo "rationalToFloat" rationalToFloatName mkFloatExpr,
1038 rule_rationalTo "rationalToDouble" rationalToDoubleName mkDoubleExpr,
1039 rule_binop "gcdInteger" gcdIntegerName gcd,
1040 rule_binop "lcmInteger" lcmIntegerName lcm,
1041 rule_binop "andInteger" andIntegerName (.&.),
1042 rule_binop "orInteger" orIntegerName (.|.),
1043 rule_binop "xorInteger" xorIntegerName xor,
1044 rule_unop "complementInteger" complementIntegerName complement,
1045 rule_Int_binop "shiftLInteger" shiftLIntegerName shiftL,
1046 rule_Int_binop "shiftRInteger" shiftRIntegerName shiftR,
1047 rule_bitInteger "bitInteger" bitIntegerName,
1048 -- See Note [Integer division constant folding] in libraries/base/GHC/Real.hs
1049 rule_divop_one "quotInteger" quotIntegerName quot,
1050 rule_divop_one "remInteger" remIntegerName rem,
1051 rule_divop_one "divInteger" divIntegerName div,
1052 rule_divop_one "modInteger" modIntegerName mod,
1053 rule_divop_both "divModInteger" divModIntegerName divMod,
1054 rule_divop_both "quotRemInteger" quotRemIntegerName quotRem,
1055 -- These rules below don't actually have to be built in, but if we
1056 -- put them in the Haskell source then we'd have to duplicate them
1057 -- between all Integer implementations
1058 rule_XToIntegerToX "smallIntegerToInt" integerToIntName smallIntegerName,
1059 rule_XToIntegerToX "wordToIntegerToWord" integerToWordName wordToIntegerName,
1060 rule_XToIntegerToX "int64ToIntegerToInt64" integerToInt64Name int64ToIntegerName,
1061 rule_XToIntegerToX "word64ToIntegerToWord64" integerToWord64Name word64ToIntegerName,
1062 rule_smallIntegerTo "smallIntegerToWord" integerToWordName Int2WordOp,
1063 rule_smallIntegerTo "smallIntegerToFloat" floatFromIntegerName Int2FloatOp,
1064 rule_smallIntegerTo "smallIntegerToDouble" doubleFromIntegerName Int2DoubleOp
1065 ]
1066 where rule_convert str name convert
1067 = BuiltinRule { ru_name = fsLit str, ru_fn = name, ru_nargs = 1,
1068 ru_try = match_Integer_convert convert }
1069 rule_IntToInteger str name
1070 = BuiltinRule { ru_name = fsLit str, ru_fn = name, ru_nargs = 1,
1071 ru_try = match_IntToInteger }
1072 rule_WordToInteger str name
1073 = BuiltinRule { ru_name = fsLit str, ru_fn = name, ru_nargs = 1,
1074 ru_try = match_WordToInteger }
1075 rule_Int64ToInteger str name
1076 = BuiltinRule { ru_name = fsLit str, ru_fn = name, ru_nargs = 1,
1077 ru_try = match_Int64ToInteger }
1078 rule_Word64ToInteger str name
1079 = BuiltinRule { ru_name = fsLit str, ru_fn = name, ru_nargs = 1,
1080 ru_try = match_Word64ToInteger }
1081 rule_unop str name op
1082 = BuiltinRule { ru_name = fsLit str, ru_fn = name, ru_nargs = 1,
1083 ru_try = match_Integer_unop op }
1084 rule_bitInteger str name
1085 = BuiltinRule { ru_name = fsLit str, ru_fn = name, ru_nargs = 1,
1086 ru_try = match_IntToInteger_unop (bit . fromIntegral) }
1087 rule_binop str name op
1088 = BuiltinRule { ru_name = fsLit str, ru_fn = name, ru_nargs = 2,
1089 ru_try = match_Integer_binop op }
1090 rule_divop_both str name op
1091 = BuiltinRule { ru_name = fsLit str, ru_fn = name, ru_nargs = 2,
1092 ru_try = match_Integer_divop_both op }
1093 rule_divop_one str name op
1094 = BuiltinRule { ru_name = fsLit str, ru_fn = name, ru_nargs = 2,
1095 ru_try = match_Integer_divop_one op }
1096 rule_Int_binop str name op
1097 = BuiltinRule { ru_name = fsLit str, ru_fn = name, ru_nargs = 2,
1098 ru_try = match_Integer_Int_binop op }
1099 rule_binop_Prim str name op
1100 = BuiltinRule { ru_name = fsLit str, ru_fn = name, ru_nargs = 2,
1101 ru_try = match_Integer_binop_Prim op }
1102 rule_binop_Ordering str name op
1103 = BuiltinRule { ru_name = fsLit str, ru_fn = name, ru_nargs = 2,
1104 ru_try = match_Integer_binop_Ordering op }
1105 rule_encodeFloat str name op
1106 = BuiltinRule { ru_name = fsLit str, ru_fn = name, ru_nargs = 2,
1107 ru_try = match_Integer_Int_encodeFloat op }
1108 rule_decodeDouble str name
1109 = BuiltinRule { ru_name = fsLit str, ru_fn = name, ru_nargs = 1,
1110 ru_try = match_decodeDouble }
1111 rule_XToIntegerToX str name toIntegerName
1112 = BuiltinRule { ru_name = fsLit str, ru_fn = name, ru_nargs = 1,
1113 ru_try = match_XToIntegerToX toIntegerName }
1114 rule_smallIntegerTo str name primOp
1115 = BuiltinRule { ru_name = fsLit str, ru_fn = name, ru_nargs = 1,
1116 ru_try = match_smallIntegerTo primOp }
1117 rule_rationalTo str name mkLit
1118 = BuiltinRule { ru_name = fsLit str, ru_fn = name, ru_nargs = 2,
1119 ru_try = match_rationalTo mkLit }
1120
1121 ---------------------------------------------------
1122 -- The rule is this:
1123 -- unpackFoldrCString# "foo" c (unpackFoldrCString# "baz" c n)
1124 -- = unpackFoldrCString# "foobaz" c n
1125
1126 match_append_lit :: RuleFun
1127 match_append_lit _ id_unf _
1128 [ Type ty1
1129 , lit1
1130 , c1
1131 , Var unpk `App` Type ty2
1132 `App` lit2
1133 `App` c2
1134 `App` n
1135 ]
1136 | unpk `hasKey` unpackCStringFoldrIdKey &&
1137 c1 `cheapEqExpr` c2
1138 , Just (MachStr s1) <- exprIsLiteral_maybe id_unf lit1
1139 , Just (MachStr s2) <- exprIsLiteral_maybe id_unf lit2
1140 = ASSERT( ty1 `eqType` ty2 )
1141 Just (Var unpk `App` Type ty1
1142 `App` Lit (MachStr (s1 `BS.append` s2))
1143 `App` c1
1144 `App` n)
1145
1146 match_append_lit _ _ _ _ = Nothing
1147
1148 ---------------------------------------------------
1149 -- The rule is this:
1150 -- eqString (unpackCString# (Lit s1)) (unpackCString# (Lit s2)) = s1==s2
1151
1152 match_eq_string :: RuleFun
1153 match_eq_string _ id_unf _
1154 [Var unpk1 `App` lit1, Var unpk2 `App` lit2]
1155 | unpk1 `hasKey` unpackCStringIdKey
1156 , unpk2 `hasKey` unpackCStringIdKey
1157 , Just (MachStr s1) <- exprIsLiteral_maybe id_unf lit1
1158 , Just (MachStr s2) <- exprIsLiteral_maybe id_unf lit2
1159 = Just (if s1 == s2 then trueValBool else falseValBool)
1160
1161 match_eq_string _ _ _ _ = Nothing
1162
1163
1164 ---------------------------------------------------
1165 -- The rule is this:
1166 -- inline f_ty (f a b c) = <f's unfolding> a b c
1167 -- (if f has an unfolding, EVEN if it's a loop breaker)
1168 --
1169 -- It's important to allow the argument to 'inline' to have args itself
1170 -- (a) because its more forgiving to allow the programmer to write
1171 -- inline f a b c
1172 -- or inline (f a b c)
1173 -- (b) because a polymorphic f wll get a type argument that the
1174 -- programmer can't avoid
1175 --
1176 -- Also, don't forget about 'inline's type argument!
1177 match_inline :: [Expr CoreBndr] -> Maybe (Expr CoreBndr)
1178 match_inline (Type _ : e : _)
1179 | (Var f, args1) <- collectArgs e,
1180 Just unf <- maybeUnfoldingTemplate (realIdUnfolding f)
1181 -- Ignore the IdUnfoldingFun here!
1182 = Just (mkApps unf args1)
1183
1184 match_inline _ = Nothing
1185
1186
1187 -- See Note [magicDictId magic] in `basicTypes/MkId.hs`
1188 -- for a description of what is going on here.
1189 match_magicDict :: [Expr CoreBndr] -> Maybe (Expr CoreBndr)
1190 match_magicDict [Type _, Var wrap `App` Type a `App` Type _ `App` f, x, y ]
1191 | Just (fieldTy, _) <- splitFunTy_maybe $ dropForAlls $ idType wrap
1192 , Just (dictTy, _) <- splitFunTy_maybe fieldTy
1193 , Just dictTc <- tyConAppTyCon_maybe dictTy
1194 , Just (_,_,co) <- unwrapNewTyCon_maybe dictTc
1195 = Just
1196 $ f `App` Cast x (mkSymCo (mkUnbranchedAxInstCo Representational co [a] []))
1197 `App` y
1198
1199 match_magicDict _ = Nothing
1200
1201 -------------------------------------------------
1202 -- Integer rules
1203 -- smallInteger (79::Int#) = 79::Integer
1204 -- wordToInteger (79::Word#) = 79::Integer
1205 -- Similarly Int64, Word64
1206
1207 match_IntToInteger :: RuleFun
1208 match_IntToInteger = match_IntToInteger_unop id
1209
1210 match_WordToInteger :: RuleFun
1211 match_WordToInteger _ id_unf id [xl]
1212 | Just (MachWord x) <- exprIsLiteral_maybe id_unf xl
1213 = case splitFunTy_maybe (idType id) of
1214 Just (_, integerTy) ->
1215 Just (Lit (LitInteger x integerTy))
1216 _ ->
1217 panic "match_WordToInteger: Id has the wrong type"
1218 match_WordToInteger _ _ _ _ = Nothing
1219
1220 match_Int64ToInteger :: RuleFun
1221 match_Int64ToInteger _ id_unf id [xl]
1222 | Just (MachInt64 x) <- exprIsLiteral_maybe id_unf xl
1223 = case splitFunTy_maybe (idType id) of
1224 Just (_, integerTy) ->
1225 Just (Lit (LitInteger x integerTy))
1226 _ ->
1227 panic "match_Int64ToInteger: Id has the wrong type"
1228 match_Int64ToInteger _ _ _ _ = Nothing
1229
1230 match_Word64ToInteger :: RuleFun
1231 match_Word64ToInteger _ id_unf id [xl]
1232 | Just (MachWord64 x) <- exprIsLiteral_maybe id_unf xl
1233 = case splitFunTy_maybe (idType id) of
1234 Just (_, integerTy) ->
1235 Just (Lit (LitInteger x integerTy))
1236 _ ->
1237 panic "match_Word64ToInteger: Id has the wrong type"
1238 match_Word64ToInteger _ _ _ _ = Nothing
1239
1240 -------------------------------------------------
1241 match_Integer_convert :: Num a
1242 => (DynFlags -> a -> Expr CoreBndr)
1243 -> RuleFun
1244 match_Integer_convert convert dflags id_unf _ [xl]
1245 | Just (LitInteger x _) <- exprIsLiteral_maybe id_unf xl
1246 = Just (convert dflags (fromInteger x))
1247 match_Integer_convert _ _ _ _ _ = Nothing
1248
1249 match_Integer_unop :: (Integer -> Integer) -> RuleFun
1250 match_Integer_unop unop _ id_unf _ [xl]
1251 | Just (LitInteger x i) <- exprIsLiteral_maybe id_unf xl
1252 = Just (Lit (LitInteger (unop x) i))
1253 match_Integer_unop _ _ _ _ _ = Nothing
1254
1255 {- Note [Rewriting bitInteger]
1256
1257 For most types the bitInteger operation can be implemented in terms of shifts.
1258 The integer-gmp package, however, can do substantially better than this if
1259 allowed to provide its own implementation. However, in so doing it previously lost
1260 constant-folding (see Trac #8832). The bitInteger rule above provides constant folding
1261 specifically for this function.
1262
1263 There is, however, a bit of trickiness here when it comes to ranges. While the
1264 AST encodes all integers (even MachInts) as Integers, `bit` expects the bit
1265 index to be given as an Int. Hence we coerce to an Int in the rule definition.
1266 This will behave a bit funny for constants larger than the word size, but the user
1267 should expect some funniness given that they will have at very least ignored a
1268 warning in this case.
1269 -}
1270
1271 match_IntToInteger_unop :: (Integer -> Integer) -> RuleFun
1272 match_IntToInteger_unop unop _ id_unf fn [xl]
1273 | Just (MachInt x) <- exprIsLiteral_maybe id_unf xl
1274 = case splitFunTy_maybe (idType fn) of
1275 Just (_, integerTy) ->
1276 Just (Lit (LitInteger (unop x) integerTy))
1277 _ ->
1278 panic "match_IntToInteger_unop: Id has the wrong type"
1279 match_IntToInteger_unop _ _ _ _ _ = Nothing
1280
1281 match_Integer_binop :: (Integer -> Integer -> Integer) -> RuleFun
1282 match_Integer_binop binop _ id_unf _ [xl,yl]
1283 | Just (LitInteger x i) <- exprIsLiteral_maybe id_unf xl
1284 , Just (LitInteger y _) <- exprIsLiteral_maybe id_unf yl
1285 = Just (Lit (LitInteger (x `binop` y) i))
1286 match_Integer_binop _ _ _ _ _ = Nothing
1287
1288 -- This helper is used for the quotRem and divMod functions
1289 match_Integer_divop_both
1290 :: (Integer -> Integer -> (Integer, Integer)) -> RuleFun
1291 match_Integer_divop_both divop _ id_unf _ [xl,yl]
1292 | Just (LitInteger x t) <- exprIsLiteral_maybe id_unf xl
1293 , Just (LitInteger y _) <- exprIsLiteral_maybe id_unf yl
1294 , y /= 0
1295 , (r,s) <- x `divop` y
1296 = Just $ mkCoreUbxTup [t,t] [Lit (LitInteger r t), Lit (LitInteger s t)]
1297 match_Integer_divop_both _ _ _ _ _ = Nothing
1298
1299 -- This helper is used for the quot and rem functions
1300 match_Integer_divop_one :: (Integer -> Integer -> Integer) -> RuleFun
1301 match_Integer_divop_one divop _ id_unf _ [xl,yl]
1302 | Just (LitInteger x i) <- exprIsLiteral_maybe id_unf xl
1303 , Just (LitInteger y _) <- exprIsLiteral_maybe id_unf yl
1304 , y /= 0
1305 = Just (Lit (LitInteger (x `divop` y) i))
1306 match_Integer_divop_one _ _ _ _ _ = Nothing
1307
1308 match_Integer_Int_binop :: (Integer -> Int -> Integer) -> RuleFun
1309 match_Integer_Int_binop binop _ id_unf _ [xl,yl]
1310 | Just (LitInteger x i) <- exprIsLiteral_maybe id_unf xl
1311 , Just (MachInt y) <- exprIsLiteral_maybe id_unf yl
1312 = Just (Lit (LitInteger (x `binop` fromIntegral y) i))
1313 match_Integer_Int_binop _ _ _ _ _ = Nothing
1314
1315 match_Integer_binop_Prim :: (Integer -> Integer -> Bool) -> RuleFun
1316 match_Integer_binop_Prim binop dflags id_unf _ [xl, yl]
1317 | Just (LitInteger x _) <- exprIsLiteral_maybe id_unf xl
1318 , Just (LitInteger y _) <- exprIsLiteral_maybe id_unf yl
1319 = Just (if x `binop` y then trueValInt dflags else falseValInt dflags)
1320 match_Integer_binop_Prim _ _ _ _ _ = Nothing
1321
1322 match_Integer_binop_Ordering :: (Integer -> Integer -> Ordering) -> RuleFun
1323 match_Integer_binop_Ordering binop _ id_unf _ [xl, yl]
1324 | Just (LitInteger x _) <- exprIsLiteral_maybe id_unf xl
1325 , Just (LitInteger y _) <- exprIsLiteral_maybe id_unf yl
1326 = Just $ case x `binop` y of
1327 LT -> ltVal
1328 EQ -> eqVal
1329 GT -> gtVal
1330 match_Integer_binop_Ordering _ _ _ _ _ = Nothing
1331
1332 match_Integer_Int_encodeFloat :: RealFloat a
1333 => (a -> Expr CoreBndr)
1334 -> RuleFun
1335 match_Integer_Int_encodeFloat mkLit _ id_unf _ [xl,yl]
1336 | Just (LitInteger x _) <- exprIsLiteral_maybe id_unf xl
1337 , Just (MachInt y) <- exprIsLiteral_maybe id_unf yl
1338 = Just (mkLit $ encodeFloat x (fromInteger y))
1339 match_Integer_Int_encodeFloat _ _ _ _ _ = Nothing
1340
1341 ---------------------------------------------------
1342 -- constant folding for Float/Double
1343 --
1344 -- This turns
1345 -- rationalToFloat n d
1346 -- into a literal Float, and similarly for Doubles.
1347 --
1348 -- it's important to not match d == 0, because that may represent a
1349 -- literal "0/0" or similar, and we can't produce a literal value for
1350 -- NaN or +-Inf
1351 match_rationalTo :: RealFloat a
1352 => (a -> Expr CoreBndr)
1353 -> RuleFun
1354 match_rationalTo mkLit _ id_unf _ [xl, yl]
1355 | Just (LitInteger x _) <- exprIsLiteral_maybe id_unf xl
1356 , Just (LitInteger y _) <- exprIsLiteral_maybe id_unf yl
1357 , y /= 0
1358 = Just (mkLit (fromRational (x % y)))
1359 match_rationalTo _ _ _ _ _ = Nothing
1360
1361 match_decodeDouble :: RuleFun
1362 match_decodeDouble _ id_unf fn [xl]
1363 | Just (MachDouble x) <- exprIsLiteral_maybe id_unf xl
1364 = case splitFunTy_maybe (idType fn) of
1365 Just (_, res)
1366 | Just [_lev1, _lev2, integerTy, intHashTy] <- tyConAppArgs_maybe res
1367 -> case decodeFloat (fromRational x :: Double) of
1368 (y, z) ->
1369 Just $ mkCoreUbxTup [integerTy, intHashTy]
1370 [Lit (LitInteger y integerTy),
1371 Lit (MachInt (toInteger z))]
1372 _ ->
1373 pprPanic "match_decodeDouble: Id has the wrong type"
1374 (ppr fn <+> dcolon <+> ppr (idType fn))
1375 match_decodeDouble _ _ _ _ = Nothing
1376
1377 match_XToIntegerToX :: Name -> RuleFun
1378 match_XToIntegerToX n _ _ _ [App (Var x) y]
1379 | idName x == n
1380 = Just y
1381 match_XToIntegerToX _ _ _ _ _ = Nothing
1382
1383 match_smallIntegerTo :: PrimOp -> RuleFun
1384 match_smallIntegerTo primOp _ _ _ [App (Var x) y]
1385 | idName x == smallIntegerName
1386 = Just $ App (Var (mkPrimOpId primOp)) y
1387 match_smallIntegerTo _ _ _ _ _ = Nothing
1388
1389
1390
1391 --------------------------------------------------------
1392 -- Constant folding through case-expressions
1393 --
1394 -- cf Scrutinee Constant Folding in simplCore/SimplUtils
1395 --------------------------------------------------------
1396
1397 -- | Match the scrutinee of a case and potentially return a new scrutinee and a
1398 -- function to apply to each literal alternative.
1399 caseRules :: DynFlags
1400 -> CoreExpr -- Scrutinee
1401 -> Maybe ( CoreExpr -- New scrutinee
1402 , AltCon -> AltCon -- How to fix up the alt pattern
1403 , Id -> CoreExpr) -- How to reconstruct the original scrutinee
1404 -- from the new case-binder
1405 -- e.g case e of b {
1406 -- ...;
1407 -- con bs -> rhs;
1408 -- ... }
1409 -- ==>
1410 -- case e' of b' {
1411 -- ...;
1412 -- fixup_altcon[con] bs -> let b = mk_orig[b] in rhs;
1413 -- ... }
1414
1415 caseRules dflags (App (App (Var f) v) (Lit l)) -- v `op` x#
1416 | Just op <- isPrimOpId_maybe f
1417 , Just x <- isLitValue_maybe l
1418 , Just adjust_lit <- adjustDyadicRight op x
1419 = Just (v, tx_lit_con dflags adjust_lit
1420 , \v -> (App (App (Var f) (Var v)) (Lit l)))
1421
1422 caseRules dflags (App (App (Var f) (Lit l)) v) -- x# `op` v
1423 | Just op <- isPrimOpId_maybe f
1424 , Just x <- isLitValue_maybe l
1425 , Just adjust_lit <- adjustDyadicLeft x op
1426 = Just (v, tx_lit_con dflags adjust_lit
1427 , \v -> (App (App (Var f) (Lit l)) (Var v)))
1428
1429
1430 caseRules dflags (App (Var f) v ) -- op v
1431 | Just op <- isPrimOpId_maybe f
1432 , Just adjust_lit <- adjustUnary op
1433 = Just (v, tx_lit_con dflags adjust_lit
1434 , \v -> App (Var f) (Var v))
1435
1436 -- See Note [caseRules for tagToEnum]
1437 caseRules dflags (App (App (Var f) type_arg) v)
1438 | Just TagToEnumOp <- isPrimOpId_maybe f
1439 = Just (v, tx_con_tte dflags
1440 , \v -> (App (App (Var f) type_arg) (Var v)))
1441
1442 -- See Note [caseRules for dataToTag]
1443 caseRules _ (App (App (Var f) (Type ty)) v) -- dataToTag x
1444 | Just DataToTagOp <- isPrimOpId_maybe f
1445 = Just (v, tx_con_dtt ty
1446 , \v -> App (App (Var f) (Type ty)) (Var v))
1447
1448 caseRules _ _ = Nothing
1449
1450
1451 tx_lit_con :: DynFlags -> (Integer -> Integer) -> AltCon -> AltCon
1452 tx_lit_con _ _ DEFAULT = DEFAULT
1453 tx_lit_con dflags adjust (LitAlt l) = LitAlt (mapLitValue dflags adjust l)
1454 tx_lit_con _ _ alt = pprPanic "caseRules" (ppr alt)
1455 -- NB: mapLitValue uses mkMachIntWrap etc, to ensure that the
1456 -- literal alternatives remain in Word/Int target ranges
1457 -- (See Note [Word/Int underflow/overflow] in Literal and #13172).
1458
1459 adjustDyadicRight :: PrimOp -> Integer -> Maybe (Integer -> Integer)
1460 -- Given (x `op` lit) return a function 'f' s.t. f (x `op` lit) = x
1461 adjustDyadicRight op lit
1462 = case op of
1463 WordAddOp -> Just (\y -> y-lit )
1464 IntAddOp -> Just (\y -> y-lit )
1465 WordSubOp -> Just (\y -> y+lit )
1466 IntSubOp -> Just (\y -> y+lit )
1467 XorOp -> Just (\y -> y `xor` lit)
1468 XorIOp -> Just (\y -> y `xor` lit)
1469 _ -> Nothing
1470
1471 adjustDyadicLeft :: Integer -> PrimOp -> Maybe (Integer -> Integer)
1472 -- Given (lit `op` x) return a function 'f' s.t. f (lit `op` x) = x
1473 adjustDyadicLeft lit op
1474 = case op of
1475 WordAddOp -> Just (\y -> y-lit )
1476 IntAddOp -> Just (\y -> y-lit )
1477 WordSubOp -> Just (\y -> lit-y )
1478 IntSubOp -> Just (\y -> lit-y )
1479 XorOp -> Just (\y -> y `xor` lit)
1480 XorIOp -> Just (\y -> y `xor` lit)
1481 _ -> Nothing
1482
1483
1484 adjustUnary :: PrimOp -> Maybe (Integer -> Integer)
1485 -- Given (op x) return a function 'f' s.t. f (op x) = x
1486 adjustUnary op
1487 = case op of
1488 NotOp -> Just (\y -> complement y)
1489 NotIOp -> Just (\y -> complement y)
1490 IntNegOp -> Just (\y -> negate y )
1491 _ -> Nothing
1492
1493 tx_con_tte :: DynFlags -> AltCon -> AltCon
1494 tx_con_tte _ DEFAULT = DEFAULT
1495 tx_con_tte dflags (DataAlt dc)
1496 | tag == 0 = DEFAULT -- See Note [caseRules for tagToEnum]
1497 | otherwise = LitAlt (mkMachInt dflags (toInteger tag))
1498 where
1499 tag = dataConTagZ dc
1500 tx_con_tte _ alt = pprPanic "caseRules" (ppr alt)
1501
1502 tx_con_dtt :: Type -> AltCon -> AltCon
1503 tx_con_dtt _ DEFAULT = DEFAULT
1504 tx_con_dtt ty (LitAlt (MachInt i)) = DataAlt (get_con ty (fromInteger i))
1505 tx_con_dtt _ alt = pprPanic "caseRules" (ppr alt)
1506
1507 get_con :: Type -> ConTagZ -> DataCon
1508 get_con ty tag = tyConDataCons (tyConAppTyCon ty) !! tag
1509
1510 {- Note [caseRules for tagToEnum]
1511 ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
1512 We want to transform
1513 case tagToEnum x of
1514 False -> e1
1515 True -> e2
1516 into
1517 case x of
1518 0# -> e1
1519 1# -> e1
1520
1521 This rule eliminates a lot of boilerplate. For
1522 if (x>y) then e1 else e2
1523 we generate
1524 case tagToEnum (x ># y) of
1525 False -> e2
1526 True -> e1
1527 and it is nice to then get rid of the tagToEnum.
1528
1529 NB: in SimplUtils, where we invoke caseRules,
1530 we convert that 0# to DEFAULT
1531
1532 Note [caseRules for dataToTag]
1533 ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
1534 We want to transform
1535 case dataToTag x of
1536 DEFAULT -> e1
1537 1# -> e2
1538 into
1539 case x of
1540 DEFAULT -> e1
1541 (:) _ _ -> e2
1542
1543 Note the need for some wildcard binders in
1544 the 'cons' case.
1545 -}