Skip to content
Snippets Groups Projects
Commit 5b950a7f authored by Ben Gamari's avatar Ben Gamari Committed by Marge Bot
Browse files

cmm: narrow when folding signed quotients

Previously the constant-folding behavior for MO_S_Quot and MO_S_Rem
failed to narrow its arguments, meaning that a program like:

    %zx64(%quot(%lobits8(0x00e1::bits16), 3::bits8))

would be miscompiled. Specifically, this program should reduce as

                %lobits8(0x00e1::bits16)                 == -31
          %quot(%lobits8(0x00e1::bits16), 3::bits8)      == -10
    %zx64(%quot(%lobits8(0x00e1::bits16), 3::bits8))     == 246

However, with this bug the `%lobits8(0x00e1::bits16)` would instead
be treated as `+31`, resulting in the incorrect result of `75`.

(cherry picked from commit 94e197e3)
parent 1724ac37
No related branches found
No related tags found
No related merge requests found
......@@ -130,8 +130,8 @@ cmmMachOpFoldM platform mop [CmmLit (CmmInt x xrep), CmmLit (CmmInt y _)]
MO_Mul r -> Just $! CmmLit (CmmInt (x * y) r)
MO_U_Quot r | y /= 0 -> Just $! CmmLit (CmmInt (x_u `quot` y_u) r)
MO_U_Rem r | y /= 0 -> Just $! CmmLit (CmmInt (x_u `rem` y_u) r)
MO_S_Quot r | y /= 0 -> Just $! CmmLit (CmmInt (x `quot` y) r)
MO_S_Rem r | y /= 0 -> Just $! CmmLit (CmmInt (x `rem` y) r)
MO_S_Quot r | y /= 0 -> Just $! CmmLit (CmmInt (x_s `quot` y_s) r)
MO_S_Rem r | y /= 0 -> Just $! CmmLit (CmmInt (x_s `rem` y_s) r)
MO_And r -> Just $! CmmLit (CmmInt (x .&. y) r)
MO_Or r -> Just $! CmmLit (CmmInt (x .|. y) r)
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment