1 ; NOTE: Assertions have been autogenerated by utils/update_test_checks.py
2 ; RUN: opt < %s -passes=aggressive-instcombine -S | FileCheck %s
4 ; https://bugs.llvm.org/show_bug.cgi?id=34924
6 define i32 @rotl(i32 %a, i32 %b) {
9 ; CHECK-NEXT: [[CMP:%.*]] = icmp eq i32 [[B:%.*]], 0
10 ; CHECK-NEXT: br i1 [[CMP]], label [[END:%.*]], label [[ROTBB:%.*]]
12 ; CHECK-NEXT: br label [[END]]
14 ; CHECK-NEXT: [[TMP0:%.*]] = call i32 @llvm.fshl.i32(i32 [[A:%.*]], i32 [[A]], i32 [[B]])
15 ; CHECK-NEXT: ret i32 [[TMP0]]
18 %cmp = icmp eq i32 %b, 0
19 br i1 %cmp, label %end, label %rotbb
23 %shr = lshr i32 %a, %sub
25 %or = or i32 %shr, %shl
29 %cond = phi i32 [ %or, %rotbb ], [ %a, %entry ]
33 define i32 @rotl_commute_phi(i32 %a, i32 %b) {
34 ; CHECK-LABEL: @rotl_commute_phi(
36 ; CHECK-NEXT: [[CMP:%.*]] = icmp eq i32 [[B:%.*]], 0
37 ; CHECK-NEXT: br i1 [[CMP]], label [[END:%.*]], label [[ROTBB:%.*]]
39 ; CHECK-NEXT: br label [[END]]
41 ; CHECK-NEXT: [[TMP0:%.*]] = call i32 @llvm.fshl.i32(i32 [[A:%.*]], i32 [[A]], i32 [[B]])
42 ; CHECK-NEXT: ret i32 [[TMP0]]
45 %cmp = icmp eq i32 %b, 0
46 br i1 %cmp, label %end, label %rotbb
50 %shr = lshr i32 %a, %sub
52 %or = or i32 %shr, %shl
56 %cond = phi i32 [ %a, %entry ], [ %or, %rotbb ]
60 define i32 @rotl_commute_or(i32 %a, i32 %b) {
61 ; CHECK-LABEL: @rotl_commute_or(
63 ; CHECK-NEXT: [[CMP:%.*]] = icmp eq i32 [[B:%.*]], 0
64 ; CHECK-NEXT: br i1 [[CMP]], label [[END:%.*]], label [[ROTBB:%.*]]
66 ; CHECK-NEXT: br label [[END]]
68 ; CHECK-NEXT: [[TMP0:%.*]] = call i32 @llvm.fshl.i32(i32 [[A:%.*]], i32 [[A]], i32 [[B]])
69 ; CHECK-NEXT: ret i32 [[TMP0]]
72 %cmp = icmp eq i32 %b, 0
73 br i1 %cmp, label %end, label %rotbb
77 %shr = lshr i32 %a, %sub
79 %or = or i32 %shl, %shr
83 %cond = phi i32 [ %a, %entry ], [ %or, %rotbb ]
87 ; Verify that the intrinsic is inserted into a valid position.
89 define i32 @rotl_insert_valid_location(i32 %a, i32 %b) {
90 ; CHECK-LABEL: @rotl_insert_valid_location(
92 ; CHECK-NEXT: [[CMP:%.*]] = icmp eq i32 [[B:%.*]], 0
93 ; CHECK-NEXT: br i1 [[CMP]], label [[END:%.*]], label [[ROTBB:%.*]]
95 ; CHECK-NEXT: br label [[END]]
97 ; CHECK-NEXT: [[OTHER:%.*]] = phi i32 [ 1, [[ROTBB]] ], [ 2, [[ENTRY:%.*]] ]
98 ; CHECK-NEXT: [[TMP0:%.*]] = call i32 @llvm.fshl.i32(i32 [[A:%.*]], i32 [[A]], i32 [[B]])
99 ; CHECK-NEXT: [[RES:%.*]] = or i32 [[TMP0]], [[OTHER]]
100 ; CHECK-NEXT: ret i32 [[RES]]
103 %cmp = icmp eq i32 %b, 0
104 br i1 %cmp, label %end, label %rotbb
107 %sub = sub i32 32, %b
108 %shr = lshr i32 %a, %sub
109 %shl = shl i32 %a, %b
110 %or = or i32 %shr, %shl
114 %cond = phi i32 [ %or, %rotbb ], [ %a, %entry ]
115 %other = phi i32 [ 1, %rotbb ], [ 2, %entry ]
116 %res = or i32 %cond, %other
120 define i32 @rotr(i32 %a, i32 %b) {
121 ; CHECK-LABEL: @rotr(
123 ; CHECK-NEXT: [[CMP:%.*]] = icmp eq i32 [[B:%.*]], 0
124 ; CHECK-NEXT: br i1 [[CMP]], label [[END:%.*]], label [[ROTBB:%.*]]
126 ; CHECK-NEXT: br label [[END]]
128 ; CHECK-NEXT: [[TMP0:%.*]] = call i32 @llvm.fshr.i32(i32 [[A:%.*]], i32 [[A]], i32 [[B]])
129 ; CHECK-NEXT: ret i32 [[TMP0]]
132 %cmp = icmp eq i32 %b, 0
133 br i1 %cmp, label %end, label %rotbb
136 %sub = sub i32 32, %b
137 %shl = shl i32 %a, %sub
138 %shr = lshr i32 %a, %b
139 %or = or i32 %shr, %shl
143 %cond = phi i32 [ %or, %rotbb ], [ %a, %entry ]
147 define i32 @rotr_commute_phi(i32 %a, i32 %b) {
148 ; CHECK-LABEL: @rotr_commute_phi(
150 ; CHECK-NEXT: [[CMP:%.*]] = icmp eq i32 [[B:%.*]], 0
151 ; CHECK-NEXT: br i1 [[CMP]], label [[END:%.*]], label [[ROTBB:%.*]]
153 ; CHECK-NEXT: br label [[END]]
155 ; CHECK-NEXT: [[TMP0:%.*]] = call i32 @llvm.fshr.i32(i32 [[A:%.*]], i32 [[A]], i32 [[B]])
156 ; CHECK-NEXT: ret i32 [[TMP0]]
159 %cmp = icmp eq i32 %b, 0
160 br i1 %cmp, label %end, label %rotbb
163 %sub = sub i32 32, %b
164 %shl = shl i32 %a, %sub
165 %shr = lshr i32 %a, %b
166 %or = or i32 %shr, %shl
170 %cond = phi i32 [ %a, %entry ], [ %or, %rotbb ]
174 define i32 @rotr_commute_or(i32 %a, i32 %b) {
175 ; CHECK-LABEL: @rotr_commute_or(
177 ; CHECK-NEXT: [[CMP:%.*]] = icmp eq i32 [[B:%.*]], 0
178 ; CHECK-NEXT: br i1 [[CMP]], label [[END:%.*]], label [[ROTBB:%.*]]
180 ; CHECK-NEXT: br label [[END]]
182 ; CHECK-NEXT: [[TMP0:%.*]] = call i32 @llvm.fshr.i32(i32 [[A:%.*]], i32 [[A]], i32 [[B]])
183 ; CHECK-NEXT: ret i32 [[TMP0]]
186 %cmp = icmp eq i32 %b, 0
187 br i1 %cmp, label %end, label %rotbb
190 %sub = sub i32 32, %b
191 %shl = shl i32 %a, %sub
192 %shr = lshr i32 %a, %b
193 %or = or i32 %shl, %shr
197 %cond = phi i32 [ %a, %entry ], [ %or, %rotbb ]
201 ; Negative test - non-power-of-2 might require urem expansion in the backend.
203 define i12 @could_be_rotr_weird_type(i12 %a, i12 %b) {
204 ; CHECK-LABEL: @could_be_rotr_weird_type(
206 ; CHECK-NEXT: [[CMP:%.*]] = icmp eq i12 [[B:%.*]], 0
207 ; CHECK-NEXT: br i1 [[CMP]], label [[END:%.*]], label [[ROTBB:%.*]]
209 ; CHECK-NEXT: [[SUB:%.*]] = sub i12 12, [[B]]
210 ; CHECK-NEXT: [[SHL:%.*]] = shl i12 [[A:%.*]], [[SUB]]
211 ; CHECK-NEXT: [[SHR:%.*]] = lshr i12 [[A]], [[B]]
212 ; CHECK-NEXT: [[OR:%.*]] = or i12 [[SHL]], [[SHR]]
213 ; CHECK-NEXT: br label [[END]]
215 ; CHECK-NEXT: [[COND:%.*]] = phi i12 [ [[A]], [[ENTRY:%.*]] ], [ [[OR]], [[ROTBB]] ]
216 ; CHECK-NEXT: ret i12 [[COND]]
219 %cmp = icmp eq i12 %b, 0
220 br i1 %cmp, label %end, label %rotbb
223 %sub = sub i12 12, %b
224 %shl = shl i12 %a, %sub
225 %shr = lshr i12 %a, %b
226 %or = or i12 %shl, %shr
230 %cond = phi i12 [ %a, %entry ], [ %or, %rotbb ]
234 ; Negative test - wrong phi ops.
236 define i32 @not_rotr_1(i32 %a, i32 %b) {
237 ; CHECK-LABEL: @not_rotr_1(
239 ; CHECK-NEXT: [[CMP:%.*]] = icmp eq i32 [[B:%.*]], 0
240 ; CHECK-NEXT: br i1 [[CMP]], label [[END:%.*]], label [[ROTBB:%.*]]
242 ; CHECK-NEXT: [[SUB:%.*]] = sub i32 32, [[B]]
243 ; CHECK-NEXT: [[SHL:%.*]] = shl i32 [[A:%.*]], [[SUB]]
244 ; CHECK-NEXT: [[SHR:%.*]] = lshr i32 [[A]], [[B]]
245 ; CHECK-NEXT: [[OR:%.*]] = or i32 [[SHL]], [[SHR]]
246 ; CHECK-NEXT: br label [[END]]
248 ; CHECK-NEXT: [[COND:%.*]] = phi i32 [ [[B]], [[ENTRY:%.*]] ], [ [[OR]], [[ROTBB]] ]
249 ; CHECK-NEXT: ret i32 [[COND]]
252 %cmp = icmp eq i32 %b, 0
253 br i1 %cmp, label %end, label %rotbb
256 %sub = sub i32 32, %b
257 %shl = shl i32 %a, %sub
258 %shr = lshr i32 %a, %b
259 %or = or i32 %shl, %shr
263 %cond = phi i32 [ %b, %entry ], [ %or, %rotbb ]
267 ; Negative test - too many phi ops.
269 define i32 @not_rotr_2(i32 %a, i32 %b, i32 %c) {
270 ; CHECK-LABEL: @not_rotr_2(
272 ; CHECK-NEXT: [[CMP:%.*]] = icmp eq i32 [[B:%.*]], 0
273 ; CHECK-NEXT: br i1 [[CMP]], label [[END:%.*]], label [[ROTBB:%.*]]
275 ; CHECK-NEXT: [[SUB:%.*]] = sub i32 32, [[B]]
276 ; CHECK-NEXT: [[SHL:%.*]] = shl i32 [[A:%.*]], [[SUB]]
277 ; CHECK-NEXT: [[SHR:%.*]] = lshr i32 [[A]], [[B]]
278 ; CHECK-NEXT: [[OR:%.*]] = or i32 [[SHL]], [[SHR]]
279 ; CHECK-NEXT: [[CMP42:%.*]] = icmp ugt i32 [[OR]], 42
280 ; CHECK-NEXT: br i1 [[CMP42]], label [[END]], label [[BOGUS:%.*]]
282 ; CHECK-NEXT: br label [[END]]
284 ; CHECK-NEXT: [[COND:%.*]] = phi i32 [ [[A]], [[ENTRY:%.*]] ], [ [[OR]], [[ROTBB]] ], [ [[C:%.*]], [[BOGUS]] ]
285 ; CHECK-NEXT: ret i32 [[COND]]
288 %cmp = icmp eq i32 %b, 0
289 br i1 %cmp, label %end, label %rotbb
292 %sub = sub i32 32, %b
293 %shl = shl i32 %a, %sub
294 %shr = lshr i32 %a, %b
295 %or = or i32 %shl, %shr
296 %cmp42 = icmp ugt i32 %or, 42
297 br i1 %cmp42, label %end, label %bogus
303 %cond = phi i32 [ %a, %entry ], [ %or, %rotbb ], [ %c, %bogus ]
307 ; Negative test - wrong cmp (but this should match?).
309 define i32 @not_rotr_3(i32 %a, i32 %b) {
310 ; CHECK-LABEL: @not_rotr_3(
312 ; CHECK-NEXT: [[CMP:%.*]] = icmp sle i32 [[B:%.*]], 0
313 ; CHECK-NEXT: br i1 [[CMP]], label [[END:%.*]], label [[ROTBB:%.*]]
315 ; CHECK-NEXT: [[SUB:%.*]] = sub i32 32, [[B]]
316 ; CHECK-NEXT: [[SHL:%.*]] = shl i32 [[A:%.*]], [[SUB]]
317 ; CHECK-NEXT: [[SHR:%.*]] = lshr i32 [[A]], [[B]]
318 ; CHECK-NEXT: [[OR:%.*]] = or i32 [[SHL]], [[SHR]]
319 ; CHECK-NEXT: br label [[END]]
321 ; CHECK-NEXT: [[COND:%.*]] = phi i32 [ [[A]], [[ENTRY:%.*]] ], [ [[OR]], [[ROTBB]] ]
322 ; CHECK-NEXT: ret i32 [[COND]]
325 %cmp = icmp sle i32 %b, 0
326 br i1 %cmp, label %end, label %rotbb
329 %sub = sub i32 32, %b
330 %shl = shl i32 %a, %sub
331 %shr = lshr i32 %a, %b
332 %or = or i32 %shl, %shr
336 %cond = phi i32 [ %a, %entry ], [ %or, %rotbb ]
340 ; Negative test - wrong shift.
342 define i32 @not_rotr_4(i32 %a, i32 %b) {
343 ; CHECK-LABEL: @not_rotr_4(
345 ; CHECK-NEXT: [[CMP:%.*]] = icmp eq i32 [[B:%.*]], 0
346 ; CHECK-NEXT: br i1 [[CMP]], label [[END:%.*]], label [[ROTBB:%.*]]
348 ; CHECK-NEXT: [[SUB:%.*]] = sub i32 32, [[B]]
349 ; CHECK-NEXT: [[SHL:%.*]] = shl i32 [[A:%.*]], [[SUB]]
350 ; CHECK-NEXT: [[SHR:%.*]] = ashr i32 [[A]], [[B]]
351 ; CHECK-NEXT: [[OR:%.*]] = or i32 [[SHL]], [[SHR]]
352 ; CHECK-NEXT: br label [[END]]
354 ; CHECK-NEXT: [[COND:%.*]] = phi i32 [ [[A]], [[ENTRY:%.*]] ], [ [[OR]], [[ROTBB]] ]
355 ; CHECK-NEXT: ret i32 [[COND]]
358 %cmp = icmp eq i32 %b, 0
359 br i1 %cmp, label %end, label %rotbb
362 %sub = sub i32 32, %b
363 %shl = shl i32 %a, %sub
364 %shr = ashr i32 %a, %b
365 %or = or i32 %shl, %shr
369 %cond = phi i32 [ %a, %entry ], [ %or, %rotbb ]
373 ; Negative test - wrong shift for rotate (but can be folded to a generic funnel shift).
375 define i32 @not_rotr_5(i32 %a, i32 %b) {
376 ; CHECK-LABEL: @not_rotr_5(
378 ; CHECK-NEXT: [[CMP:%.*]] = icmp eq i32 [[B:%.*]], 0
379 ; CHECK-NEXT: br i1 [[CMP]], label [[END:%.*]], label [[ROTBB:%.*]]
381 ; CHECK-NEXT: br label [[END]]
383 ; CHECK-NEXT: [[TMP0:%.*]] = call i32 @llvm.fshr.i32(i32 [[B]], i32 [[A:%.*]], i32 [[B]])
384 ; CHECK-NEXT: ret i32 [[TMP0]]
387 %cmp = icmp eq i32 %b, 0
388 br i1 %cmp, label %end, label %rotbb
391 %sub = sub i32 32, %b
392 %shl = shl i32 %b, %sub
393 %shr = lshr i32 %a, %b
394 %or = or i32 %shl, %shr
398 %cond = phi i32 [ %a, %entry ], [ %or, %rotbb ]
402 ; Negative test - wrong sub.
404 define i32 @not_rotr_6(i32 %a, i32 %b) {
405 ; CHECK-LABEL: @not_rotr_6(
407 ; CHECK-NEXT: [[CMP:%.*]] = icmp eq i32 [[B:%.*]], 0
408 ; CHECK-NEXT: br i1 [[CMP]], label [[END:%.*]], label [[ROTBB:%.*]]
410 ; CHECK-NEXT: [[SUB:%.*]] = sub i32 8, [[B]]
411 ; CHECK-NEXT: [[SHL:%.*]] = shl i32 [[A:%.*]], [[SUB]]
412 ; CHECK-NEXT: [[SHR:%.*]] = lshr i32 [[A]], [[B]]
413 ; CHECK-NEXT: [[OR:%.*]] = or i32 [[SHL]], [[SHR]]
414 ; CHECK-NEXT: br label [[END]]
416 ; CHECK-NEXT: [[COND:%.*]] = phi i32 [ [[A]], [[ENTRY:%.*]] ], [ [[OR]], [[ROTBB]] ]
417 ; CHECK-NEXT: ret i32 [[COND]]
420 %cmp = icmp eq i32 %b, 0
421 br i1 %cmp, label %end, label %rotbb
425 %shl = shl i32 %a, %sub
426 %shr = lshr i32 %a, %b
427 %or = or i32 %shl, %shr
431 %cond = phi i32 [ %a, %entry ], [ %or, %rotbb ]
435 ; Negative test - extra use. Technically, we could transform this
436 ; because it doesn't increase the instruction count, but we're
437 ; being cautious not to cause a potential perf pessimization for
438 ; targets that do not have a rotate instruction.
440 define i32 @could_be_rotr(i32 %a, i32 %b, ptr %p) {
441 ; CHECK-LABEL: @could_be_rotr(
443 ; CHECK-NEXT: [[CMP:%.*]] = icmp eq i32 [[B:%.*]], 0
444 ; CHECK-NEXT: br i1 [[CMP]], label [[END:%.*]], label [[ROTBB:%.*]]
446 ; CHECK-NEXT: [[SUB:%.*]] = sub i32 32, [[B]]
447 ; CHECK-NEXT: [[SHL:%.*]] = shl i32 [[A:%.*]], [[SUB]]
448 ; CHECK-NEXT: [[SHR:%.*]] = lshr i32 [[A]], [[B]]
449 ; CHECK-NEXT: [[OR:%.*]] = or i32 [[SHL]], [[SHR]]
450 ; CHECK-NEXT: store i32 [[OR]], ptr [[P:%.*]], align 4
451 ; CHECK-NEXT: br label [[END]]
453 ; CHECK-NEXT: [[COND:%.*]] = phi i32 [ [[A]], [[ENTRY:%.*]] ], [ [[OR]], [[ROTBB]] ]
454 ; CHECK-NEXT: ret i32 [[COND]]
457 %cmp = icmp eq i32 %b, 0
458 br i1 %cmp, label %end, label %rotbb
461 %sub = sub i32 32, %b
462 %shl = shl i32 %a, %sub
463 %shr = lshr i32 %a, %b
464 %or = or i32 %shl, %shr
465 store i32 %or, ptr %p
469 %cond = phi i32 [ %a, %entry ], [ %or, %rotbb ]