[GHC] #3744: Comparisons against minBound/maxBound not optimised

GHC trac at galois.com
Thu Dec 10 22:26:01 EST 2009


#3744: Comparisons against minBound/maxBound not optimised
---------------------------------+------------------------------------------
    Reporter:  rl                |       Owner:                         
        Type:  bug               |      Status:  new                    
    Priority:  normal            |   Component:  Compiler               
     Version:  6.13              |    Keywords:                         
          Os:  Unknown/Multiple  |    Testcase:                         
Architecture:  Unknown/Multiple  |     Failure:  Runtime performance bug
---------------------------------+------------------------------------------
 {{{
 foo :: Int -> Bool
 foo n = n < minBound || n > maxBound
 }}}

 GHC retains both comparisons even though they are guaranteed to be False.
 This also happens for other integral types. The optimisation is fairly
 easy to implement for `Int` and `Word` (only requires some plumbing in
 !PrelRules) but it's not clear what to do about smaller integral types.
 For `Int64` and `Word64`, GHC doesn't even inline `minBound` and
 `maxBound`:

 {{{
 T.$wfoo :: GHC.Prim.Int64# -> GHC.Bool.Bool
 T.$wfoo =
   \ (ww_ss5 :: GHC.Prim.Int64#) ->
     case GHC.Int.$fBoundedInt64_$cminBound
     of _ { GHC.Int.I64# y#_are ->
     case {__ccall hs_ltInt64 GHC.Prim.Int64#
                     -> GHC.Prim.Int64#
                     -> GHC.Prim.State# GHC.Prim.RealWorld
                     -> (# GHC.Prim.State# GHC.Prim.RealWorld,
 GHC.Prim.Int# #)}_arD
            ww_ss5 y#_are GHC.Prim.realWorld#
     of _ { (# _, ds3_arH #) ->
     case ds3_arH of _ {
       __DEFAULT -> GHC.Bool.True;
       0 ->
         case GHC.Int.$fBoundedInt64_$cmaxBound
         of _ { GHC.Int.I64# y#1_ar4 ->
         GHC.IntWord64.gtInt64# ww_ss5 y#1_ar4
         }
     }
     }
     }

 T.foo :: GHC.Int.Int64 -> GHC.Bool.Bool
 T.foo =
   \ (w_ss3 :: GHC.Int.Int64) ->
     case w_ss3 of _ { GHC.Int.I64# ww_ss5 -> T.$wfoo ww_ss5 }

 }}}

-- 
Ticket URL: <http://hackage.haskell.org/trac/ghc/ticket/3744>
GHC <http://www.haskell.org/ghc/>
The Glasgow Haskell Compiler


More information about the Glasgow-haskell-bugs mailing list