@@ -360,6 +360,7 @@ let neg_int c dbg = sub_int (Cconst_int (0, dbg)) c dbg
360
360
361
361
let rec lsl_int c1 c2 dbg =
362
362
match c1, c2 with
363
+ | c1 , Cconst_int (0 , _ ) -> c1
363
364
| Cop (Clsl , [c; Cconst_int (n1, _)], _), Cconst_int (n2, _)
364
365
when n1 > 0 && n2 > 0 && n1 + n2 < size_int * 8 ->
365
366
Cop (Clsl , [c; Cconst_int (n1 + n2, dbg)], dbg)
@@ -432,6 +433,8 @@ let asr_int c1 c2 dbg =
432
433
| c1' -> Cop (Casr , [c1'; c2], dbg))
433
434
| _ -> Cop (Casr , [c1; c2], dbg)
434
435
436
+ let asr_const c n dbg = asr_int c (Cconst_int (n, dbg)) dbg
437
+
435
438
let tag_int i dbg =
436
439
match i with
437
440
| Cconst_int (n , _ ) -> int_const dbg n
@@ -1299,9 +1302,10 @@ let rec low_bits ~bits dbg x =
1299
1302
( (Casr | Clsr ),
1300
1303
[Cop (Clsl , [x; Cconst_int (left, _)], _); Cconst_int (right, _)],
1301
1304
_ )
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
1305
1309
| x -> (
1306
1310
match get_const_bitmask x with
1307
1311
| Some (x , bitmask ) when does_mask_ignore_low_bits bitmask ->
@@ -1342,21 +1346,20 @@ let sign_extend ~bits dbg e =
1342
1346
let arch_bits = Arch. size_int * 8 in
1343
1347
let unused_bits = arch_bits - bits in
1344
1348
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
1350
1350
in
1351
1351
if bits = arch_bits
1352
1352
then e
1353
1353
else
1354
1354
map_tail
1355
1355
(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
1360
1363
| Cop (Cload { memory_chunk; mutability; is_atomic }, args, dbg) as e
1361
1364
-> (
1362
1365
let load memory_chunk =
0 commit comments