Simon Jakobi pushed to branch wip/sjakobi/T27688-bt-single-bit-tests at Glasgow Haskell Compiler / GHC

Commits:

9 changed files:

Changes:

  • changelog.d/ncg-x86-bit-test-instructions
    1 1
     section: compiler
    
    2 2
     synopsis: The x86 native code generator now uses the bit-test instructions
    
    3
    -  ``btr``/``bts``/``btc`` to clear, set or complement a single bit
    
    3
    +  ``bt``/``btr``/``bts``/``btc`` to test, clear, set or complement a single
    
    4
    +  bit
    
    4 5
     description:
    
    5
    -  Cmm patterns such as ``x & ~(1 << i)``, ``x | (1 << i)`` and
    
    6
    -  ``x ^ (1 << i)`` now compile to a single ``btr``/``bts``/``btc``
    
    7
    -  instruction instead of a mov/shl/not/and-style sequence, matching what C
    
    8
    -  compilers produce. The same applies to the literal masks that constant
    
    9
    -  folding produces from these patterns when ``i`` is constant, in the cases
    
    10
    -  where the mask doesn't fit in an imm32 operand.
    
    6
    +  Cmm patterns such as ``(x & (1 << i)) != 0``, ``x & ~(1 << i)``,
    
    7
    +  ``x | (1 << i)`` and ``x ^ (1 << i)`` now compile to a single
    
    8
    +  ``bt``/``btr``/``bts``/``btc`` instruction instead of a mov/shl/and-style
    
    9
    +  sequence, matching what C compilers produce. The same happens with a
    
    10
    +  literal ``i`` when the folded mask does not fit in an imm32 operand,
    
    11
    +  e.g. ``x | (1 << 40)``.
    
    11 12
     
    
    12 13
       For a variable bit index this applies only when the shift is unchecked,
    
    13 14
       as with ``uncheckedShiftL#`` or ``Data.Bits.unsafeShiftL``. The
    
    14 15
       bounds-checked ``shiftL`` — used, for example, by the default
    
    15
    -  implementations of ``clearBit``, ``setBit`` and ``complementBit`` —
    
    16
    -  wraps the shift in a bounds mask that this optimisation does not see
    
    17
    -  through. With a literal index, the bounds mask is constant-folded away,
    
    18
    -  so the checked operations benefit too.
    
    19
    -mrs: !16311
    
    20
    -issues: #25233
    16
    +  implementations of ``testBit``, ``clearBit``, ``setBit`` and
    
    17
    +  ``complementBit`` — wraps the shift in a bounds mask that this
    
    18
    +  optimisation does not see through. With a literal index, the bounds mask
    
    19
    +  is constant-folded away, so the checked operations benefit too.
    
    20
    +mrs: !16311 !16639
    
    21
    +issues: #25233 #27688

  • compiler/GHC/CmmToAsm/X86/CodeGen.hs
    ... ... @@ -1428,16 +1428,16 @@ getRegister' platform is32Bit (CmmMachOp mop [x]) = do -- unary MachOps
    1428 1428
     -- See Note [Bit-test instructions].
    
    1429 1429
     getRegister' platform is32Bit (CmmMachOp (MO_And w) [x, y])
    
    1430 1430
       | bitTestOpWidthOK is32Bit w
    
    1431
    -  , Just (opnd, ix) <- clearBitArgs_maybe platform w x y
    
    1432
    -  = genBitTestCode (intFormat w) BTR opnd ix
    
    1431
    +  , Just (opnd, ix) <- matchEitherArg (invSingleBitMask_maybe platform w) x y
    
    1432
    +  = genBitModifyCode (intFormat w) BTR opnd ix
    
    1433 1433
     getRegister' platform is32Bit (CmmMachOp (MO_Or w) [x, y])
    
    1434 1434
       | bitTestOpWidthOK is32Bit w
    
    1435
    -  , Just (opnd, ix) <- setBitArgs_maybe platform w x y
    
    1436
    -  = genBitTestCode (intFormat w) BTS opnd ix
    
    1435
    +  , Just (opnd, ix) <- matchEitherArg (singleBitMask_maybe platform w) x y
    
    1436
    +  = genBitModifyCode (intFormat w) BTS opnd ix
    
    1437 1437
     getRegister' platform is32Bit (CmmMachOp (MO_Xor w) [x, y])
    
    1438 1438
       | bitTestOpWidthOK is32Bit w
    
    1439
    -  , Just (opnd, ix) <- setBitArgs_maybe platform w x y
    
    1440
    -  = genBitTestCode (intFormat w) BTC opnd ix
    
    1439
    +  , Just (opnd, ix) <- matchEitherArg (singleBitMask_maybe platform w) x y
    
    1440
    +  = genBitModifyCode (intFormat w) BTC opnd ix
    
    1441 1441
     
    
    1442 1442
     getRegister' platform is32Bit (CmmMachOp mop [x, y]) = do -- dyadic MachOps
    
    1443 1443
       sse4_1 <- x86Feature x86Sse4_1Enabled
    
    ... ... @@ -3926,6 +3926,24 @@ condIntCode' platform cond (CmmLoad x ty _) (CmmLit lit)
    3926 3926
         --
    
    3927 3927
         return (CondCode False cond code)
    
    3928 3928
     
    
    3929
    +-- single-bit test, e.g. (x & (1 << i)) != 0
    
    3930
    +-- see Note [Bit-test instructions]
    
    3931
    +condIntCode' platform cond (CmmMachOp (MO_And w) [x, y]) (CmmLit (CmmInt 0 _))
    
    3932
    +  | Just bt_cond <- bitTestCond cond
    
    3933
    +  , bitTestOpWidthOK (target32Bit platform) w
    
    3934
    +  , Just (opnd, ix) <- matchEitherArg (singleBitMask_maybe platform w) x y
    
    3935
    +  , let fmt = intFormat w
    
    3936
    +  = case ix of
    
    3937
    +      BitIndexImm i -> do
    
    3938
    +        (x_reg, x_code) <- getSomeReg opnd
    
    3939
    +        return $ CondCode False bt_cond $
    
    3940
    +          x_code `snocOL` BT fmt (OpImm (ImmInt i)) (OpReg x_reg)
    
    3941
    +      BitIndexReg i -> do
    
    3942
    +        (i_reg, i_code) <- getNonClobberedReg i
    
    3943
    +        (x_reg, x_code) <- getSomeReg opnd
    
    3944
    +        return $ CondCode False bt_cond $
    
    3945
    +          i_code `appOL` x_code `snocOL` BT fmt (OpReg i_reg) (OpReg x_reg)
    
    3946
    +
    
    3929 3947
     -- anything vs zero, using a mask
    
    3930 3948
     -- TODO: Add some sanity checking!!!!
    
    3931 3949
     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
    

  • compiler/GHC/CmmToAsm/X86/Instr.hs
    ... ... @@ -192,7 +192,8 @@ data Instr
    192 192
             | SHRD        Format Operand{-amount-} Operand Operand
    
    193 193
             | SHLD        Format Operand{-amount-} Operand Operand
    
    194 194
     
    
    195
    -        | BT          Format Imm Operand
    
    195
    +        -- | Bit test
    
    196
    +        | BT          Format Operand{- ^ bit offset (imm/reg) -} Operand
    
    196 197
             -- | Bit test-and-reset
    
    197 198
             | BTR         Format Operand{- ^ bit offset (imm/reg) -} Operand
    
    198 199
             -- | Bit set
    
    ... ... @@ -506,7 +507,7 @@ regUsageOfInstr platform instr
    506 507
         SHR    fmt imm dst             -> readOp fmt imm <.> modifyOp fmt dst
    
    507 508
         SHLD   fmt imm dst1 dst2       -> readModifyModify fmt imm dst1 dst2
    
    508 509
         SHRD   fmt imm dst1 dst2       -> readModifyModify fmt imm dst1 dst2
    
    509
    -    BT     fmt _   src             -> readOp fmt src
    
    510
    +    BT     fmt off src             -> readOp fmt off <.> readOp fmt src
    
    510 511
         BTR    fmt off dst             -> readOp fmt off <.> modifyOp fmt dst
    
    511 512
         BTS    fmt off dst             -> readOp fmt off <.> modifyOp fmt dst
    
    512 513
         BTC    fmt off dst             -> readOp fmt off <.> modifyOp fmt dst
    
    ... ... @@ -819,7 +820,7 @@ patchRegsOfInstr platform instr env
    819 820
         SHR  fmt imm dst     -> patch1 (SHR fmt imm) dst
    
    820 821
         SHLD fmt imm dst1 dst2 -> patch2 (SHLD fmt imm) dst1 dst2
    
    821 822
         SHRD fmt imm dst1 dst2 -> patch2 (SHRD fmt imm) dst1 dst2
    
    822
    -    BT   fmt imm src     -> patch1 (BT  fmt imm) src
    
    823
    +    BT   fmt off src     -> patch2 (BT  fmt) off src
    
    823 824
         BTR  fmt off dst     -> patch2 (BTR fmt) off dst
    
    824 825
         BTS  fmt off dst     -> patch2 (BTS fmt) off dst
    
    825 826
         BTC  fmt off dst     -> patch2 (BTC fmt) off dst
    

  • compiler/GHC/CmmToAsm/X86/Ppr.hs
    ... ... @@ -860,8 +860,8 @@ pprInstr platform i = case i of
    860 860
        SHRD format src dst1 dst2
    
    861 861
           -> pprShift2 (text "shrd") format src dst1 dst2
    
    862 862
     
    
    863
    -   BT format imm src
    
    864
    -      -> pprFormatImmOp (text "bt") format imm src
    
    863
    +   BT format off src
    
    864
    +      -> pprFormatOpOp (text "bt") format off src
    
    865 865
     
    
    866 866
        BTR format off dst
    
    867 867
           -> pprFormatOpOp (text "btr") format off dst
    
    ... ... @@ -1251,16 +1251,6 @@ pprInstr platform i = case i of
    1251 1251
              FmtInt64  -> text "q"
    
    1252 1252
        pprBroadcastFormat _ = panic "Scalar Format invading vector operation"
    
    1253 1253
     
    
    1254
    -   pprFormatImmOp :: Line doc -> Format -> Imm -> Operand -> doc
    
    1255
    -   pprFormatImmOp name format imm op1
    
    1256
    -     = line $ hcat [
    
    1257
    -           pprMnemonic name format,
    
    1258
    -           char '$',
    
    1259
    -           pprImm platform imm,
    
    1260
    -           comma,
    
    1261
    -           pprOperand platform format op1
    
    1262
    -       ]
    
    1263
    -
    
    1264 1254
        pprFormatOp_ :: Line doc -> Format -> Operand -> doc
    
    1265 1255
        pprFormatOp_ name format op1
    
    1266 1256
          = line $ hcat [
    

  • testsuite/tests/codeGen/should_gen_asm/T27688.asm
    1
    +btq %
    
    2
    +btl %
    
    3
    +btq $40,

  • testsuite/tests/codeGen/should_gen_asm/T27688.hs
    1
    +{-# LANGUAGE MagicHash #-}
    
    2
    +
    
    3
    +-- Check that testing a single, variable bit uses the bt instruction
    
    4
    +-- (#27688).
    
    5
    +module T27688 where
    
    6
    +
    
    7
    +import GHC.Exts
    
    8
    +
    
    9
    +testBitNe :: Word# -> Int# -> Int#
    
    10
    +testBitNe x i =
    
    11
    +  if isTrue# ((x `and#` (1## `uncheckedShiftL#` i)) `neWord#` 0##)
    
    12
    +  then 1# else 2#
    
    13
    +
    
    14
    +testBitEq :: Word# -> Int# -> Int#
    
    15
    +testBitEq x i =
    
    16
    +  if isTrue# ((x `and#` (1## `uncheckedShiftL#` i)) `eqWord#` 0##)
    
    17
    +  then 1# else 2#
    
    18
    +
    
    19
    +testBit32 :: Word32# -> Int# -> Int#
    
    20
    +testBit32 x i =
    
    21
    +  if isTrue# ((x `andWord32#` (wordToWord32# 1## `uncheckedShiftLWord32#` i))
    
    22
    +                `neWord32#` wordToWord32# 0##)
    
    23
    +  then 1# else 2#
    
    24
    +
    
    25
    +-- With a constant bit index >= 32, the mask constant-folds to a literal
    
    26
    +-- that does not fit in an imm32, so bt with an immediate offset is used.
    
    27
    +testBit40 :: Word# -> Int#
    
    28
    +testBit40 x =
    
    29
    +  if isTrue# ((x `and#` (1## `uncheckedShiftL#` 40#)) `neWord#` 0##)
    
    30
    +  then 1# else 2#

  • testsuite/tests/codeGen/should_gen_asm/T27688b.asm
    1
    +btq %
    
    2
    +setc
    
    3
    +setae
    
    4
    +btq $40,

  • testsuite/tests/codeGen/should_gen_asm/T27688b.cmm
    1
    +#include "Cmm.h"
    
    2
    +
    
    3
    +// Single-bit tests whose result is used as a value rather than as a branch
    
    4
    +// condition, a literal mask that does not fit in an imm32, and the mask on
    
    5
    +// the left of the operator (#27688).
    
    6
    +
    
    7
    +testBitValue (W_ x, W_ i) {
    
    8
    +    return ((x & (1 << i)) != 0);
    
    9
    +}
    
    10
    +
    
    11
    +testBitValueEq (W_ x, W_ i) {
    
    12
    +    return ((x & (1 << i)) == 0);
    
    13
    +}
    
    14
    +
    
    15
    +testBit40 (W_ x) {
    
    16
    +    return (((0x10000000000 :: bits64) & x) != 0);
    
    17
    +}
    
    18
    +
    
    19
    +testBitLeft (W_ x, W_ i) {
    
    20
    +    return (((1 << i) & x) != 0);
    
    21
    +}

  • testsuite/tests/codeGen/should_gen_asm/all.T
    ... ... @@ -44,6 +44,10 @@ test('T25233', [unless(arch('x86_64'), skip),
    44 44
     test('T25233b', [unless(arch('x86_64'), skip),
    
    45 45
                      when(unregisterised(), skip)], compile_grep_asm, ['cmm', True, ''])
    
    46 46
     test('T24813', is_amd64_codegen, compile_cmp_asm, ['cmm', '-msse4.2'])
    
    47
    +test('T27688', [unless(arch('x86_64'), skip),
    
    48
    +                when(unregisterised(), skip)], compile_grep_asm, ['hs', True, '-O'])
    
    49
    +test('T27688b', [unless(arch('x86_64'), skip),
    
    50
    +                 when(unregisterised(), skip)], compile_grep_asm, ['cmm', True, ''])
    
    47 51
     is_aarch64_codegen = [
    
    48 52
         unless(arch('aarch64'), skip),
    
    49 53
         when(unregisterised(), skip),