emitCmmLitSwitch could be better
This is a spin off #10124. While looking at the code generated for
f :: Int -> Bool f a = case a of 1 -> True 2 -> True 3 -> True 4 -> True 8 -> True 9 -> True 11 -> True 19 -> True _ -> False
I noticed this Cmm:
c2tI: if (%MO_S_Lt_W64(_s2sJ::I64, 3)) goto c2tw; else goto c2tx; c2tw: if (%MO_S_Lt_W64(_s2sJ::I64, 2)) goto c2tq; else goto c2tr; c2tq: if (_s2sJ::I64 != 1) goto c2tg; else goto c2th; c2tr: if (_s2sJ::I64 != 2) goto c2tg; else goto c2th;
Note that when
c2tr is reached, we know 2 ≤ _s2sJ < 3, so _s2sJ already is 2, and this check is redundant.
emitCmmLitSwitch does not take that into account, probably because it also needs to work for floats.
I wonder if it isn’t a bit shady to use an if-then-else tree for floats. Maybe for float types, a sequence of equality tests is more suitable. For all other cases, the code generator could make use of "2 ≤ x < 3 ⇒ x = 2" and skip one check.