| ... |
... |
@@ -1425,16 +1425,16 @@ getRegister' platform is32Bit (CmmMachOp mop [x]) = do -- unary MachOps |
|
1425
|
1425
|
-- See Note [Bit-test instructions].
|
|
1426
|
1426
|
getRegister' platform is32Bit (CmmMachOp (MO_And w) [x, y])
|
|
1427
|
1427
|
| bitTestOpWidthOK is32Bit w
|
|
1428
|
|
- , Just (opnd, ix) <- clearBitArgs_maybe platform w x y
|
|
1429
|
|
- = genBitTestCode (intFormat w) BTR opnd ix
|
|
|
1428
|
+ , Just (opnd, ix) <- matchEitherArg (invSingleBitMask_maybe platform w) x y
|
|
|
1429
|
+ = genBitModifyCode (intFormat w) BTR opnd ix
|
|
1430
|
1430
|
getRegister' platform is32Bit (CmmMachOp (MO_Or w) [x, y])
|
|
1431
|
1431
|
| bitTestOpWidthOK is32Bit w
|
|
1432
|
|
- , Just (opnd, ix) <- setBitArgs_maybe platform w x y
|
|
1433
|
|
- = genBitTestCode (intFormat w) BTS opnd ix
|
|
|
1432
|
+ , Just (opnd, ix) <- matchEitherArg (singleBitMask_maybe platform w) x y
|
|
|
1433
|
+ = genBitModifyCode (intFormat w) BTS opnd ix
|
|
1434
|
1434
|
getRegister' platform is32Bit (CmmMachOp (MO_Xor w) [x, y])
|
|
1435
|
1435
|
| bitTestOpWidthOK is32Bit w
|
|
1436
|
|
- , Just (opnd, ix) <- setBitArgs_maybe platform w x y
|
|
1437
|
|
- = genBitTestCode (intFormat w) BTC opnd ix
|
|
|
1436
|
+ , Just (opnd, ix) <- matchEitherArg (singleBitMask_maybe platform w) x y
|
|
|
1437
|
+ = genBitModifyCode (intFormat w) BTC opnd ix
|
|
1438
|
1438
|
|
|
1439
|
1439
|
getRegister' platform is32Bit (CmmMachOp mop [x, y]) = do -- dyadic MachOps
|
|
1440
|
1440
|
sse4_1 <- x86Feature x86Sse4_1Enabled
|
| ... |
... |
@@ -3939,6 +3939,24 @@ condIntCode' platform cond (CmmLoad x ty _) (CmmLit lit) |
|
3939
|
3939
|
--
|
|
3940
|
3940
|
return (CondCode False cond code)
|
|
3941
|
3941
|
|
|
|
3942
|
+-- single-bit test, e.g. (x & (1 << i)) != 0
|
|
|
3943
|
+-- see Note [Bit-test instructions]
|
|
|
3944
|
+condIntCode' platform cond (CmmMachOp (MO_And w) [x, y]) (CmmLit (CmmInt 0 _))
|
|
|
3945
|
+ | Just bt_cond <- bitTestCond cond
|
|
|
3946
|
+ , bitTestOpWidthOK (target32Bit platform) w
|
|
|
3947
|
+ , Just (opnd, ix) <- matchEitherArg (singleBitMask_maybe platform w) x y
|
|
|
3948
|
+ , let fmt = intFormat w
|
|
|
3949
|
+ = case ix of
|
|
|
3950
|
+ BitIndexImm i -> do
|
|
|
3951
|
+ (x_reg, x_code) <- getSomeReg opnd
|
|
|
3952
|
+ return $ CondCode False bt_cond $
|
|
|
3953
|
+ x_code `snocOL` BT fmt (OpImm (ImmInt i)) (OpReg x_reg)
|
|
|
3954
|
+ BitIndexReg i -> do
|
|
|
3955
|
+ (i_reg, i_code) <- getNonClobberedReg i
|
|
|
3956
|
+ (x_reg, x_code) <- getSomeReg opnd
|
|
|
3957
|
+ return $ CondCode False bt_cond $
|
|
|
3958
|
+ i_code `appOL` x_code `snocOL` BT fmt (OpReg i_reg) (OpReg x_reg)
|
|
|
3959
|
+
|
|
3942
|
3960
|
-- anything vs zero, using a mask
|
|
3943
|
3961
|
-- TODO: Add some sanity checking!!!!
|
|
3944
|
3962
|
condIntCode' platform cond (CmmMachOp (MO_And _) [x,o2]) (CmmLit (CmmInt 0 ty))
|
| ... |
... |
@@ -5883,17 +5901,18 @@ genTrivialCode rep instr a b = do |
|
5883
|
5901
|
|
|
5884
|
5902
|
{- Note [Bit-test instructions]
|
|
5885
|
5903
|
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
|
|
5886
|
|
-x86 has dedicated instructions for clearing (btr), setting (bts) and
|
|
5887
|
|
-complementing (btc) a single bit whose index is given in a register. We use
|
|
5888
|
|
-them for Cmm patterns such as
|
|
|
5904
|
+x86 has dedicated instructions for testing (bt), clearing (btr), setting
|
|
|
5905
|
+(bts) and complementing (btc) a single bit whose index is given in a
|
|
|
5906
|
+register. We use them for Cmm patterns such as
|
|
5889
|
5907
|
|
|
5890
|
|
- x & ~(1 << i) ==> btr i, x (#25233)
|
|
|
5908
|
+ (x & (1 << i)) != 0 ==> bt i, x; jc
|
|
|
5909
|
+ x & ~(1 << i) ==> btr i, x
|
|
5891
|
5910
|
|
|
5892
|
|
-replacing a mov/shl/not/and sequence with a single instruction. The
|
|
5893
|
|
-shift-count register operand of shl is masked modulo the operand width, and
|
|
5894
|
|
-the bit-offset register operand of btr/bts/btc is masked the same way, so
|
|
5895
|
|
-the replacement is faithful even for out-of-range i (where the Cmm shift is
|
|
5896
|
|
-in any case undefined).
|
|
|
5911
|
+replacing a mov/shl/and/test or mov/shl/not/and sequence with a single
|
|
|
5912
|
+instruction. The shift-count register operand of shl is masked modulo the
|
|
|
5913
|
+operand width, and the bit-offset register operand of the bit-test
|
|
|
5914
|
+instructions is masked the same way, so the replacement is faithful even for
|
|
|
5915
|
+out-of-range i (where the Cmm shift is in any case undefined).
|
|
5897
|
5916
|
|
|
5898
|
5917
|
The bit-offset operand of these instructions must be an immediate or a
|
|
5899
|
5918
|
register. When the bit index is a literal, no shift reaches the NCG:
|
| ... |
... |
@@ -5901,10 +5920,11 @@ constant folding has already turned the whole mask into a literal. If that |
|
5901
|
5920
|
mask fits in an imm32, we keep the ordinary and/or/xor with an immediate:
|
|
5902
|
5921
|
it has the same latency and better throughput (more execution ports) than
|
|
5903
|
5922
|
the bit-test instructions, and at worst two bytes of extra code size for bit
|
|
5904
|
|
-indices 7..30.
|
|
|
5923
|
+indices 7..30. Likewise a single-bit test keeps 'test' with an immediate:
|
|
|
5924
|
+test+jcc macro-fuse into one uop, bt+jcc do not.
|
|
5905
|
5925
|
But a W64 mask touching the upper bits, e.g. ~(1 << 40), would have to be moved
|
|
5906
|
5926
|
into a register first. For such masks we recognise the folded literal itself
|
|
5907
|
|
-(exactly one bit clear resp. set) and emit btr/bts/btc with an immediate
|
|
|
5927
|
+(exactly one bit clear resp. set) and emit bt/btr/bts/btc with an immediate
|
|
5908
|
5928
|
bit offset.
|
|
5909
|
5929
|
|
|
5910
|
5930
|
We restrict the pattern to W32 and native-width W64: the instructions do not
|
| ... |
... |
@@ -5916,15 +5936,15 @@ they are not worth the extra care. |
|
5916
|
5936
|
--
|
|
5917
|
5937
|
-- The returned expression is always at word width ('machOpArgReps' fixes
|
|
5918
|
5938
|
-- shift amounts at 'wordWidth'). See Note [Bit-test instructions].
|
|
5919
|
|
-singleBit_maybe :: CmmExpr -> Maybe CmmExpr
|
|
5920
|
|
-singleBit_maybe (CmmMachOp (MO_Shl _) [CmmLit (CmmInt 1 _), i]) = Just i
|
|
5921
|
|
-singleBit_maybe _ = Nothing
|
|
|
5939
|
+singleBitShift_maybe :: CmmExpr -> Maybe CmmExpr
|
|
|
5940
|
+singleBitShift_maybe (CmmMachOp (MO_Shl _) [CmmLit (CmmInt 1 _), i]) = Just i
|
|
|
5941
|
+singleBitShift_maybe _ = Nothing
|
|
5922
|
5942
|
|
|
5923
|
5943
|
-- | If exactly one bit of @m@, taken at width @w@, is set, return its index.
|
|
5924
|
5944
|
--
|
|
5925
|
5945
|
-- See Note [Bit-test instructions].
|
|
5926
|
|
-setBitLit_maybe :: Width -> Integer -> Maybe Int
|
|
5927
|
|
-setBitLit_maybe w m
|
|
|
5946
|
+singleBitLit_maybe :: Width -> Integer -> Maybe Int
|
|
|
5947
|
+singleBitLit_maybe w m
|
|
5928
|
5948
|
| popCount m' == 1 = Just (countTrailingZeros m')
|
|
5929
|
5949
|
| otherwise = Nothing
|
|
5930
|
5950
|
where
|
| ... |
... |
@@ -5935,66 +5955,74 @@ setBitLit_maybe w m |
|
5935
|
5955
|
-- index.
|
|
5936
|
5956
|
--
|
|
5937
|
5957
|
-- See Note [Bit-test instructions].
|
|
5938
|
|
-clearBitLit_maybe :: Width -> Integer -> Maybe Int
|
|
5939
|
|
-clearBitLit_maybe w m = setBitLit_maybe w (complement m)
|
|
|
5958
|
+invSingleBitLit_maybe :: Width -> Integer -> Maybe Int
|
|
|
5959
|
+invSingleBitLit_maybe w m = singleBitLit_maybe w (complement m)
|
|
5940
|
5960
|
|
|
5941
|
5961
|
bitTestOpWidthOK :: Bool -> Width -> Bool
|
|
5942
|
5962
|
bitTestOpWidthOK is32Bit w = w == W32 || (w == W64 && not is32Bit)
|
|
5943
|
5963
|
|
|
5944
|
|
--- | The bit-offset operand of a bit-test instruction (btr/bts/btc).
|
|
|
5964
|
+-- | The condition to branch on after @bt@, for a @mask != 0@ ('NE') or
|
|
|
5965
|
+-- @mask == 0@ ('EQQ') comparison. 'Nothing' for any other condition.
|
|
|
5966
|
+--
|
|
|
5967
|
+-- @bt@ reports the tested bit in CF, so we branch on 'CARRY' or 'GEU'
|
|
|
5968
|
+-- rather than on ZF via 'NE' or 'EQQ'.
|
|
|
5969
|
+bitTestCond :: Cond -> Maybe Cond
|
|
|
5970
|
+bitTestCond NE = Just CARRY
|
|
|
5971
|
+bitTestCond EQQ = Just GEU
|
|
|
5972
|
+bitTestCond _ = Nothing
|
|
|
5973
|
+
|
|
|
5974
|
+-- | The bit-offset operand of a bit-test instruction.
|
|
5945
|
5975
|
data BitIndex
|
|
5946
|
|
- = BitIndexReg CmmExpr -- ^ variable index, computed into a register
|
|
5947
|
|
- | BitIndexImm Int -- ^ literal index, emitted as an immediate
|
|
|
5976
|
+ = BitIndexReg !CmmExpr -- ^ variable index, computed into a register
|
|
|
5977
|
+ | BitIndexImm !Int -- ^ literal index, emitted as an immediate
|
|
5948
|
5978
|
|
|
5949
|
|
--- | Match the operands of a single-bit set or complement operation: one
|
|
5950
|
|
--- operand is a mask @1 << i@, or a literal with exactly one bit set that
|
|
5951
|
|
--- does not fit in an imm32. Returns the other operand and the bit index.
|
|
5952
|
|
---
|
|
5953
|
|
--- Both operand orders are matched, e.g. @x | (1 << i)@ and @(1 << i) | x@.
|
|
|
5979
|
+-- | Match a single-bit mask: @1 << i@, or a literal with exactly one bit set
|
|
|
5980
|
+-- that does not fit in an imm32. Returns the bit index.
|
|
5954
|
5981
|
--
|
|
5955
|
5982
|
-- See Note [Bit-test instructions].
|
|
5956
|
|
-setBitArgs_maybe :: Platform -> Width -> CmmExpr -> CmmExpr
|
|
5957
|
|
- -> Maybe (CmmExpr, BitIndex)
|
|
5958
|
|
-setBitArgs_maybe platform w x y = go x y `mplus` go y x
|
|
5959
|
|
- where
|
|
5960
|
|
- go opnd mask
|
|
5961
|
|
- | Just i <- singleBit_maybe mask
|
|
5962
|
|
- = Just (opnd, BitIndexReg i)
|
|
5963
|
|
- | CmmLit lit@(CmmInt m _) <- mask
|
|
5964
|
|
- , Just i <- setBitLit_maybe w m
|
|
5965
|
|
- , not (is32BitLit platform lit)
|
|
5966
|
|
- = Just (opnd, BitIndexImm i)
|
|
5967
|
|
- | otherwise
|
|
5968
|
|
- = Nothing
|
|
|
5983
|
+singleBitMask_maybe :: Platform -> Width -> CmmExpr -> Maybe BitIndex
|
|
|
5984
|
+singleBitMask_maybe platform w mask
|
|
|
5985
|
+ | Just i <- singleBitShift_maybe mask
|
|
|
5986
|
+ = Just (BitIndexReg i)
|
|
|
5987
|
+ | CmmLit lit@(CmmInt m _) <- mask
|
|
|
5988
|
+ , Just i <- singleBitLit_maybe w m
|
|
|
5989
|
+ , not (is32BitLit platform lit)
|
|
|
5990
|
+ = Just (BitIndexImm i)
|
|
|
5991
|
+ | otherwise
|
|
|
5992
|
+ = Nothing
|
|
|
5993
|
+
|
|
|
5994
|
+-- | As 'singleBitMask_maybe', for an inverted single-bit mask: @~(1 << i)@,
|
|
|
5995
|
+-- or a literal with exactly one bit clear.
|
|
|
5996
|
+invSingleBitMask_maybe :: Platform -> Width -> CmmExpr -> Maybe BitIndex
|
|
|
5997
|
+invSingleBitMask_maybe platform w mask
|
|
|
5998
|
+ | CmmMachOp (MO_Not _) [b] <- mask
|
|
|
5999
|
+ , Just i <- singleBitShift_maybe b
|
|
|
6000
|
+ = Just (BitIndexReg i)
|
|
|
6001
|
+ | CmmLit lit@(CmmInt m _) <- mask
|
|
|
6002
|
+ , Just i <- invSingleBitLit_maybe w m
|
|
|
6003
|
+ , not (is32BitLit platform lit)
|
|
|
6004
|
+ = Just (BitIndexImm i)
|
|
|
6005
|
+ | otherwise
|
|
|
6006
|
+ = Nothing
|
|
5969
|
6007
|
|
|
5970
|
|
--- | As 'setBitArgs_maybe', for a single-bit clear operation: the mask is
|
|
5971
|
|
--- @~(1 << i)@, or a literal with exactly one bit clear.
|
|
5972
|
|
-clearBitArgs_maybe :: Platform -> Width -> CmmExpr -> CmmExpr
|
|
5973
|
|
- -> Maybe (CmmExpr, BitIndex)
|
|
5974
|
|
-clearBitArgs_maybe platform w x y = go x y `mplus` go y x
|
|
5975
|
|
- where
|
|
5976
|
|
- go opnd mask
|
|
5977
|
|
- | CmmMachOp (MO_Not _) [b] <- mask
|
|
5978
|
|
- , Just i <- singleBit_maybe b
|
|
5979
|
|
- = Just (opnd, BitIndexReg i)
|
|
5980
|
|
- | CmmLit lit@(CmmInt m _) <- mask
|
|
5981
|
|
- , Just i <- clearBitLit_maybe w m
|
|
5982
|
|
- , not (is32BitLit platform lit)
|
|
5983
|
|
- = Just (opnd, BitIndexImm i)
|
|
5984
|
|
- | otherwise
|
|
5985
|
|
- = Nothing
|
|
|
6008
|
+-- | Apply a matcher to either of two operands, returning the other operand
|
|
|
6009
|
+-- alongside the match, e.g. for both @x | (1 << i)@ and @(1 << i) | x@.
|
|
|
6010
|
+matchEitherArg :: (CmmExpr -> Maybe a) -> CmmExpr -> CmmExpr
|
|
|
6011
|
+ -> Maybe (CmmExpr, a)
|
|
|
6012
|
+matchEitherArg match x y =
|
|
|
6013
|
+ ((,) x <$> match y) `mplus` ((,) y <$> match x)
|
|
5986
|
6014
|
|
|
5987
|
6015
|
-- | Generate code for @dst := x@ followed by a bit-test instruction
|
|
5988
|
6016
|
-- (btr/bts/btc).
|
|
5989
|
6017
|
--
|
|
5990
|
6018
|
-- See Note [Bit-test instructions].
|
|
5991
|
|
-genBitTestCode :: Format -> (Format -> Operand -> Operand -> Instr)
|
|
5992
|
|
- -> CmmExpr -> BitIndex -> NatM Register
|
|
5993
|
|
-genBitTestCode rep instr x (BitIndexImm i) = do
|
|
|
6019
|
+genBitModifyCode :: Format -> (Format -> Operand -> Operand -> Instr)
|
|
|
6020
|
+ -> CmmExpr -> BitIndex -> NatM Register
|
|
|
6021
|
+genBitModifyCode rep instr x (BitIndexImm i) = do
|
|
5994
|
6022
|
x_code <- getAnyReg x
|
|
5995
|
6023
|
let code dst = x_code dst `snocOL` instr rep (OpImm (ImmInt i)) (OpReg dst)
|
|
5996
|
6024
|
return (Any rep code)
|
|
5997
|
|
-genBitTestCode rep instr x (BitIndexReg i) = do
|
|
|
6025
|
+genBitModifyCode rep instr x (BitIndexReg i) = do
|
|
5998
|
6026
|
(i_reg, i_code) <- getNonClobberedReg i
|
|
5999
|
6027
|
x_code <- getAnyReg x
|
|
6000
|
6028
|
tmp <- getNewRegNat rep
|