[Git][ghc/ghc][wip/andreask/arm_immediates] Aarch64 NCG: Don't emit overflowed literals

Andreas Klebinger (@AndreasK) gitlab at gitlab.haskell.org
Thu Jul 20 14:12:36 UTC 2023



Andreas Klebinger pushed to branch wip/andreask/arm_immediates at Glasgow Haskell Compiler / GHC


Commits:
b5b21723 by Andreas Klebinger at 2023-07-20T16:03:06+02:00
Aarch64 NCG: Don't emit overflowed literals

Rather than emitting overflowed literals we truncate them now.

- - - - -


2 changed files:

- compiler/GHC/CmmToAsm/AArch64/CodeGen.hs
- compiler/GHC/CmmToAsm/AArch64/Regs.hs


Changes:

=====================================
compiler/GHC/CmmToAsm/AArch64/CodeGen.hs
=====================================
@@ -397,9 +397,9 @@ For example mov x1, #0x10000 is allowed but will be assembled to movz x1, #0x1,
 -- Allows for 16bit immediate which can be shifted by 0/16/32/48 bits.
 -- Used with MOVZ,MOVN, MOVK
 -- See Note [Aarch64 immediates]
-getMovWideImm :: Integer -> Maybe Operand
-getMovWideImm n
-  -- TODO: Handle sign extension
+getMovWideImm :: Integer -> Width -> Maybe Operand
+getMovWideImm n w
+  -- TODO: Handle sign extension/negatives
   | n <= 0
   = Nothing
   -- Fits in 16 bits
@@ -408,44 +408,46 @@ getMovWideImm n
 
   -- 0x0000 0000 xxxx 0000
   | trailing_zeros >= 16 && sized_n < 2^(32 :: Int)
-  = Just $ OpImmShift (ImmInteger $ n `shiftR` 16) SLSL 16
+  = Just $ OpImmShift (ImmInteger $ truncated `shiftR` 16) SLSL 16
 
   -- 0x 0000 xxxx 0000 0000
   | trailing_zeros >= 32 && sized_n < 2^(48 :: Int)
-  = Just $ OpImmShift (ImmInteger $ n `shiftR` 32) SLSL 32
+  = Just $ OpImmShift (ImmInteger $ truncated `shiftR` 32) SLSL 32
 
   -- 0x xxxx 0000 0000 0000
   | trailing_zeros >= 48
-  = Just $ OpImmShift (ImmInteger $ n `shiftR` 48) SLSL 48
+  = Just $ OpImmShift (ImmInteger $ truncated `shiftR` 48) SLSL 48
 
   | otherwise
   = Nothing
   where
-    sized_n = fromIntegral n :: Word64
+    truncated = narrowU w n
+    sized_n = fromIntegral truncated :: Word64
     trailing_zeros = countTrailingZeros sized_n
 
 -- | Arithmetic(immediate)
 --  Allows for 12bit immediates which can be shifted by 0 or 12 bits.
 -- Used with ADD, ADDS, SUB, SUBS, CMP, CMN
 -- See Note [Aarch64 immediates]
-getArithImm :: Integer -> Maybe Operand
-getArithImm n
+getArithImm :: Integer -> Width -> Maybe Operand
+getArithImm n w
   -- TODO: Handle sign extension
   | n <= 0
   = Nothing
   -- Fits in 16 bits
   -- Fits in 12 bits
   | sized_n < 2^(12::Int)
-  = Just $ OpImm (ImmInteger n)
+  = Just $ OpImm (ImmInteger truncated)
 
   -- 12 bits shifted by 12 places.
   | trailing_zeros >= 12 && sized_n < 2^(24::Int)
-  = Just $ OpImmShift (ImmInteger $ n `shiftR` 12) SLSL 12
+  = Just $ OpImmShift (ImmInteger $ truncated `shiftR` 12) SLSL 12
 
   | otherwise
   = Nothing
   where
-    sized_n = fromIntegral n :: Word64
+    sized_n = fromIntegral truncated :: Word64
+    truncated = narrowU w n
     trailing_zeros = countTrailingZeros sized_n
 
 -- |  Logical (immediate)
@@ -453,10 +455,12 @@ getArithImm n
 -- Used with AND, ANDS, EOR, ORR, TST
 -- and their aliases which includes at least MOV (bitmask immediate)
 -- See Note [Aarch64 immediates]
-getBitmaskImm :: Integer -> Maybe Operand
-getBitmaskImm n
-  | isAArch64Bitmask n = Just $ OpImm (ImmInteger n)
+getBitmaskImm :: Integer -> Width -> Maybe Operand
+getBitmaskImm n w
+  | isAArch64Bitmask truncated = Just $ OpImm (ImmInteger truncated)
   | otherwise = Nothing
+  where
+    truncated = narrowU w n
 
 
 -- TODO OPT: we might be able give getRegister


=====================================
compiler/GHC/CmmToAsm/AArch64/Regs.hs
=====================================
@@ -77,6 +77,8 @@ litToImm (CmmInt i w)        = ImmInteger (narrowS w i)
                 -- narrow to the width: a CmmInt might be out of
                 -- range, but we assume that ImmInteger only contains
                 -- in-range values.  A signed value should be fine here.
+                -- AK: We do call this with out of range values, however
+                -- it just truncates as we would expect.
 litToImm (CmmFloat f W32)    = ImmFloat f
 litToImm (CmmFloat f W64)    = ImmDouble f
 litToImm (CmmLabel l)        = ImmCLbl l



View it on GitLab: https://gitlab.haskell.org/ghc/ghc/-/commit/b5b217238dffa64369ddbf0dcf03ebb804e3c94c

-- 
View it on GitLab: https://gitlab.haskell.org/ghc/ghc/-/commit/b5b217238dffa64369ddbf0dcf03ebb804e3c94c
You're receiving this email because of your account on gitlab.haskell.org.


-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.haskell.org/pipermail/ghc-commits/attachments/20230720/a15a2642/attachment-0001.html>


More information about the ghc-commits mailing list