popcount.ll
7.93 KB
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
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
; NOTE: Assertions have been autogenerated by utils/update_test_checks.py
; RUN: opt < %s -aggressive-instcombine -S | FileCheck %s
;int popcount8(unsigned char i) {
; i = i - ((i >> 1) & 0x55);
; i = (i & 0x33) + ((i >> 2) & 0x33);
; i = ((i + (i >> 4)) & 0x0F);
; return (i * 0x01010101);
;}
define signext i32 @popcount8(i8 zeroext %0) {
; CHECK-LABEL: @popcount8(
; CHECK-NEXT: [[TMP2:%.*]] = lshr i8 [[TMP0:%.*]], 1
; CHECK-NEXT: [[TMP3:%.*]] = and i8 [[TMP2]], 85
; CHECK-NEXT: [[TMP4:%.*]] = sub i8 [[TMP0]], [[TMP3]]
; CHECK-NEXT: [[TMP5:%.*]] = and i8 [[TMP4]], 51
; CHECK-NEXT: [[TMP6:%.*]] = lshr i8 [[TMP4]], 2
; CHECK-NEXT: [[TMP7:%.*]] = and i8 [[TMP6]], 51
; CHECK-NEXT: [[TMP8:%.*]] = add nuw nsw i8 [[TMP7]], [[TMP5]]
; CHECK-NEXT: [[TMP9:%.*]] = lshr i8 [[TMP8]], 4
; CHECK-NEXT: [[TMP10:%.*]] = add nuw nsw i8 [[TMP9]], [[TMP8]]
; CHECK-NEXT: [[TMP11:%.*]] = and i8 [[TMP10]], 15
; CHECK-NEXT: [[TMP12:%.*]] = zext i8 [[TMP11]] to i32
; CHECK-NEXT: ret i32 [[TMP12]]
;
%2 = lshr i8 %0, 1
%3 = and i8 %2, 85
%4 = sub i8 %0, %3
%5 = and i8 %4, 51
%6 = lshr i8 %4, 2
%7 = and i8 %6, 51
%8 = add nuw nsw i8 %7, %5
%9 = lshr i8 %8, 4
%10 = add nuw nsw i8 %9, %8
%11 = and i8 %10, 15
%12 = zext i8 %11 to i32
ret i32 %12
}
;int popcount32(unsigned i) {
; i = i - ((i >> 1) & 0x55555555);
; i = (i & 0x33333333) + ((i >> 2) & 0x33333333);
; i = ((i + (i >> 4)) & 0x0F0F0F0F);
; return (i * 0x01010101) >> 24;
;}
define signext i32 @popcount32(i32 zeroext %0) {
; CHECK-LABEL: @popcount32(
; CHECK-NEXT: [[TMP2:%.*]] = call i32 @llvm.ctpop.i32(i32 [[TMP0:%.*]])
; CHECK-NEXT: ret i32 [[TMP2]]
;
%2 = lshr i32 %0, 1
%3 = and i32 %2, 1431655765
%4 = sub i32 %0, %3
%5 = and i32 %4, 858993459
%6 = lshr i32 %4, 2
%7 = and i32 %6, 858993459
%8 = add nuw nsw i32 %7, %5
%9 = lshr i32 %8, 4
%10 = add nuw nsw i32 %9, %8
%11 = and i32 %10, 252645135
%12 = mul i32 %11, 16843009
%13 = lshr i32 %12, 24
ret i32 %13
}
;int popcount64(unsigned long long i) {
; i = i - ((i >> 1) & 0x5555555555555555);
; i = (i & 0x3333333333333333) + ((i >> 2) & 0x3333333333333333);
; i = ((i + (i >> 4)) & 0x0F0F0F0F0F0F0F0F);
; return (i * 0x0101010101010101) >> 56;
;}
define signext i32 @popcount64(i64 %0) {
; CHECK-LABEL: @popcount64(
; CHECK-NEXT: [[TMP2:%.*]] = call i64 @llvm.ctpop.i64(i64 [[TMP0:%.*]])
; CHECK-NEXT: [[TMP3:%.*]] = trunc i64 [[TMP2]] to i32
; CHECK-NEXT: ret i32 [[TMP3]]
;
%2 = lshr i64 %0, 1
%3 = and i64 %2, 6148914691236517205
%4 = sub i64 %0, %3
%5 = and i64 %4, 3689348814741910323
%6 = lshr i64 %4, 2
%7 = and i64 %6, 3689348814741910323
%8 = add nuw nsw i64 %7, %5
%9 = lshr i64 %8, 4
%10 = add nuw nsw i64 %9, %8
%11 = and i64 %10, 1085102592571150095
%12 = mul i64 %11, 72340172838076673
%13 = lshr i64 %12, 56
%14 = trunc i64 %13 to i32
ret i32 %14
}
;int popcount128(__uint128_t i) {
; __uint128_t x = 0x5555555555555555;
; x <<= 64;
; x |= 0x5555555555555555;
; __uint128_t y = 0x3333333333333333;
; y <<= 64;
; y |= 0x3333333333333333;
; __uint128_t z = 0x0f0f0f0f0f0f0f0f;
; z <<= 64;
; z |= 0x0f0f0f0f0f0f0f0f;
; __uint128_t a = 0x0101010101010101;
; a <<= 64;
; a |= 0x0101010101010101;
; unsigned mask = 120;
; i = i - ((i >> 1) & x);
; i = (i & y) + ((i >> 2) & y);
; i = ((i + (i >> 4)) & z);
; return (i * a) >> mask;
;}
define signext i32 @popcount128(i128 %0) {
; CHECK-LABEL: @popcount128(
; CHECK-NEXT: [[TMP2:%.*]] = call i128 @llvm.ctpop.i128(i128 [[TMP0:%.*]])
; CHECK-NEXT: [[TMP3:%.*]] = trunc i128 [[TMP2]] to i32
; CHECK-NEXT: ret i32 [[TMP3]]
;
%2 = lshr i128 %0, 1
%3 = and i128 %2, 113427455640312821154458202477256070485
%4 = sub i128 %0, %3
%5 = and i128 %4, 68056473384187692692674921486353642291
%6 = lshr i128 %4, 2
%7 = and i128 %6, 68056473384187692692674921486353642291
%8 = add nuw nsw i128 %7, %5
%9 = lshr i128 %8, 4
%10 = add nuw nsw i128 %9, %8
%11 = and i128 %10, 20016609818878733144904388672456953615
%12 = mul i128 %11, 1334440654591915542993625911497130241
%13 = lshr i128 %12, 120
%14 = trunc i128 %13 to i32
ret i32 %14
}
;vector unsigned char popcount8vec(vector unsigned char i)
;{
; i = i - ((i>> 1) & 0x55);
; i = (i & 0x33) + ((i >> 2) & 0x33);
; i = ((i + (i >> 4)) & 0x0F);
; return (i * 0x01);
;}
define <16 x i8> @popcount8vec(<16 x i8> %0) {
; CHECK-LABEL: @popcount8vec(
; CHECK-NEXT: [[TMP2:%.*]] = lshr <16 x i8> [[TMP0:%.*]], <i8 1, i8 1, i8 1, i8 1, i8 1, i8 1, i8 1, i8 1, i8 1, i8 1, i8 1, i8 1, i8 1, i8 1, i8 1, i8 1>
; CHECK-NEXT: [[TMP3:%.*]] = and <16 x i8> [[TMP2]], <i8 85, i8 85, i8 85, i8 85, i8 85, i8 85, i8 85, i8 85, i8 85, i8 85, i8 85, i8 85, i8 85, i8 85, i8 85, i8 85>
; CHECK-NEXT: [[TMP4:%.*]] = sub <16 x i8> [[TMP0]], [[TMP3]]
; CHECK-NEXT: [[TMP5:%.*]] = and <16 x i8> [[TMP4]], <i8 51, i8 51, i8 51, i8 51, i8 51, i8 51, i8 51, i8 51, i8 51, i8 51, i8 51, i8 51, i8 51, i8 51, i8 51, i8 51>
; CHECK-NEXT: [[TMP6:%.*]] = lshr <16 x i8> [[TMP4]], <i8 2, i8 2, i8 2, i8 2, i8 2, i8 2, i8 2, i8 2, i8 2, i8 2, i8 2, i8 2, i8 2, i8 2, i8 2, i8 2>
; CHECK-NEXT: [[TMP7:%.*]] = and <16 x i8> [[TMP6]], <i8 51, i8 51, i8 51, i8 51, i8 51, i8 51, i8 51, i8 51, i8 51, i8 51, i8 51, i8 51, i8 51, i8 51, i8 51, i8 51>
; CHECK-NEXT: [[TMP8:%.*]] = add nuw nsw <16 x i8> [[TMP7]], [[TMP5]]
; CHECK-NEXT: [[TMP9:%.*]] = lshr <16 x i8> [[TMP8]], <i8 4, i8 4, i8 4, i8 4, i8 4, i8 4, i8 4, i8 4, i8 4, i8 4, i8 4, i8 4, i8 4, i8 4, i8 4, i8 4>
; CHECK-NEXT: [[TMP10:%.*]] = add nuw nsw <16 x i8> [[TMP9]], [[TMP8]]
; CHECK-NEXT: [[TMP11:%.*]] = and <16 x i8> [[TMP10]], <i8 15, i8 15, i8 15, i8 15, i8 15, i8 15, i8 15, i8 15, i8 15, i8 15, i8 15, i8 15, i8 15, i8 15, i8 15, i8 15>
; CHECK-NEXT: ret <16 x i8> [[TMP11]]
;
%2 = lshr <16 x i8> %0, <i8 1, i8 1, i8 1, i8 1, i8 1, i8 1, i8 1, i8 1, i8 1, i8 1, i8 1, i8 1, i8 1, i8 1, i8 1, i8 1>
%3 = and <16 x i8> %2, <i8 85, i8 85, i8 85, i8 85, i8 85, i8 85, i8 85, i8 85, i8 85, i8 85, i8 85, i8 85, i8 85, i8 85, i8 85, i8 85>
%4 = sub <16 x i8> %0, %3
%5 = and <16 x i8> %4, <i8 51, i8 51, i8 51, i8 51, i8 51, i8 51, i8 51, i8 51, i8 51, i8 51, i8 51, i8 51, i8 51, i8 51, i8 51, i8 51>
%6 = lshr <16 x i8> %4, <i8 2, i8 2, i8 2, i8 2, i8 2, i8 2, i8 2, i8 2, i8 2, i8 2, i8 2, i8 2, i8 2, i8 2, i8 2, i8 2>
%7 = and <16 x i8> %6, <i8 51, i8 51, i8 51, i8 51, i8 51, i8 51, i8 51, i8 51, i8 51, i8 51, i8 51, i8 51, i8 51, i8 51, i8 51, i8 51>
%8 = add nuw nsw <16 x i8> %7, %5
%9 = lshr <16 x i8> %8, <i8 4, i8 4, i8 4, i8 4, i8 4, i8 4, i8 4, i8 4, i8 4, i8 4, i8 4, i8 4, i8 4, i8 4, i8 4, i8 4>
%10 = add nuw nsw <16 x i8> %9, %8
%11 = and <16 x i8> %10, <i8 15, i8 15, i8 15, i8 15, i8 15, i8 15, i8 15, i8 15, i8 15, i8 15, i8 15, i8 15, i8 15, i8 15, i8 15, i8 15>
ret <16 x i8> %11
}
;vector unsigned int popcount32vec(vector unsigned int i)
;{
; i = i - ((i>> 1) & 0x55555555);
; i = (i & 0x33333333) + ((i >> 2) & 0x33333333);
; i = ((i + (i >> 4)) & 0x0F0F0F0F);
; return (i * 0x01010101) >> 24;
;}
define <4 x i32> @popcount32vec(<4 x i32> %0) {
; CHECK-LABEL: @popcount32vec(
; CHECK-NEXT: [[TMP2:%.*]] = call <4 x i32> @llvm.ctpop.v4i32(<4 x i32> [[TMP0:%.*]])
; CHECK-NEXT: ret <4 x i32> [[TMP2]]
;
%2 = lshr <4 x i32> %0, <i32 1, i32 1, i32 1, i32 1>
%3 = and <4 x i32> %2, <i32 1431655765, i32 1431655765, i32 1431655765, i32 1431655765>
%4 = sub <4 x i32> %0, %3
%5 = and <4 x i32> %4, <i32 858993459, i32 858993459, i32 858993459, i32 858993459>
%6 = lshr <4 x i32> %4, <i32 2, i32 2, i32 2, i32 2>
%7 = and <4 x i32> %6, <i32 858993459, i32 858993459, i32 858993459, i32 858993459>
%8 = add nuw nsw <4 x i32> %7, %5
%9 = lshr <4 x i32> %8, <i32 4, i32 4, i32 4, i32 4>
%10 = add nuw nsw <4 x i32> %9, %8
%11 = and <4 x i32> %10, <i32 252645135, i32 252645135, i32 252645135, i32 252645135>
%12 = mul <4 x i32> %11, <i32 16843009, i32 16843009, i32 16843009, i32 16843009>
%13 = lshr <4 x i32> %12, <i32 24, i32 24, i32 24, i32 24>
ret <4 x i32> %13
}