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
- 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 ofand
primops with a constant. - Fix Pattern-Match checker to not require a DEFAULT alternative and to not insert a
patError
if it is missing.