Skip to content

Commit c16e6fe

Browse files
authored
[Cranelift] folds comparison over bitwise-not operands (#11638)
* [Cranelift] add opts and tests * [Cranelift] add opts and tests * [ISLE] add runtests
1 parent bdd8d1b commit c16e6fe

File tree

3 files changed

+57
-0
lines changed

3 files changed

+57
-0
lines changed

cranelift/codegen/src/opts/icmp.isle

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -298,3 +298,7 @@
298298
;; (x < y) ? x : y != x == x > y, for both signed and unsigned.
299299
(rule (simplify (ne cty (select ty (slt cty x y) x y) x)) (sgt cty x y))
300300
(rule (simplify (ne cty (select ty (ult cty x y) x y) x)) (ugt cty x y))
301+
302+
(rule (simplify (ult cty (bnot ty x) (bnot ty y))) (ugt cty x y))
303+
(rule (simplify (slt cty (bnot ty x) (bnot ty y))) (sgt cty x y))
304+
Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
test optimize precise-output
2+
set opt_level=speed
3+
target x86_64
4+
5+
function %test1(i8, i8) -> i8 {
6+
block0(v0: i8, v2: i8):
7+
v1 = bnot v0
8+
v3 = bnot v2
9+
v4 = icmp ult v1, v3
10+
return v4
11+
}
12+
13+
; function %test1(i8, i8) -> i8 fast {
14+
; block0(v0: i8, v2: i8):
15+
; v5 = icmp ugt v0, v2
16+
; return v5
17+
; }
18+
19+
function %test1(i8, i8) -> i8 {
20+
block0(v0: i8, v2: i8):
21+
v1 = bnot v0
22+
v3 = bnot v2
23+
v4 = icmp slt v1, v3
24+
return v4
25+
}
26+
27+
; function %test1(i8, i8) -> i8 fast {
28+
; block0(v0: i8, v2: i8):
29+
; v5 = icmp sgt v0, v2
30+
; return v5
31+
; }
32+

cranelift/filetests/filetests/runtests/icmp.clif

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -44,3 +44,24 @@ block0(v0: i32, v1: i32):
4444
; run: %ule_sub_swap(1, -88) == 0
4545
; run: %ule_sub_swap(1, 2) == 0
4646
; run: %ule_sub_swap(2, 1) == 1
47+
48+
function %fold_cmp_over_nots1(i8, i8) -> i8 {
49+
block0(v0: i8, v2: i8):
50+
v1 = bnot v0
51+
v3 = bnot v2
52+
v4 = icmp ult v1, v3
53+
return v4
54+
}
55+
56+
; run: %fold_cmp_over_nots1(2, 1) == 1
57+
58+
function %fold_cmp_over_nots2(i8, i8) -> i8 {
59+
block0(v0: i8, v2: i8):
60+
v1 = bnot v0
61+
v3 = bnot v2
62+
v4 = icmp slt v1, v3
63+
return v4
64+
}
65+
66+
; run: %fold_cmp_over_nots2(2, 1) == 1
67+

0 commit comments

Comments
 (0)