Skip to content

Commit bb5a845

Browse files
authored
[Cranelift] resolve #12790, fix arithmetic rewrite for wider integers (#12796)
* [Cranelift] resolve #12790 * [Cranelift] runtest setting added
1 parent 75404ec commit bb5a845

3 files changed

Lines changed: 30 additions & 2 deletions

File tree

cranelift/codegen/src/opts/arithmetic.isle

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -318,5 +318,9 @@
318318
(rule (simplify (isub ty (bor ty y x) (bxor ty y x))) (band ty x y))
319319

320320
;; (~x) + x == -1
321-
(rule (simplify (iadd ty (bnot ty x) x)) (iconst_s ty -1))
322-
(rule (simplify (iadd ty x (bnot ty x))) (iconst_s ty -1))
321+
;; Keep the generic fold for <=64-bit types, and handle i128 explicitly.
322+
(rule (simplify (iadd (fits_in_64 ty) (bnot ty x) x)) (iconst_s ty -1))
323+
(rule (simplify (iadd (fits_in_64 ty) x (bnot ty x))) (iconst_s ty -1))
324+
325+
(rule (simplify (iadd $I128 (bnot $I128 x) x)) (sextend $I128 (iconst_s $I64 -1)))
326+
(rule (simplify (iadd $I128 x (bnot $I128 x))) (sextend $I128 (iconst_s $I64 -1)))

cranelift/filetests/filetests/egraph/arithmetic-precise.clif

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -71,3 +71,16 @@ block0(v0: i32):
7171
; return v3 ; v3 = -1
7272
; }
7373

74+
function %fold_iadd_bnot_x_to_allones_i128(i128) -> i128 {
75+
block0(v0: i128):
76+
v1 = bnot v0
77+
v2 = iadd v1, v0
78+
return v2
79+
}
80+
81+
; function %fold_iadd_bnot_x_to_allones_i128(i128) -> i128 fast {
82+
; block0(v0: i128):
83+
; v3 = iconst.i64 -1
84+
; v4 = sextend.i128 v3 ; v3 = -1
85+
; return v4
86+
; }

cranelift/filetests/filetests/runtests/i128-arithmetic-extends.clif

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
test interpret
22
test run
33
set enable_llvm_abi_extensions=true
4+
set enable_multi_ret_implicit_sret
45
target aarch64
56
target s390x
67
target x86_64
@@ -17,3 +18,13 @@ block0(v0: i32, v1: i128):
1718
; run: %sext_sshr_i32_i128(0x8000_0000, 0) == 0xFFFFFFFF80000000
1819
; run: %sext_sshr_i32_i128(0x8000_0000, 32) == 0xFFFFFFFF80000000
1920
; run: %sext_sshr_i32_i128(0x8000_0000, 0xFFFFFFFF_FFFFFFFF_FFFFFFFF_FFFFFF20) == 0xFFFFFFFF80000000
21+
22+
function %fold_iadd_bnot_x_to_allones_i128_rt(i128) -> i128 {
23+
block0(v0: i128):
24+
v1 = bnot v0
25+
v2 = iadd v1, v0
26+
return v2
27+
}
28+
; run: %fold_iadd_bnot_x_to_allones_i128_rt(0) == -1
29+
; run: %fold_iadd_bnot_x_to_allones_i128_rt(1) == -1
30+
; run: %fold_iadd_bnot_x_to_allones_i128_rt(0x12345678_9ABCDEF0_11111111_22222222) == -1

0 commit comments

Comments
 (0)