#3744: Comparisons against minBound/maxBound not optimised
---------------------------------+------------------------------------------
    Reporter:  rl                |        Owner:                         
        Type:  bug               |       Status:  new                    
    Priority:  normal            |    Milestone:  7.0.1                  
   Component:  Compiler          |      Version:  6.13                   
    Keywords:                    |     Testcase:                         
   Blockedby:                    |   Difficulty:                         
          Os:  Unknown/Multiple  |     Blocking:                         
Architecture:  Unknown/Multiple  |      Failure:  Runtime performance bug
---------------------------------+------------------------------------------
Changes (by michalt):

 * cc: michal.terep...@… (added)


Comment:

 I've created a patch with rules for PrelRules.lhs that detect comparisons
 with
 minBound and maxBound that are always true or always false (like in the
 above
 examples). And it seems to work just fine:
 {{{
 foo1, foo2, foo3, foo4 :: Int -> Bool

 foo1 n = n < minBound

 foo2 n = n > maxBound

 foo3 n = n >= minBound

 foo4 n = n <= maxBound
 }}}
 gives now
 {{{
 Test.foo1 =
   \ (n_aaD :: GHC.Types.Int) ->
     case n_aaD of _ { GHC.Types.I# _ -> GHC.Bool.False }

 Test.foo2 =
   \ (n_ag0 :: GHC.Types.Int) ->
     case n_ag0 of _ { GHC.Types.I# _ -> GHC.Bool.False }

 Test.foo3 =
   \ (n_ag1 :: GHC.Types.Int) ->
     case n_ag1 of _ { GHC.Types.I# _ -> GHC.Bool.True }

 Test.foo4 =
   \ (n_ag2 :: GHC.Types.Int) ->
     case n_ag2 of _ { GHC.Types.I# _ -> GHC.Bool.True }
 }}}
 This works fine also for Char, and Word, but interestingly in case of
 Int64 only
 maxBound is inlined, but not minBound (and so foo2 and foo4 are optimised
 but
 not foo1 and foo3), whereas in case of Word64 the opposite happens. For
 example
 by changing the type to Int64 in the above code we have:
 {{{
 Test.foo1 =
   \ (n_aaF :: GHC.Int.Int64) ->
     case n_aaF of _ { GHC.Int.I64# a1_ar3 ->
     case GHC.Int.$fBoundedInt64_$cminBound
     of _ { GHC.Int.I64# b1_ar7 ->
     GHC.Prim.<# a1_ar3 b1_ar7
     }
     }

 Test.foo2 =
   \ (n_ag2 :: GHC.Int.Int64) ->
     case n_ag2 of _ { GHC.Int.I64# _ -> GHC.Bool.False }

 Test.foo3 =
   \ (n_ag3 :: GHC.Int.Int64) ->
     case n_ag3 of _ { GHC.Int.I64# a1_arn ->
     case GHC.Int.$fBoundedInt64_$cminBound
     of _ { GHC.Int.I64# b1_arr ->
     GHC.Prim.>=# a1_arn b1_arr
     }
     }

 Test.foo4 =
   \ (n_ag4 :: GHC.Int.Int64) ->
     case n_ag4 of _ { GHC.Int.I64# _ -> GHC.Bool.True }
 }}}
 Anyway, I'll attach the patch in a second. And please let me know what do
 you
 think about it (especially since I don't really know a lot about GHC
 internals).

-- 
Ticket URL: <http://hackage.haskell.org/trac/ghc/ticket/3744#comment:5>
GHC <http://www.haskell.org/ghc/>
The Glasgow Haskell Compiler
_______________________________________________
Glasgow-haskell-bugs mailing list
[email protected]
http://www.haskell.org/mailman/listinfo/glasgow-haskell-bugs

Reply via email to