1 ; NOTE: Assertions have been autogenerated by utils/update_test_checks.py
2 ; RUN: opt < %s -passes=instsimplify -S | FileCheck %s
4 declare i64 @llvm.ctpop.i64(i64)
5 declare i32 @llvm.ctpop.i32(i32)
6 declare i16 @llvm.ctpop.i16(i16)
7 declare i8 @llvm.ctpop.i8(i8)
8 declare <2 x i32> @llvm.ctpop.v2i32(<2 x i32>)
9 declare void @llvm.assume(i1)
11 define i64 @ctpop_1_shl(i64 %x) {
12 ; CHECK-LABEL: @ctpop_1_shl(
13 ; CHECK-NEXT: ret i64 1
16 %cnt = call i64 @llvm.ctpop.i64(i64 %v)
20 define i32 @ctpop_imin_lshr(i32 %x) {
21 ; CHECK-LABEL: @ctpop_imin_lshr(
22 ; CHECK-NEXT: ret i32 1
24 %v = lshr i32 2147483648, %x
25 %cnt = call i32 @llvm.ctpop.i32(i32 %v)
29 define i16 @ctpop_x_and_negx(i16 %x) {
30 ; CHECK-LABEL: @ctpop_x_and_negx(
31 ; CHECK-NEXT: [[V0:%.*]] = sub i16 0, [[X:%.*]]
32 ; CHECK-NEXT: [[V1:%.*]] = and i16 [[X]], [[V0]]
33 ; CHECK-NEXT: [[CNT:%.*]] = call i16 @llvm.ctpop.i16(i16 [[V1]])
34 ; CHECK-NEXT: ret i16 [[CNT]]
38 %cnt = call i16 @llvm.ctpop.i16(i16 %v1)
42 define i8 @ctpop_x_nz_and_negx(i8 %x) {
43 ; CHECK-LABEL: @ctpop_x_nz_and_negx(
44 ; CHECK-NEXT: ret i8 1
49 %cnt = call i8 @llvm.ctpop.i8(i8 %v1)
53 define i32 @ctpop_2_shl(i32 %x) {
54 ; CHECK-LABEL: @ctpop_2_shl(
55 ; CHECK-NEXT: [[V:%.*]] = shl i32 2, [[X:%.*]]
56 ; CHECK-NEXT: [[CNT:%.*]] = call i32 @llvm.ctpop.i32(i32 [[V]])
57 ; CHECK-NEXT: ret i32 [[CNT]]
60 %cnt = call i32 @llvm.ctpop.i32(i32 %v)
64 define i16 @ctpop_imin_plus1_lshr(i16 %x) {
65 ; CHECK-LABEL: @ctpop_imin_plus1_lshr(
66 ; CHECK-NEXT: [[V:%.*]] = lshr i16 -32767, [[X:%.*]]
67 ; CHECK-NEXT: [[CNT:%.*]] = call i16 @llvm.ctpop.i16(i16 [[V]])
68 ; CHECK-NEXT: ret i16 [[CNT]]
70 %v = lshr i16 32769, %x
71 %cnt = call i16 @llvm.ctpop.i16(i16 %v)
76 define i32 @ctpop_2_shl_nz(i32 %x) {
77 ; CHECK-LABEL: @ctpop_2_shl_nz(
78 ; CHECK-NEXT: [[XA30:%.*]] = and i32 30, [[X:%.*]]
79 ; CHECK-NEXT: [[V:%.*]] = shl i32 2, [[XA30]]
80 ; CHECK-NEXT: [[CNT:%.*]] = call i32 @llvm.ctpop.i32(i32 [[V]])
81 ; CHECK-NEXT: ret i32 [[CNT]]
83 %xa30 = and i32 30, %x
85 %cnt = call i32 @llvm.ctpop.i32(i32 %v)
89 define i8 @ctpop_imin_plus1_lshr_nz(i8 %x) {
90 ; CHECK-LABEL: @ctpop_imin_plus1_lshr_nz(
91 ; CHECK-NEXT: [[CMP:%.*]] = icmp ne i8 [[X:%.*]], 0
92 ; CHECK-NEXT: call void @llvm.assume(i1 [[CMP]])
93 ; CHECK-NEXT: [[V:%.*]] = lshr i8 -127, [[X]]
94 ; CHECK-NEXT: [[CNT:%.*]] = call i8 @llvm.ctpop.i8(i8 [[V]])
95 ; CHECK-NEXT: ret i8 [[CNT]]
97 %cmp = icmp ne i8 %x, 0
98 call void @llvm.assume(i1 %cmp)
100 %cnt = call i8 @llvm.ctpop.i8(i8 %v)
105 define i64 @ctpop_x_and_negx_nz(i64 %x) {
106 ; CHECK-LABEL: @ctpop_x_and_negx_nz(
107 ; CHECK-NEXT: [[V0:%.*]] = sub i64 0, [[X:%.*]]
108 ; CHECK-NEXT: [[V1:%.*]] = and i64 [[X]], [[V0]]
109 ; CHECK-NEXT: [[CMP:%.*]] = icmp ne i64 [[V1]], 0
110 ; CHECK-NEXT: call void @llvm.assume(i1 [[CMP]])
111 ; CHECK-NEXT: [[CNT:%.*]] = call i64 @llvm.ctpop.i64(i64 [[V1]])
112 ; CHECK-NEXT: ret i64 [[CNT]]
115 %v1 = and i64 %x, %v0
116 %cmp = icmp ne i64 %v1, 0
117 call void @llvm.assume(i1 %cmp)
118 %cnt = call i64 @llvm.ctpop.i64(i64 %v1)
122 define <2 x i32> @ctpop_shl1_vec(<2 x i32> %x) {
123 ; CHECK-LABEL: @ctpop_shl1_vec(
124 ; CHECK-NEXT: ret <2 x i32> splat (i32 1)
126 %shl = shl <2 x i32> <i32 1 ,i32 1>, %x
127 %cnt = call <2 x i32> @llvm.ctpop.v2i32(<2 x i32> %shl)
131 define <2 x i32> @ctpop_shl2_1_vec(<2 x i32> %x) {
132 ; CHECK-LABEL: @ctpop_shl2_1_vec(
133 ; CHECK-NEXT: [[SHL:%.*]] = shl <2 x i32> <i32 2, i32 1>, [[X:%.*]]
134 ; CHECK-NEXT: [[CNT:%.*]] = call <2 x i32> @llvm.ctpop.v2i32(<2 x i32> [[SHL]])
135 ; CHECK-NEXT: ret <2 x i32> [[CNT]]
137 %shl = shl <2 x i32> <i32 2 ,i32 1>, %x
138 %cnt = call <2 x i32> @llvm.ctpop.v2i32(<2 x i32> %shl)
142 define <2 x i32> @ctpop_lshr_intmin_vec(<2 x i32> %x) {
143 ; CHECK-LABEL: @ctpop_lshr_intmin_vec(
144 ; CHECK-NEXT: ret <2 x i32> splat (i32 1)
146 %shr = lshr <2 x i32> <i32 2147483648 ,i32 2147483648>, %x
147 %cnt = call <2 x i32> @llvm.ctpop.v2i32(<2 x i32> %shr)
152 define <2 x i32> @ctpop_lshr_intmin_intmin_plus1_vec(<2 x i32> %x) {
153 ; CHECK-LABEL: @ctpop_lshr_intmin_intmin_plus1_vec(
154 ; CHECK-NEXT: [[SHR:%.*]] = lshr <2 x i32> <i32 -2147483648, i32 -2147483647>, [[X:%.*]]
155 ; CHECK-NEXT: [[CNT:%.*]] = call <2 x i32> @llvm.ctpop.v2i32(<2 x i32> [[SHR]])
156 ; CHECK-NEXT: ret <2 x i32> [[CNT]]
158 %shr = lshr <2 x i32> <i32 2147483648 ,i32 2147483649>, %x
159 %cnt = call <2 x i32> @llvm.ctpop.v2i32(<2 x i32> %shr)
163 define <2 x i32> @ctpop_lshr_intmin_intmin_plus1_vec_nz(<2 x i32> %x) {
164 ; CHECK-LABEL: @ctpop_lshr_intmin_intmin_plus1_vec_nz(
165 ; CHECK-NEXT: [[X1:%.*]] = or <2 x i32> [[X:%.*]], splat (i32 1)
166 ; CHECK-NEXT: [[SHR:%.*]] = lshr <2 x i32> <i32 -2147483648, i32 -2147483647>, [[X1]]
167 ; CHECK-NEXT: [[CNT:%.*]] = call <2 x i32> @llvm.ctpop.v2i32(<2 x i32> [[SHR]])
168 ; CHECK-NEXT: ret <2 x i32> [[CNT]]
170 %x1 = or <2 x i32> %x, <i32 1 ,i32 1>
171 %shr = lshr <2 x i32> <i32 2147483648 ,i32 2147483649>, %x1
172 %cnt = call <2 x i32> @llvm.ctpop.v2i32(<2 x i32> %shr)
177 define <2 x i32> @ctpop_shl2_1_vec_nz(<2 x i32> %x) {
178 ; CHECK-LABEL: @ctpop_shl2_1_vec_nz(
179 ; CHECK-NEXT: [[AND:%.*]] = and <2 x i32> [[X:%.*]], splat (i32 15)
180 ; CHECK-NEXT: [[SHL:%.*]] = shl <2 x i32> <i32 2, i32 1>, [[AND]]
181 ; CHECK-NEXT: [[CNT:%.*]] = call <2 x i32> @llvm.ctpop.v2i32(<2 x i32> [[SHL]])
182 ; CHECK-NEXT: ret <2 x i32> [[CNT]]
184 %and = and <2 x i32> %x, <i32 15 ,i32 15>
185 %shl = shl <2 x i32> <i32 2 ,i32 1>, %and
186 %cnt = call <2 x i32> @llvm.ctpop.v2i32(<2 x i32> %shl)
190 define <2 x i32> @ctpop_x_and_negx_vec(<2 x i32> %x) {
191 ; CHECK-LABEL: @ctpop_x_and_negx_vec(
192 ; CHECK-NEXT: [[SUB:%.*]] = sub <2 x i32> zeroinitializer, [[X:%.*]]
193 ; CHECK-NEXT: [[AND:%.*]] = and <2 x i32> [[SUB]], [[X]]
194 ; CHECK-NEXT: [[CNT:%.*]] = call <2 x i32> @llvm.ctpop.v2i32(<2 x i32> [[AND]])
195 ; CHECK-NEXT: ret <2 x i32> [[CNT]]
197 %sub = sub <2 x i32> <i32 0 ,i32 0>, %x
198 %and = and <2 x i32> %sub, %x
199 %cnt = call <2 x i32> @llvm.ctpop.v2i32(<2 x i32> %and)
203 define <2 x i32> @ctpop_x_and_negx_vec_nz(<2 x i32> %x) {
204 ; CHECK-LABEL: @ctpop_x_and_negx_vec_nz(
205 ; CHECK-NEXT: [[X1:%.*]] = or <2 x i32> [[X:%.*]], splat (i32 1)
206 ; CHECK-NEXT: [[SUB:%.*]] = sub <2 x i32> zeroinitializer, [[X1]]
207 ; CHECK-NEXT: [[AND:%.*]] = and <2 x i32> [[SUB]], [[X]]
208 ; CHECK-NEXT: [[CNT:%.*]] = call <2 x i32> @llvm.ctpop.v2i32(<2 x i32> [[AND]])
209 ; CHECK-NEXT: ret <2 x i32> [[CNT]]
211 %x1 = or <2 x i32> %x, <i32 1 ,i32 1>
212 %sub = sub <2 x i32> <i32 0 ,i32 0>, %x1
213 %and = and <2 x i32> %sub, %x
214 %cnt = call <2 x i32> @llvm.ctpop.v2i32(<2 x i32> %and)