Skip to content

Commit c0cbe3f

Browse files
committed
[NFC][DAGCombine][X86][AArch64] Tests for 'A - (A & (B - 1))' pattern (PR44448)
https://rise4fun.com/Alive/ZVdp Name: ptr - (ptr & (alignment-1)) -> ptr & (0 - alignment) %mask = add i64 %alignment, -1 %bias = and i64 %ptr, %mask %r = sub i64 %ptr, %bias => %highbitmask = sub i64 0, %alignment %r = and i64 %ptr, %highbitmask The main motivational pattern involes pointer-typed values, so this transform can't really be done in middle-end. See https://bugs.llvm.org/show_bug.cgi?id=44448 https://reviews.llvm.org/D71499
1 parent 2e03324 commit c0cbe3f

File tree

2 files changed

+424
-0
lines changed

2 files changed

+424
-0
lines changed
Lines changed: 153 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,153 @@
1+
; NOTE: Assertions have been autogenerated by utils/update_llc_test_checks.py
2+
; RUN: llc -mtriple=aarch64-unknown-linux-gnu < %s | FileCheck %s
3+
4+
; Fold
5+
; ptr - (ptr & (alignment-1))
6+
; To
7+
; ptr & (0 - alignment)
8+
;
9+
; This needs to be a backend-level fold because only by now pointers
10+
; are just registers; in middle-end IR this can only be done via @llvm.ptrmask()
11+
; intrinsic which is not sufficiently widely-spread yet.
12+
;
13+
; https://bugs.llvm.org/show_bug.cgi?id=44448
14+
15+
; The basic positive tests
16+
17+
define i32 @t0_32(i32 %ptr, i32 %alignment) nounwind {
18+
; CHECK-LABEL: t0_32:
19+
; CHECK: // %bb.0:
20+
; CHECK-NEXT: sub w8, w1, #1 // =1
21+
; CHECK-NEXT: and w8, w0, w8
22+
; CHECK-NEXT: sub w0, w0, w8
23+
; CHECK-NEXT: ret
24+
%mask = add i32 %alignment, -1
25+
%bias = and i32 %ptr, %mask
26+
%r = sub i32 %ptr, %bias
27+
ret i32 %r
28+
}
29+
define i64 @t1_64(i64 %ptr, i64 %alignment) nounwind {
30+
; CHECK-LABEL: t1_64:
31+
; CHECK: // %bb.0:
32+
; CHECK-NEXT: sub x8, x1, #1 // =1
33+
; CHECK-NEXT: and x8, x0, x8
34+
; CHECK-NEXT: sub x0, x0, x8
35+
; CHECK-NEXT: ret
36+
%mask = add i64 %alignment, -1
37+
%bias = and i64 %ptr, %mask
38+
%r = sub i64 %ptr, %bias
39+
ret i64 %r
40+
}
41+
42+
define i32 @t2_commutative(i32 %ptr, i32 %alignment) nounwind {
43+
; CHECK-LABEL: t2_commutative:
44+
; CHECK: // %bb.0:
45+
; CHECK-NEXT: sub w8, w1, #1 // =1
46+
; CHECK-NEXT: and w8, w8, w0
47+
; CHECK-NEXT: sub w0, w0, w8
48+
; CHECK-NEXT: ret
49+
%mask = add i32 %alignment, -1
50+
%bias = and i32 %mask, %ptr ; swapped
51+
%r = sub i32 %ptr, %bias
52+
ret i32 %r
53+
}
54+
55+
; Extra use tests
56+
57+
define i32 @t3_extrause0(i32 %ptr, i32 %alignment, i32* %mask_storage) nounwind {
58+
; CHECK-LABEL: t3_extrause0:
59+
; CHECK: // %bb.0:
60+
; CHECK-NEXT: sub w8, w1, #1 // =1
61+
; CHECK-NEXT: and w9, w0, w8
62+
; CHECK-NEXT: sub w0, w0, w9
63+
; CHECK-NEXT: str w8, [x2]
64+
; CHECK-NEXT: ret
65+
%mask = add i32 %alignment, -1
66+
store i32 %mask, i32* %mask_storage
67+
%bias = and i32 %ptr, %mask
68+
%r = sub i32 %ptr, %bias
69+
ret i32 %r
70+
}
71+
define i32 @n4_extrause1(i32 %ptr, i32 %alignment, i32* %bias_storage) nounwind {
72+
; CHECK-LABEL: n4_extrause1:
73+
; CHECK: // %bb.0:
74+
; CHECK-NEXT: sub w8, w1, #1 // =1
75+
; CHECK-NEXT: and w8, w0, w8
76+
; CHECK-NEXT: sub w0, w0, w8
77+
; CHECK-NEXT: str w8, [x2]
78+
; CHECK-NEXT: ret
79+
%mask = add i32 %alignment, -1
80+
%bias = and i32 %ptr, %mask ; has extra uses, can't fold
81+
store i32 %bias, i32* %bias_storage
82+
%r = sub i32 %ptr, %bias
83+
ret i32 %r
84+
}
85+
define i32 @n5_extrause2(i32 %ptr, i32 %alignment, i32* %mask_storage, i32* %bias_storage) nounwind {
86+
; CHECK-LABEL: n5_extrause2:
87+
; CHECK: // %bb.0:
88+
; CHECK-NEXT: sub w8, w1, #1 // =1
89+
; CHECK-NEXT: str w8, [x2]
90+
; CHECK-NEXT: and w8, w0, w8
91+
; CHECK-NEXT: sub w0, w0, w8
92+
; CHECK-NEXT: str w8, [x3]
93+
; CHECK-NEXT: ret
94+
%mask = add i32 %alignment, -1
95+
store i32 %mask, i32* %mask_storage
96+
%bias = and i32 %ptr, %mask ; has extra uses, can't fold
97+
store i32 %bias, i32* %bias_storage
98+
%r = sub i32 %ptr, %bias
99+
ret i32 %r
100+
}
101+
102+
; Negative tests
103+
104+
define i32 @n6_different_ptrs(i32 %ptr0, i32 %ptr1, i32 %alignment) nounwind {
105+
; CHECK-LABEL: n6_different_ptrs:
106+
; CHECK: // %bb.0:
107+
; CHECK-NEXT: sub w8, w2, #1 // =1
108+
; CHECK-NEXT: and w8, w1, w8
109+
; CHECK-NEXT: sub w0, w0, w8
110+
; CHECK-NEXT: ret
111+
%mask = add i32 %alignment, -1
112+
%bias = and i32 %ptr1, %mask ; not %ptr0
113+
%r = sub i32 %ptr0, %bias ; not %ptr1
114+
ret i32 %r
115+
}
116+
define i32 @n7_different_ptrs_commutative(i32 %ptr0, i32 %ptr1, i32 %alignment) nounwind {
117+
; CHECK-LABEL: n7_different_ptrs_commutative:
118+
; CHECK: // %bb.0:
119+
; CHECK-NEXT: sub w8, w2, #1 // =1
120+
; CHECK-NEXT: and w8, w8, w1
121+
; CHECK-NEXT: sub w0, w0, w8
122+
; CHECK-NEXT: ret
123+
%mask = add i32 %alignment, -1
124+
%bias = and i32 %mask, %ptr1 ; swapped, not %ptr0
125+
%r = sub i32 %ptr0, %bias ; not %ptr1
126+
ret i32 %r
127+
}
128+
129+
define i32 @n8_not_lowbit_mask(i32 %ptr, i32 %alignment) nounwind {
130+
; CHECK-LABEL: n8_not_lowbit_mask:
131+
; CHECK: // %bb.0:
132+
; CHECK-NEXT: add w8, w1, #1 // =1
133+
; CHECK-NEXT: and w8, w0, w8
134+
; CHECK-NEXT: sub w0, w0, w8
135+
; CHECK-NEXT: ret
136+
%mask = add i32 %alignment, 1 ; not -1
137+
%bias = and i32 %ptr, %mask
138+
%r = sub i32 %ptr, %bias
139+
ret i32 %r
140+
}
141+
142+
define i32 @n9_sub_is_not_commutative(i32 %ptr, i32 %alignment) nounwind {
143+
; CHECK-LABEL: n9_sub_is_not_commutative:
144+
; CHECK: // %bb.0:
145+
; CHECK-NEXT: sub w8, w1, #1 // =1
146+
; CHECK-NEXT: and w8, w0, w8
147+
; CHECK-NEXT: sub w0, w8, w0
148+
; CHECK-NEXT: ret
149+
%mask = add i32 %alignment, -1
150+
%bias = and i32 %ptr, %mask
151+
%r = sub i32 %bias, %ptr ; wrong order
152+
ret i32 %r
153+
}

0 commit comments

Comments
 (0)