Skip to content

Commit c2deb71

Browse files
committed
updated shift code to optimize away extra tagging operations
1 parent 400136f commit c2deb71

File tree

1 file changed

+15
-12
lines changed

1 file changed

+15
-12
lines changed

backend/cmm_helpers.ml

+15-12
Original file line numberDiff line numberDiff line change
@@ -360,6 +360,7 @@ let neg_int c dbg = sub_int (Cconst_int (0, dbg)) c dbg
360360

361361
let rec lsl_int c1 c2 dbg =
362362
match c1, c2 with
363+
| c1, Cconst_int (0, _) -> c1
363364
| Cop (Clsl, [c; Cconst_int (n1, _)], _), Cconst_int (n2, _)
364365
when n1 > 0 && n2 > 0 && n1 + n2 < size_int * 8 ->
365366
Cop (Clsl, [c; Cconst_int (n1 + n2, dbg)], dbg)
@@ -432,6 +433,8 @@ let asr_int c1 c2 dbg =
432433
| c1' -> Cop (Casr, [c1'; c2], dbg))
433434
| _ -> Cop (Casr, [c1; c2], dbg)
434435

436+
let asr_const c n dbg = asr_int c (Cconst_int (n, dbg)) dbg
437+
435438
let tag_int i dbg =
436439
match i with
437440
| Cconst_int (n, _) -> int_const dbg n
@@ -1299,9 +1302,10 @@ let rec low_bits ~bits dbg x =
12991302
( (Casr | Clsr),
13001303
[Cop (Clsl, [x; Cconst_int (left, _)], _); Cconst_int (right, _)],
13011304
_ )
1302-
when 0 < left && left = right && right <= unused_bits ->
1303-
(* Ignore sign extension which does not affect the low bits *)
1304-
low_bits ~bits dbg x
1305+
when 0 <= right && right <= left && left <= unused_bits ->
1306+
if left = right
1307+
then low_bits ~bits dbg x
1308+
else lsl_const x (left - right) dbg
13051309
| x -> (
13061310
match get_const_bitmask x with
13071311
| Some (x, bitmask) when does_mask_ignore_low_bits bitmask ->
@@ -1342,21 +1346,20 @@ let sign_extend ~bits dbg e =
13421346
let arch_bits = Arch.size_int * 8 in
13431347
let unused_bits = arch_bits - bits in
13441348
let sign_extend_via_shift e =
1345-
Cop
1346-
( Casr,
1347-
[ Cop (Clsl, [e; Cconst_int (unused_bits, dbg)], dbg);
1348-
Cconst_int (unused_bits, dbg) ],
1349-
dbg )
1349+
asr_const (lsl_const e unused_bits dbg) unused_bits dbg
13501350
in
13511351
if bits = arch_bits
13521352
then e
13531353
else
13541354
map_tail
13551355
(function
1356-
| Cop (Casr, [_; Cconst_int (n, _)], _) as e
1357-
when unused_bits <= n && n < arch_bits ->
1358-
(* the sign is already in the high bits. *)
1359-
e
1356+
| Cop ((Casr | Clsr), [inner; Cconst_int (n, _)], _) as e
1357+
when 0 <= n && n < arch_bits ->
1358+
if n > unused_bits
1359+
then (* already sign-extended *) e
1360+
else
1361+
let e = lsl_const inner (unused_bits - n) dbg in
1362+
asr_const e unused_bits dbg
13601363
| Cop (Cload { memory_chunk; mutability; is_atomic }, args, dbg) as e
13611364
-> (
13621365
let load memory_chunk =

0 commit comments

Comments
 (0)