summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorSylvain Henry <sylvain@haskus.fr>2021-05-04 15:10:04 +0200
committerMarge Bot <ben+marge-bot@smart-cactus.org>2021-10-05 14:35:17 -0400
commit11240b7427c54c3233aa3cce5adc3a5209df8031 (patch)
treeb6d5198009cb222c3a720efbc918c9489372840d
parent52400ebb537504eb0f8e0f4678f3f721b2fbc2d9 (diff)
downloadhaskell-11240b7427c54c3233aa3cce5adc3a5209df8031.tar.gz
Constant folding for (.&.) maxBound (#20448)
-rw-r--r--compiler/GHC/Core/Opt/ConstantFold.hs6
-rw-r--r--testsuite/tests/numeric/should_compile/T20448.hs36
-rw-r--r--testsuite/tests/numeric/should_compile/T20448.stderr27
-rw-r--r--testsuite/tests/numeric/should_compile/all.T1
4 files changed, 70 insertions, 0 deletions
diff --git a/compiler/GHC/Core/Opt/ConstantFold.hs b/compiler/GHC/Core/Opt/ConstantFold.hs
index 892cca7e4f..c31ed204bf 100644
--- a/compiler/GHC/Core/Opt/ConstantFold.hs
+++ b/compiler/GHC/Core/Opt/ConstantFold.hs
@@ -157,6 +157,7 @@ primOpRules nm = \case
Word8AndOp -> mkPrimOpRule nm 2 [ binaryLit (word8Op2 (.&.))
, idempotent
, zeroElem
+ , identity (mkLitWord8 0xFF)
, sameArgIdempotentCommut Word8AndOp
, andFoldingRules word8Ops
]
@@ -230,6 +231,7 @@ primOpRules nm = \case
Word16AndOp -> mkPrimOpRule nm 2 [ binaryLit (word16Op2 (.&.))
, idempotent
, zeroElem
+ , identity (mkLitWord16 0xFFFF)
, sameArgIdempotentCommut Word16AndOp
, andFoldingRules word16Ops
]
@@ -303,6 +305,7 @@ primOpRules nm = \case
Word32AndOp -> mkPrimOpRule nm 2 [ binaryLit (word32Op2 (.&.))
, idempotent
, zeroElem
+ , identity (mkLitWord32 0xFFFFFFFF)
, sameArgIdempotentCommut Word32AndOp
, andFoldingRules word32Ops
]
@@ -375,6 +378,7 @@ primOpRules nm = \case
Word64AndOp -> mkPrimOpRule nm 2 [ binaryLit (word64Op2 (.&.))
, idempotent
, zeroElem
+ , identity (mkLitWord64 0xFFFFFFFFFFFFFFFF)
, sameArgIdempotentCommut Word64AndOp
, andFoldingRules word64Ops
]
@@ -456,6 +460,7 @@ primOpRules nm = \case
IntAndOp -> mkPrimOpRule nm 2 [ binaryLit (intOp2 (.&.))
, idempotent
, zeroElem
+ , identityPlatform (\p -> mkLitInt p (-1))
, sameArgIdempotentCommut IntAndOp
, andFoldingRules intOps
]
@@ -507,6 +512,7 @@ primOpRules nm = \case
WordAndOp -> mkPrimOpRule nm 2 [ binaryLit (wordOp2 (.&.))
, idempotent
, zeroElem
+ , identityPlatform (\p -> mkLitWord p (platformMaxWord p))
, sameArgIdempotentCommut WordAndOp
, andFoldingRules wordOps
]
diff --git a/testsuite/tests/numeric/should_compile/T20448.hs b/testsuite/tests/numeric/should_compile/T20448.hs
new file mode 100644
index 0000000000..1f98a3a826
--- /dev/null
+++ b/testsuite/tests/numeric/should_compile/T20448.hs
@@ -0,0 +1,36 @@
+module Test where
+
+import Data.Word
+import Data.Int
+import Data.Bits
+
+w8 :: Word8 -> Word8
+w8 x = x .&. 0xFF
+
+w16 :: Word16 -> Word16
+w16 x = x .&. 0xFFFF
+
+w32 :: Word32 -> Word32
+w32 x = x .&. 0xFFFFFFFF
+
+w64 :: Word64 -> Word64
+w64 x = x .&. 0xFFFFFFFFFFFFFFFF
+
+w :: Word -> Word
+w x = x .&. maxBound
+
+
+i8 :: Int8 -> Int8
+i8 x = x .&. (-1)
+
+i16 :: Int16 -> Int16
+i16 x = x .&. (-1)
+
+i32 :: Int32 -> Int32
+i32 x = x .&. (-1)
+
+i64 :: Int64 -> Int64
+i64 x = x .&. (-1)
+
+i :: Int -> Int
+i x = x .&. (-1)
diff --git a/testsuite/tests/numeric/should_compile/T20448.stderr b/testsuite/tests/numeric/should_compile/T20448.stderr
new file mode 100644
index 0000000000..5cbea14d1f
--- /dev/null
+++ b/testsuite/tests/numeric/should_compile/T20448.stderr
@@ -0,0 +1,27 @@
+
+==================== Tidy Core ====================
+Result size of Tidy Core
+ = {terms: 30, types: 30, coercions: 0, joins: 0/0}
+
+w8 = \ x -> x
+
+w16 = \ x -> x
+
+w32 = \ x -> x
+
+w64 = \ x -> x
+
+w = \ x -> x
+
+i8 = \ x -> x
+
+i16 = \ x -> x
+
+i32 = \ x -> x
+
+i64 = \ x -> x
+
+i = \ x -> x
+
+
+
diff --git a/testsuite/tests/numeric/should_compile/all.T b/testsuite/tests/numeric/should_compile/all.T
index 59b66f8d5f..087eb7b358 100644
--- a/testsuite/tests/numeric/should_compile/all.T
+++ b/testsuite/tests/numeric/should_compile/all.T
@@ -16,3 +16,4 @@ test('T20376', normal, compile, ['-ddump-simpl -O -dsuppress-all -dsuppress-uniq
test('T20374', normal, compile, ['-ddump-simpl -O -dsuppress-all -dno-typeable-binds -dsuppress-uniques'])
test('T19769', normal, compile, ['-ddump-simpl -O -dsuppress-all -dno-typeable-binds -dsuppress-uniques'])
test('T20347', normal, compile, ['-ddump-simpl -O -dsuppress-all -dno-typeable-binds -dsuppress-uniques'])
+test('T20448', normal, compile, ['-ddump-simpl -O -dsuppress-all -dno-typeable-binds -dsuppress-uniques'])