Compiler optimizations questions for ghc 6.10...

Krasimir Angelov kr.angelov at
Thu Feb 19 18:20:33 EST 2009

Oh. I looked at the primops.txt.pp for something suspicious but I
didn't checked the actual implementation of quot. I thought that quot
calls quotInt# directly. When I use quotInt in the code I can get the
real idiv assembly instruction. Still the code generated by GHC is
strange it doesn't throw any exception actually. It just evaluates the
same expression but with the constant maxBound.

On Thu, Feb 19, 2009 at 11:19 PM, Max Bolingbroke
<batterseapower at> wrote:
> 2009/2/19 Krasimir Angelov <kr.angelov at>:
>> I was surprised to see this case expression:
>>>        case GHC.Prim.-# 9223372036854775807 ipv_s1bD
>>>        of wild2_a1xi [ALWAYS Just L] {
>> What is the purpose to compare the value with maxBound before the
>> division? The case expression doesn't disappear even if I use quot
>> instead of div.
> Have a look at this snippet of the base library, file GHC/Real.lhs:
>    a `quot` b
>     | b == 0                     = divZeroError
>     | a == minBound && b == (-1) = overflowError
>     | otherwise                  =  a `quotInt` b
> quotInt is defined in GHC/Base.lhs as:
> (I# x) `quotInt`  (I# y) = I# (x `quotInt#` y)
> And quotInt# is a primitive, which I guess is implemented via machine
> division (though I don't work on the codegen stuff at all) - so your
> offending case must come from those tests in GHC/Real.lhs.
> In general, if you want to answer questions like this you can usually
> find the answer by looking at the base code: it's all in Haskell, so
> very readable! You can get it online at
> Cheers,
> Max

More information about the Glasgow-haskell-users mailing list