/
fold-csel-cttz-and.ll
160 lines (146 loc) · 3.92 KB
/
fold-csel-cttz-and.ll
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
; NOTE: Assertions have been autogenerated by utils/update_llc_test_checks.py
; RUN: llc -mtriple=aarch64 < %s | FileCheck %s
;; Check the transformation
;; CSEL 0, cttz, cc -> AND cttz numbits-1
;; for cttz in the case of i32 and i64 respectively
;; Cases for which the optimzation takes place
define i32 @cttzi32(i32 %x) {
; CHECK-LABEL: cttzi32:
; CHECK: // %bb.0: // %entry
; CHECK-NEXT: rbit w8, w0
; CHECK-NEXT: clz w8, w8
; CHECK-NEXT: and w0, w8, #0x1f
; CHECK-NEXT: ret
entry:
%0 = call i32 @llvm.cttz.i32(i32 %x, i1 true)
%1 = icmp eq i32 %x, 0
%2 = select i1 %1, i32 0, i32 %0
ret i32 %2
}
define i64 @cttzi64(i64 %x) {
; CHECK-LABEL: cttzi64:
; CHECK: // %bb.0: // %entry
; CHECK-NEXT: rbit x8, x0
; CHECK-NEXT: clz x8, x8
; CHECK-NEXT: and x0, x8, #0x3f
; CHECK-NEXT: ret
entry:
%0 = call i64 @llvm.cttz.i64(i64 %x, i1 true)
%1 = icmp eq i64 %x, 0
%2 = select i1 %1, i64 0, i64 %0
ret i64 %2
}
define i32 @cttzi32ne(i32 %x) {
; CHECK-LABEL: cttzi32ne:
; CHECK: // %bb.0: // %entry
; CHECK-NEXT: rbit w8, w0
; CHECK-NEXT: clz w8, w8
; CHECK-NEXT: and w0, w8, #0x1f
; CHECK-NEXT: ret
entry:
%0 = call i32 @llvm.cttz.i32(i32 %x, i1 true)
%1 = icmp ne i32 %x, 0
%2 = select i1 %1, i32 %0, i32 0
ret i32 %2
}
define i64 @cttzi64ne(i64 %x) {
; CHECK-LABEL: cttzi64ne:
; CHECK: // %bb.0: // %entry
; CHECK-NEXT: rbit x8, x0
; CHECK-NEXT: clz x8, x8
; CHECK-NEXT: and x0, x8, #0x3f
; CHECK-NEXT: ret
entry:
%0 = call i64 @llvm.cttz.i64(i64 %x, i1 true)
%1 = icmp ne i64 %x, 0
%2 = select i1 %1, i64 %0, i64 0
ret i64 %2
}
define i32 @cttztrunc(i64 %x) {
; CHECK-LABEL: cttztrunc:
; CHECK: // %bb.0: // %entry
; CHECK-NEXT: rbit x8, x0
; CHECK-NEXT: clz x8, x8
; CHECK-NEXT: and w0, w8, #0x1f
; CHECK-NEXT: ret
entry:
%0 = call i64 @llvm.cttz.i64(i64 %x, i1 true)
%1 = icmp eq i64 %x, 0
%2 = select i1 %1, i64 0, i64 %0
%3 = trunc i64 %2 to i32
ret i32 %3
}
;; Cases for which the optimization does not take place
define i32 @cttzne(i32 %x) {
; CHECK-LABEL: cttzne:
; CHECK: // %bb.0: // %entry
; CHECK-NEXT: rbit w8, w0
; CHECK-NEXT: cmp w0, #0
; CHECK-NEXT: clz w8, w8
; CHECK-NEXT: csel w0, wzr, w8, ne
; CHECK-NEXT: ret
entry:
%0 = call i32 @llvm.cttz.i32(i32 %x, i1 true)
%1 = icmp ne i32 %x, 0
%2 = select i1 %1, i32 0, i32 %0
ret i32 %2
}
define i32 @cttzxnot0(i32 %x) {
; CHECK-LABEL: cttzxnot0:
; CHECK: // %bb.0: // %entry
; CHECK-NEXT: rbit w8, w0
; CHECK-NEXT: cmp w0, #10
; CHECK-NEXT: clz w8, w8
; CHECK-NEXT: csel w0, wzr, w8, eq
; CHECK-NEXT: ret
entry:
%0 = call i32 @llvm.cttz.i32(i32 %x, i1 true)
%1 = icmp eq i32 %x, 10
%2 = select i1 %1, i32 0, i32 %0
ret i32 %2
}
define i32 @cttzlhsnot0(i32 %x) {
; CHECK-LABEL: cttzlhsnot0:
; CHECK: // %bb.0: // %entry
; CHECK-NEXT: rbit w9, w0
; CHECK-NEXT: mov w8, #10
; CHECK-NEXT: clz w9, w9
; CHECK-NEXT: cmp w0, #0
; CHECK-NEXT: csel w0, w8, w9, eq
; CHECK-NEXT: ret
entry:
%0 = call i32 @llvm.cttz.i32(i32 %x, i1 true)
%1 = icmp eq i32 %x, 0
%2 = select i1 %1, i32 10, i32 %0
ret i32 %2
}
define i32 @notcttz(i32 %x) {
; CHECK-LABEL: notcttz:
; CHECK: // %bb.0: // %entry
; CHECK-NEXT: clz w8, w0
; CHECK-NEXT: cmp w0, #0
; CHECK-NEXT: csel w0, wzr, w8, eq
; CHECK-NEXT: ret
entry:
%0 = call i32 @llvm.ctlz.i32(i32 %x, i1 true)
%1 = icmp eq i32 %x, 0
%2 = select i1 %1, i32 0, i32 %0
ret i32 %2
}
define i32 @cttzlhsnotx(i32 %x, i32 %y) {
; CHECK-LABEL: cttzlhsnotx:
; CHECK: // %bb.0: // %entry
; CHECK-NEXT: rbit w8, w0
; CHECK-NEXT: cmp w1, #0
; CHECK-NEXT: clz w8, w8
; CHECK-NEXT: csel w0, wzr, w8, eq
; CHECK-NEXT: ret
entry:
%0 = call i32 @llvm.cttz.i32(i32 %x, i1 true)
%1 = icmp eq i32 %y, 0
%2 = select i1 %1, i32 0, i32 %0
ret i32 %2
}
declare i32 @llvm.cttz.i32(i32, i1)
declare i64 @llvm.cttz.i64(i64, i1)
declare i32 @llvm.ctlz.i32(i32, i1)