Skip to content

Optimization for binary masks

Motivation

GHC doesn't optimize binary masks as in this example:

foo :: Int -> Int
foo x = case x .&. 0x3 of
    0 -> 0
    1 -> 1
    2 -> 2
    3 -> 3
    4 -> 4 -- unreachable branch but not removed
    _ -> undefined -- required for the PM checker but unreachable too

Proposal

  1. In Core: detect ranges in these cases and remove unreachable branches. To fix this example, we should just have to enhance GHC.Core.Opt.ConstantFold.caseRules to detect (potentially nested) applications of and primops with a constant.
  2. Fix Pattern-Match checker to not require a DEFAULT alternative and to not insert a patError if it is missing.
To upload designs, you'll need to enable LFS and have an admin enable hashed storage. More information