1 ; NOTE: Assertions have been autogenerated by utils/update_test_checks.py
2 ; RUN: opt < %s -instcombine -S | FileCheck %s
4 target datalayout = "e-p:64:64:64-i1:8:8-i8:8:8-i16:16:16-i32:32:32-i64:64:64-f32:32:32-f64:64:64-v64:64:64-v128:128:128-a0:0:64-s0:64:64-f80:128:128"
6 declare i32 @llvm.ctpop.i32(i32)
7 declare i32 @llvm.ctlz.i32(i32, i1)
8 declare i32 @llvm.cttz.i32(i32, i1)
10 define i64 @test1(i32 %x) {
11 ; CHECK-LABEL: @test1(
12 ; CHECK-NEXT: [[T:%.*]] = call i32 @llvm.ctpop.i32(i32 [[X:%.*]]), !range [[RNG0:![0-9]+]]
13 ; CHECK-NEXT: [[S:%.*]] = zext i32 [[T]] to i64
14 ; CHECK-NEXT: ret i64 [[S]]
16 %t = call i32 @llvm.ctpop.i32(i32 %x)
17 %s = sext i32 %t to i64
21 define i64 @test2(i32 %x) {
22 ; CHECK-LABEL: @test2(
23 ; CHECK-NEXT: [[T:%.*]] = call i32 @llvm.ctlz.i32(i32 [[X:%.*]], i1 true), !range [[RNG0]]
24 ; CHECK-NEXT: [[S:%.*]] = zext i32 [[T]] to i64
25 ; CHECK-NEXT: ret i64 [[S]]
27 %t = call i32 @llvm.ctlz.i32(i32 %x, i1 true)
28 %s = sext i32 %t to i64
32 define i64 @test3(i32 %x) {
33 ; CHECK-LABEL: @test3(
34 ; CHECK-NEXT: [[T:%.*]] = call i32 @llvm.cttz.i32(i32 [[X:%.*]], i1 true), !range [[RNG0]]
35 ; CHECK-NEXT: [[S:%.*]] = zext i32 [[T]] to i64
36 ; CHECK-NEXT: ret i64 [[S]]
38 %t = call i32 @llvm.cttz.i32(i32 %x, i1 true)
39 %s = sext i32 %t to i64
43 define i64 @test4(i32 %x) {
44 ; CHECK-LABEL: @test4(
45 ; CHECK-NEXT: [[T:%.*]] = udiv i32 [[X:%.*]], 3
46 ; CHECK-NEXT: [[S:%.*]] = zext i32 [[T]] to i64
47 ; CHECK-NEXT: ret i64 [[S]]
50 %s = sext i32 %t to i64
54 define i64 @test5(i32 %x) {
55 ; CHECK-LABEL: @test5(
56 ; CHECK-NEXT: [[T:%.*]] = urem i32 [[X:%.*]], 30000
57 ; CHECK-NEXT: [[S:%.*]] = zext i32 [[T]] to i64
58 ; CHECK-NEXT: ret i64 [[S]]
60 %t = urem i32 %x, 30000
61 %s = sext i32 %t to i64
65 define i64 @test6(i32 %x) {
66 ; CHECK-LABEL: @test6(
67 ; CHECK-NEXT: [[U:%.*]] = lshr i32 [[X:%.*]], 3
68 ; CHECK-NEXT: [[T:%.*]] = mul nuw nsw i32 [[U]], 3
69 ; CHECK-NEXT: [[S:%.*]] = zext i32 [[T]] to i64
70 ; CHECK-NEXT: ret i64 [[S]]
74 %s = sext i32 %t to i64
78 define i64 @test7(i32 %x) {
79 ; CHECK-LABEL: @test7(
80 ; CHECK-NEXT: [[T:%.*]] = and i32 [[X:%.*]], 511
81 ; CHECK-NEXT: [[U:%.*]] = sub nuw nsw i32 20000, [[T]]
82 ; CHECK-NEXT: [[S:%.*]] = zext i32 [[U]] to i64
83 ; CHECK-NEXT: ret i64 [[S]]
86 %u = sub i32 20000, %t
87 %s = sext i32 %u to i64
91 define i32 @test8(i8 %a, i32 %f, i1 %p, i32* %z) {
92 ; CHECK-LABEL: @test8(
93 ; CHECK-NEXT: [[D:%.*]] = lshr i32 [[F:%.*]], 24
94 ; CHECK-NEXT: [[N:%.*]] = select i1 [[P:%.*]], i32 [[D]], i32 0
95 ; CHECK-NEXT: ret i32 [[N]]
98 %e = select i1 %p, i32 %d, i32 0
99 %s = trunc i32 %e to i16
100 %n = sext i16 %s to i32
105 define i16 @test9(i16 %t, i1 %cond) {
106 ; CHECK-LABEL: @test9(
108 ; CHECK-NEXT: br i1 [[COND:%.*]], label [[TBB:%.*]], label [[FBB:%.*]]
110 ; CHECK-NEXT: br label [[FBB]]
112 ; CHECK-NEXT: [[V_OFF0:%.*]] = phi i16 [ [[T:%.*]], [[TBB]] ], [ 42, [[ENTRY:%.*]] ]
113 ; CHECK-NEXT: ret i16 [[V_OFF0]]
116 br i1 %cond, label %TBB, label %FBB
119 %t2 = sext i16 %t to i32
123 %V = phi i32 [%t2, %TBB], [42, %entry]
124 %W = trunc i32 %V to i16
129 define i32 @test10(i32 %i) {
130 ; CHECK-LABEL: @test10(
131 ; CHECK-NEXT: [[D1:%.*]] = shl i32 [[I:%.*]], 30
132 ; CHECK-NEXT: [[D:%.*]] = ashr exact i32 [[D1]], 30
133 ; CHECK-NEXT: ret i32 [[D]]
135 %A = trunc i32 %i to i8
138 %D = sext i8 %C to i32
142 define <2 x i32> @test10_vec(<2 x i32> %i) {
143 ; CHECK-LABEL: @test10_vec(
144 ; CHECK-NEXT: [[D1:%.*]] = shl <2 x i32> [[I:%.*]], <i32 30, i32 30>
145 ; CHECK-NEXT: [[D:%.*]] = ashr exact <2 x i32> [[D1]], <i32 30, i32 30>
146 ; CHECK-NEXT: ret <2 x i32> [[D]]
148 %A = trunc <2 x i32> %i to <2 x i8>
149 %B = shl <2 x i8> %A, <i8 6, i8 6>
150 %C = ashr <2 x i8> %B, <i8 6, i8 6>
151 %D = sext <2 x i8> %C to <2 x i32>
155 define <2 x i32> @test10_vec_nonuniform(<2 x i32> %i) {
156 ; CHECK-LABEL: @test10_vec_nonuniform(
157 ; CHECK-NEXT: [[D1:%.*]] = shl <2 x i32> [[I:%.*]], <i32 30, i32 27>
158 ; CHECK-NEXT: [[D:%.*]] = ashr <2 x i32> [[D1]], <i32 30, i32 27>
159 ; CHECK-NEXT: ret <2 x i32> [[D]]
161 %A = trunc <2 x i32> %i to <2 x i8>
162 %B = shl <2 x i8> %A, <i8 6, i8 3>
163 %C = ashr <2 x i8> %B, <i8 6, i8 3>
164 %D = sext <2 x i8> %C to <2 x i32>
168 define <2 x i32> @test10_vec_undef0(<2 x i32> %i) {
169 ; CHECK-LABEL: @test10_vec_undef0(
170 ; CHECK-NEXT: [[D1:%.*]] = shl <2 x i32> [[I:%.*]], <i32 30, i32 undef>
171 ; CHECK-NEXT: [[D:%.*]] = ashr <2 x i32> [[D1]], <i32 30, i32 undef>
172 ; CHECK-NEXT: ret <2 x i32> [[D]]
174 %A = trunc <2 x i32> %i to <2 x i8>
175 %B = shl <2 x i8> %A, <i8 6, i8 0>
176 %C = ashr <2 x i8> %B, <i8 6, i8 undef>
177 %D = sext <2 x i8> %C to <2 x i32>
180 define <2 x i32> @test10_vec_undef1(<2 x i32> %i) {
181 ; CHECK-LABEL: @test10_vec_undef1(
182 ; CHECK-NEXT: [[D1:%.*]] = shl <2 x i32> [[I:%.*]], <i32 30, i32 undef>
183 ; CHECK-NEXT: [[D:%.*]] = ashr <2 x i32> [[D1]], <i32 30, i32 undef>
184 ; CHECK-NEXT: ret <2 x i32> [[D]]
186 %A = trunc <2 x i32> %i to <2 x i8>
187 %B = shl <2 x i8> %A, <i8 6, i8 undef>
188 %C = ashr <2 x i8> %B, <i8 6, i8 0>
189 %D = sext <2 x i8> %C to <2 x i32>
192 define <2 x i32> @test10_vec_undef2(<2 x i32> %i) {
193 ; CHECK-LABEL: @test10_vec_undef2(
194 ; CHECK-NEXT: [[D1:%.*]] = shl <2 x i32> [[I:%.*]], <i32 30, i32 undef>
195 ; CHECK-NEXT: [[D:%.*]] = ashr <2 x i32> [[D1]], <i32 30, i32 undef>
196 ; CHECK-NEXT: ret <2 x i32> [[D]]
198 %A = trunc <2 x i32> %i to <2 x i8>
199 %B = shl <2 x i8> %A, <i8 6, i8 undef>
200 %C = ashr <2 x i8> %B, <i8 6, i8 undef>
201 %D = sext <2 x i8> %C to <2 x i32>
205 define void @test11(<2 x i16> %srcA, <2 x i16> %srcB, <2 x i16>* %dst) {
206 ; CHECK-LABEL: @test11(
207 ; CHECK-NEXT: [[CMP:%.*]] = icmp eq <2 x i16> [[SRCB:%.*]], [[SRCA:%.*]]
208 ; CHECK-NEXT: [[SEXT:%.*]] = sext <2 x i1> [[CMP]] to <2 x i16>
209 ; CHECK-NEXT: store <2 x i16> [[SEXT]], <2 x i16>* [[DST:%.*]], align 4
210 ; CHECK-NEXT: ret void
212 %cmp = icmp eq <2 x i16> %srcB, %srcA
213 %sext = sext <2 x i1> %cmp to <2 x i16>
214 %tmask = ashr <2 x i16> %sext, <i16 15, i16 15>
215 store <2 x i16> %tmask, <2 x i16>* %dst
219 define i64 @test12(i32 %x) {
220 ; CHECK-LABEL: @test12(
221 ; CHECK-NEXT: [[SHR:%.*]] = lshr i32 [[X:%.*]], 1
222 ; CHECK-NEXT: [[SUB:%.*]] = sub nsw i32 0, [[SHR]]
223 ; CHECK-NEXT: [[CONV:%.*]] = sext i32 [[SUB]] to i64
224 ; CHECK-NEXT: ret i64 [[CONV]]
226 %shr = lshr i32 %x, 1
227 %sub = sub nsw i32 0, %shr
228 %conv = sext i32 %sub to i64
232 define i32 @test13(i32 %x) {
233 ; CHECK-LABEL: @test13(
234 ; CHECK-NEXT: [[AND:%.*]] = lshr i32 [[X:%.*]], 3
235 ; CHECK-NEXT: [[TMP1:%.*]] = and i32 [[AND]], 1
236 ; CHECK-NEXT: [[SEXT:%.*]] = add nsw i32 [[TMP1]], -1
237 ; CHECK-NEXT: ret i32 [[SEXT]]
240 %cmp = icmp eq i32 %and, 0
241 %ext = sext i1 %cmp to i32
245 define i32 @test14(i16 %x) {
246 ; CHECK-LABEL: @test14(
247 ; CHECK-NEXT: [[AND:%.*]] = lshr i16 [[X:%.*]], 4
248 ; CHECK-NEXT: [[TMP1:%.*]] = and i16 [[AND]], 1
249 ; CHECK-NEXT: [[SEXT:%.*]] = add nsw i16 [[TMP1]], -1
250 ; CHECK-NEXT: [[EXT:%.*]] = sext i16 [[SEXT]] to i32
251 ; CHECK-NEXT: ret i32 [[EXT]]
253 %and = and i16 %x, 16
254 %cmp = icmp ne i16 %and, 16
255 %ext = sext i1 %cmp to i32
259 define i32 @test15(i32 %x) {
260 ; CHECK-LABEL: @test15(
261 ; CHECK-NEXT: [[AND:%.*]] = shl i32 [[X:%.*]], 27
262 ; CHECK-NEXT: [[SEXT:%.*]] = ashr i32 [[AND]], 31
263 ; CHECK-NEXT: ret i32 [[SEXT]]
265 %and = and i32 %x, 16
266 %cmp = icmp ne i32 %and, 0
267 %ext = sext i1 %cmp to i32
271 define i32 @test16(i16 %x) {
272 ; CHECK-LABEL: @test16(
273 ; CHECK-NEXT: [[AND:%.*]] = shl i16 [[X:%.*]], 12
274 ; CHECK-NEXT: [[SEXT:%.*]] = ashr i16 [[AND]], 15
275 ; CHECK-NEXT: [[EXT:%.*]] = sext i16 [[SEXT]] to i32
276 ; CHECK-NEXT: ret i32 [[EXT]]
279 %cmp = icmp eq i16 %and, 8
280 %ext = sext i1 %cmp to i32
284 define i32 @test17(i1 %x) {
285 ; CHECK-LABEL: @test17(
286 ; CHECK-NEXT: [[C1_NEG:%.*]] = zext i1 [[X:%.*]] to i32
287 ; CHECK-NEXT: ret i32 [[C1_NEG]]
289 %c1 = sext i1 %x to i32
294 define i32 @test18(i16 %x) {
295 ; CHECK-LABEL: @test18(
296 ; CHECK-NEXT: [[TMP1:%.*]] = icmp sgt i16 [[X:%.*]], 0
297 ; CHECK-NEXT: [[SEL:%.*]] = select i1 [[TMP1]], i16 [[X]], i16 0
298 ; CHECK-NEXT: [[EXT:%.*]] = zext i16 [[SEL]] to i32
299 ; CHECK-NEXT: ret i32 [[EXT]]
301 %cmp = icmp slt i16 %x, 0
302 %sel = select i1 %cmp, i16 0, i16 %x
303 %ext = sext i16 %sel to i32
307 define i10 @test19(i10 %i) {
308 ; CHECK-LABEL: @test19(
309 ; CHECK-NEXT: [[A:%.*]] = trunc i10 [[I:%.*]] to i3
310 ; CHECK-NEXT: [[TMP1:%.*]] = and i3 [[A]], 1
311 ; CHECK-NEXT: [[C:%.*]] = sub nsw i3 0, [[TMP1]]
312 ; CHECK-NEXT: [[D:%.*]] = sext i3 [[C]] to i10
313 ; CHECK-NEXT: ret i10 [[D]]
315 %a = trunc i10 %i to i3
318 %d = sext i3 %c to i10