+ | otherwise = trueVal
+
+
+-- | Check if there is comparison with minBound or maxBound, that is
+-- always true or false. For instance, an Int cannot be smaller than its
+-- minBound, so we can replace such comparison with False.
+boundsCmp :: Name -> Comparison -> [CoreRule]
+boundsCmp op_name op = [ rule ]
+ where
+ rule = BuiltinRule
+ { ru_name = occNameFS (nameOccName op_name)
+ `appendFS` (fsLit "min/maxBound")
+ , ru_fn = op_name
+ , ru_nargs = 2
+ , ru_try = rule_fn
+ }
+ rule_fn _ [a, b] = mkRuleFn op a b
+ rule_fn _ _ = Nothing
+
+data Comparison = Gt | Ge | Lt | Le
+
+mkRuleFn :: Comparison -> CoreExpr -> CoreExpr -> Maybe CoreExpr
+mkRuleFn Gt (Lit lit) _ | isMinBound lit = Just falseVal
+mkRuleFn Le (Lit lit) _ | isMinBound lit = Just trueVal
+mkRuleFn Ge _ (Lit lit) | isMinBound lit = Just trueVal
+mkRuleFn Lt _ (Lit lit) | isMinBound lit = Just falseVal
+mkRuleFn Ge (Lit lit) _ | isMaxBound lit = Just trueVal
+mkRuleFn Lt (Lit lit) _ | isMaxBound lit = Just falseVal
+mkRuleFn Gt _ (Lit lit) | isMaxBound lit = Just falseVal
+mkRuleFn Le _ (Lit lit) | isMaxBound lit = Just trueVal
+mkRuleFn _ _ _ = Nothing
+
+isMinBound :: Literal -> Bool
+isMinBound (MachChar c) = c == minBound
+isMinBound (MachInt i) = i == toInteger (minBound :: Int)
+isMinBound (MachInt64 i) = i == toInteger (minBound :: Int64)
+isMinBound (MachWord i) = i == toInteger (minBound :: Word)
+isMinBound (MachWord64 i) = i == toInteger (minBound :: Word64)
+isMinBound _ = False
+
+isMaxBound :: Literal -> Bool
+isMaxBound (MachChar c) = c == maxBound
+isMaxBound (MachInt i) = i == toInteger (maxBound :: Int)
+isMaxBound (MachInt64 i) = i == toInteger (maxBound :: Int64)
+isMaxBound (MachWord i) = i == toInteger (maxBound :: Word)
+isMaxBound (MachWord64 i) = i == toInteger (maxBound :: Word64)
+isMaxBound _ = False
+