1 ; NOTE: Assertions have been autogenerated by utils/update_test_checks.py
2 ; RUN: opt < %s -passes=instsimplify -S | FileCheck %s
4 ; A == B implies A >u B is false.
6 define void @test1(i32 %a, i32 %b) {
8 ; CHECK-NEXT: [[CMP1:%.*]] = icmp eq i32 [[A:%.*]], [[B:%.*]]
9 ; CHECK-NEXT: br i1 [[CMP1]], label [[TAKEN:%.*]], label [[END:%.*]]
11 ; CHECK-NEXT: call void @foo(i32 10)
12 ; CHECK-NEXT: br label [[END]]
14 ; CHECK-NEXT: ret void
16 %cmp1 = icmp eq i32 %a, %b
17 br i1 %cmp1, label %taken, label %end
20 %cmp2 = icmp ugt i32 %a, %b
21 %c = select i1 %cmp2, i32 0, i32 10
22 call void @foo(i32 %c)
29 ; If A == B is false then A != B is true.
31 define void @test2(i32 %a, i32 %b) {
32 ; CHECK-LABEL: @test2(
33 ; CHECK-NEXT: [[CMP1:%.*]] = icmp eq i32 [[A:%.*]], [[B:%.*]]
34 ; CHECK-NEXT: br i1 [[CMP1]], label [[END:%.*]], label [[TAKEN:%.*]]
36 ; CHECK-NEXT: call void @foo(i32 20)
37 ; CHECK-NEXT: br label [[END]]
39 ; CHECK-NEXT: ret void
41 %cmp1 = icmp eq i32 %a, %b
42 br i1 %cmp1, label %end, label %taken
45 %cmp2 = icmp ne i32 %a, %b
46 %c = select i1 %cmp2, i32 20, i32 0
47 call void @foo(i32 %c)
54 ; A >u 10 implies A >u 10 is true.
56 define void @test3(i32 %a) {
57 ; CHECK-LABEL: @test3(
58 ; CHECK-NEXT: [[CMP1:%.*]] = icmp ugt i32 [[A:%.*]], 10
59 ; CHECK-NEXT: br i1 [[CMP1]], label [[TAKEN:%.*]], label [[END:%.*]]
61 ; CHECK-NEXT: call void @foo(i32 30)
62 ; CHECK-NEXT: br label [[END]]
64 ; CHECK-NEXT: ret void
66 %cmp1 = icmp ugt i32 %a, 10
67 br i1 %cmp1, label %taken, label %end
70 %cmp2 = icmp ugt i32 %a, 10
71 %c = select i1 %cmp2, i32 30, i32 0
72 call void @foo(i32 %c)
79 define i8 @PR23333(ptr addrspace(1) %ptr) {
80 ; CHECK-LABEL: @PR23333(
81 ; CHECK-NEXT: [[CMP:%.*]] = icmp eq ptr addrspace(1) [[PTR:%.*]], null
82 ; CHECK-NEXT: br i1 [[CMP]], label [[TAKEN:%.*]], label [[END:%.*]]
84 ; CHECK-NEXT: ret i8 1
86 ; CHECK-NEXT: ret i8 0
88 %cmp = icmp eq ptr addrspace(1) %ptr, null
89 br i1 %cmp, label %taken, label %end
92 %cmp2 = icmp ne ptr addrspace(1) %ptr, null
93 %res = select i1 %cmp2, i8 2, i8 1
100 ; We know the condition of the select is true based on a dominating condition.
101 ; Therefore, we can replace %cond with %len.
102 ; TODO: len == 8 is known false in bb. This is handled by other passes, but should it be handled here?
104 define void @test4(i32 %len) {
105 ; CHECK-LABEL: @test4(
107 ; CHECK-NEXT: [[TMP0:%.*]] = call i32 @bar(i32 [[LEN:%.*]])
108 ; CHECK-NEXT: [[CMP:%.*]] = icmp ult i32 [[LEN]], 4
109 ; CHECK-NEXT: br i1 [[CMP]], label [[BB:%.*]], label [[B1:%.*]]
111 ; CHECK-NEXT: br i1 false, label [[B0:%.*]], label [[B1]]
113 ; CHECK-NEXT: call void @foo(i32 [[LEN]])
114 ; CHECK-NEXT: br label [[B1]]
116 ; CHECK-NEXT: [[TMP1:%.*]] = phi i32 [ [[LEN]], [[BB]] ], [ undef, [[B0]] ], [ [[TMP0]], [[ENTRY:%.*]] ]
117 ; CHECK-NEXT: br label [[RET:%.*]]
119 ; CHECK-NEXT: call void @foo(i32 [[TMP1]])
120 ; CHECK-NEXT: ret void
123 %0 = call i32 @bar(i32 %len);
124 %cmp = icmp ult i32 %len, 4
125 br i1 %cmp, label %bb, label %b1
127 %cond = select i1 %cmp, i32 %len, i32 8
128 %cmp11 = icmp eq i32 %cond, 8
129 br i1 %cmp11, label %b0, label %b1
132 call void @foo(i32 %len)
136 %1 = phi i32 [ %cond, %bb ], [ undef, %b0 ], [ %0, %entry ]
140 call void @foo(i32 %1)
144 ; A >u 10 implies A >u 9 is true.
146 define void @test5(i32 %a) {
147 ; CHECK-LABEL: @test5(
148 ; CHECK-NEXT: [[CMP1:%.*]] = icmp ugt i32 [[A:%.*]], 10
149 ; CHECK-NEXT: br i1 [[CMP1]], label [[TAKEN:%.*]], label [[END:%.*]]
151 ; CHECK-NEXT: call void @foo(i32 30)
152 ; CHECK-NEXT: br label [[END]]
154 ; CHECK-NEXT: ret void
156 %cmp1 = icmp ugt i32 %a, 10
157 br i1 %cmp1, label %taken, label %end
160 %cmp2 = icmp ugt i32 %a, 9
161 %c = select i1 %cmp2, i32 30, i32 0
162 call void @foo(i32 %c)
169 declare void @foo(i32)
170 declare i32 @bar(i32)
172 define i32 @test_and(i32 %a, i32 %b) {
173 ; CHECK-LABEL: @test_and(
175 ; CHECK-NEXT: [[CMP1:%.*]] = icmp ne i32 [[A:%.*]], 0
176 ; CHECK-NEXT: [[CMP2:%.*]] = icmp ne i32 [[B:%.*]], 0
177 ; CHECK-NEXT: [[AND:%.*]] = and i1 [[CMP1]], [[CMP2]]
178 ; CHECK-NEXT: br i1 [[AND]], label [[TPATH:%.*]], label [[END:%.*]]
180 ; CHECK-NEXT: ret i32 313
182 ; CHECK-NEXT: ret i32 0
185 %cmp1 = icmp ne i32 %a, 0
186 %cmp2 = icmp ne i32 %b, 0
187 %and = and i1 %cmp1, %cmp2
188 br i1 %and, label %tpath, label %end
191 %cmp3 = icmp eq i32 %a, 0 ;; <-- implied false
192 %c = select i1 %cmp3, i32 0, i32 313
199 ; cmp1 and cmp2 are false on the 'fpath' path and thus cmp3 is true.
201 define i32 @test_or1(i32 %a, i32 %b) {
202 ; CHECK-LABEL: @test_or1(
204 ; CHECK-NEXT: [[CMP1:%.*]] = icmp eq i32 [[A:%.*]], 0
205 ; CHECK-NEXT: [[CMP2:%.*]] = icmp eq i32 [[B:%.*]], 0
206 ; CHECK-NEXT: [[OR:%.*]] = or i1 [[CMP1]], [[CMP2]]
207 ; CHECK-NEXT: br i1 [[OR]], label [[END:%.*]], label [[FPATH:%.*]]
209 ; CHECK-NEXT: ret i32 37
211 ; CHECK-NEXT: ret i32 0
214 %cmp1 = icmp eq i32 %a, 0
215 %cmp2 = icmp eq i32 %b, 0
216 %or = or i1 %cmp1, %cmp2
217 br i1 %or, label %end, label %fpath
220 %cmp3 = icmp ne i32 %a, 0 ;; <-- implied true
221 %c = select i1 %cmp3, i32 37, i32 0
228 ; LHS ==> RHS by definition (true -> true)
230 define void @test6(i32 %a, i32 %b) {
231 ; CHECK-LABEL: @test6(
232 ; CHECK-NEXT: [[CMP1:%.*]] = icmp eq i32 [[A:%.*]], [[B:%.*]]
233 ; CHECK-NEXT: br i1 [[CMP1]], label [[TAKEN:%.*]], label [[END:%.*]]
235 ; CHECK-NEXT: call void @foo(i32 10)
236 ; CHECK-NEXT: br label [[END]]
238 ; CHECK-NEXT: ret void
240 %cmp1 = icmp eq i32 %a, %b
241 br i1 %cmp1, label %taken, label %end
244 %c = select i1 %cmp1, i32 10, i32 0
245 call void @foo(i32 %c)
252 ; LHS ==> RHS by definition (false -> false)
254 define void @test7(i32 %a, i32 %b) {
255 ; CHECK-LABEL: @test7(
256 ; CHECK-NEXT: [[CMP1:%.*]] = icmp eq i32 [[A:%.*]], [[B:%.*]]
257 ; CHECK-NEXT: br i1 [[CMP1]], label [[END:%.*]], label [[TAKEN:%.*]]
259 ; CHECK-NEXT: call void @foo(i32 11)
260 ; CHECK-NEXT: br label [[END]]
262 ; CHECK-NEXT: ret void
264 %cmp1 = icmp eq i32 %a, %b
265 br i1 %cmp1, label %end, label %taken
268 %c = select i1 %cmp1, i32 0, i32 11
269 call void @foo(i32 %c)
276 define void @implies_or(i32 %a, i32 %b, i1 %x) {
277 ; CHECK-LABEL: @implies_or(
278 ; CHECK-NEXT: [[CMP1:%.*]] = icmp eq i32 [[A:%.*]], [[B:%.*]]
279 ; CHECK-NEXT: br i1 [[CMP1]], label [[END:%.*]], label [[TAKEN:%.*]]
281 ; CHECK-NEXT: call void @foo(i32 20)
282 ; CHECK-NEXT: br label [[END]]
284 ; CHECK-NEXT: ret void
286 %cmp1 = icmp eq i32 %a, %b
287 br i1 %cmp1, label %end, label %taken
290 %cmp2 = icmp ne i32 %a, %b
291 %or = or i1 %cmp2, %x
292 %c = select i1 %or, i32 20, i32 0
293 call void @foo(i32 %c)
300 define void @implies_or_comm(i32 %a, i32 %b, i1 %x) {
301 ; CHECK-LABEL: @implies_or_comm(
302 ; CHECK-NEXT: [[CMP1:%.*]] = icmp eq i32 [[A:%.*]], [[B:%.*]]
303 ; CHECK-NEXT: br i1 [[CMP1]], label [[END:%.*]], label [[TAKEN:%.*]]
305 ; CHECK-NEXT: call void @foo(i32 20)
306 ; CHECK-NEXT: br label [[END]]
308 ; CHECK-NEXT: ret void
310 %cmp1 = icmp eq i32 %a, %b
311 br i1 %cmp1, label %end, label %taken
314 %cmp2 = icmp ne i32 %a, %b
315 %or = or i1 %x, %cmp2
316 %c = select i1 %or, i32 20, i32 0
317 call void @foo(i32 %c)
324 define void @implies_or_branch_comm(i32 %a, i32 %b, i1 %x) {
325 ; CHECK-LABEL: @implies_or_branch_comm(
326 ; CHECK-NEXT: [[CMP1:%.*]] = icmp ne i32 [[A:%.*]], [[B:%.*]]
327 ; CHECK-NEXT: br i1 [[CMP1]], label [[TAKEN:%.*]], label [[END:%.*]]
329 ; CHECK-NEXT: call void @foo(i32 20)
330 ; CHECK-NEXT: br label [[END]]
332 ; CHECK-NEXT: ret void
334 %cmp1 = icmp ne i32 %a, %b
335 br i1 %cmp1, label %taken, label %end
338 %cmp2 = icmp ne i32 %a, %b
339 %or = or i1 %cmp2, %x
340 %c = select i1 %or, i32 20, i32 0
341 call void @foo(i32 %c)
348 define void @implies_logical_or(i32 %a, i32 %b, i1 %x) {
349 ; CHECK-LABEL: @implies_logical_or(
350 ; CHECK-NEXT: [[CMP1:%.*]] = icmp eq i32 [[A:%.*]], [[B:%.*]]
351 ; CHECK-NEXT: br i1 [[CMP1]], label [[END:%.*]], label [[TAKEN:%.*]]
353 ; CHECK-NEXT: call void @foo(i32 20)
354 ; CHECK-NEXT: br label [[END]]
356 ; CHECK-NEXT: ret void
358 %cmp1 = icmp eq i32 %a, %b
359 br i1 %cmp1, label %end, label %taken
362 %cmp2 = icmp ne i32 %a, %b
363 %or = select i1 %cmp2, i1 true, i1 %x
364 %c = select i1 %or, i32 20, i32 0
365 call void @foo(i32 %c)
372 define void @implies_logical_or_comm(i32 %a, i32 %b, i1 %x) {
373 ; CHECK-LABEL: @implies_logical_or_comm(
374 ; CHECK-NEXT: [[CMP1:%.*]] = icmp eq i32 [[A:%.*]], [[B:%.*]]
375 ; CHECK-NEXT: br i1 [[CMP1]], label [[END:%.*]], label [[TAKEN:%.*]]
377 ; CHECK-NEXT: call void @foo(i32 20)
378 ; CHECK-NEXT: br label [[END]]
380 ; CHECK-NEXT: ret void
382 %cmp1 = icmp eq i32 %a, %b
383 br i1 %cmp1, label %end, label %taken
386 %cmp2 = icmp ne i32 %a, %b
387 %or = select i1 %x, i1 true, i1 %cmp2
388 %c = select i1 %or, i32 20, i32 0
389 call void @foo(i32 %c)
396 define void @doesnt_imply_and(i32 %a, i32 %b, i1 %x) {
397 ; CHECK-LABEL: @doesnt_imply_and(
398 ; CHECK-NEXT: [[CMP1:%.*]] = icmp eq i32 [[A:%.*]], [[B:%.*]]
399 ; CHECK-NEXT: br i1 [[CMP1]], label [[END:%.*]], label [[TAKEN:%.*]]
401 ; CHECK-NEXT: [[C:%.*]] = select i1 [[X:%.*]], i32 20, i32 0
402 ; CHECK-NEXT: call void @foo(i32 [[C]])
403 ; CHECK-NEXT: br label [[END]]
405 ; CHECK-NEXT: ret void
407 %cmp1 = icmp eq i32 %a, %b
408 br i1 %cmp1, label %end, label %taken
411 %cmp2 = icmp ne i32 %a, %b
412 %or = and i1 %cmp2, %x
413 %c = select i1 %or, i32 20, i32 0
414 call void @foo(i32 %c)
421 define void @implies_not_and(i32 %a, i32 %b, i1 %x) {
422 ; CHECK-LABEL: @implies_not_and(
423 ; CHECK-NEXT: [[CMP1:%.*]] = icmp eq i32 [[A:%.*]], [[B:%.*]]
424 ; CHECK-NEXT: br i1 [[CMP1]], label [[END:%.*]], label [[TAKEN:%.*]]
426 ; CHECK-NEXT: call void @foo(i32 0)
427 ; CHECK-NEXT: br label [[END]]
429 ; CHECK-NEXT: ret void
431 %cmp1 = icmp eq i32 %a, %b
432 br i1 %cmp1, label %end, label %taken
435 %cmp2 = icmp eq i32 %a, %b
436 %and = and i1 %cmp2, %x
437 %c = select i1 %and, i32 20, i32 0
438 call void @foo(i32 %c)
445 define void @implies_not_and_comm(i32 %a, i32 %b, i1 %x) {
446 ; CHECK-LABEL: @implies_not_and_comm(
447 ; CHECK-NEXT: [[CMP1:%.*]] = icmp eq i32 [[A:%.*]], [[B:%.*]]
448 ; CHECK-NEXT: br i1 [[CMP1]], label [[END:%.*]], label [[TAKEN:%.*]]
450 ; CHECK-NEXT: call void @foo(i32 0)
451 ; CHECK-NEXT: br label [[END]]
453 ; CHECK-NEXT: ret void
455 %cmp1 = icmp eq i32 %a, %b
456 br i1 %cmp1, label %end, label %taken
459 %cmp2 = icmp eq i32 %a, %b
460 %and = and i1 %x, %cmp2
461 %c = select i1 %and, i32 20, i32 0
462 call void @foo(i32 %c)
469 define void @implies_not_and_branch_comm(i32 %a, i32 %b, i1 %x) {
470 ; CHECK-LABEL: @implies_not_and_branch_comm(
471 ; CHECK-NEXT: [[CMP1:%.*]] = icmp ne i32 [[A:%.*]], [[B:%.*]]
472 ; CHECK-NEXT: br i1 [[CMP1]], label [[TAKEN:%.*]], label [[END:%.*]]
474 ; CHECK-NEXT: call void @foo(i32 0)
475 ; CHECK-NEXT: br label [[END]]
477 ; CHECK-NEXT: ret void
479 %cmp1 = icmp ne i32 %a, %b
480 br i1 %cmp1, label %taken, label %end
483 %cmp2 = icmp eq i32 %a, %b
484 %and = and i1 %cmp2, %x
485 %c = select i1 %and, i32 20, i32 0
486 call void @foo(i32 %c)
493 define void @implies_not_logical_and(i32 %a, i32 %b, i1 %x) {
494 ; CHECK-LABEL: @implies_not_logical_and(
495 ; CHECK-NEXT: [[CMP1:%.*]] = icmp eq i32 [[A:%.*]], [[B:%.*]]
496 ; CHECK-NEXT: br i1 [[CMP1]], label [[END:%.*]], label [[TAKEN:%.*]]
498 ; CHECK-NEXT: call void @foo(i32 0)
499 ; CHECK-NEXT: br label [[END]]
501 ; CHECK-NEXT: ret void
503 %cmp1 = icmp eq i32 %a, %b
504 br i1 %cmp1, label %end, label %taken
507 %cmp2 = icmp eq i32 %a, %b
508 %and = select i1 %cmp2, i1 %x, i1 false
509 %c = select i1 %and, i32 20, i32 0
510 call void @foo(i32 %c)
517 define void @implies_not_logical_and_comm(i32 %a, i32 %b, i1 %x) {
518 ; CHECK-LABEL: @implies_not_logical_and_comm(
519 ; CHECK-NEXT: [[CMP1:%.*]] = icmp eq i32 [[A:%.*]], [[B:%.*]]
520 ; CHECK-NEXT: br i1 [[CMP1]], label [[END:%.*]], label [[TAKEN:%.*]]
522 ; CHECK-NEXT: call void @foo(i32 0)
523 ; CHECK-NEXT: br label [[END]]
525 ; CHECK-NEXT: ret void
527 %cmp1 = icmp eq i32 %a, %b
528 br i1 %cmp1, label %end, label %taken
531 %cmp2 = icmp eq i32 %a, %b
532 %and = select i1 %x, i1 %cmp2, i1 false
533 %c = select i1 %and, i32 20, i32 0
534 call void @foo(i32 %c)
541 define void @doesnt_imply_or(i32 %a, i32 %b, i1 %x) {
542 ; CHECK-LABEL: @doesnt_imply_or(
543 ; CHECK-NEXT: [[CMP1:%.*]] = icmp eq i32 [[A:%.*]], [[B:%.*]]
544 ; CHECK-NEXT: br i1 [[CMP1]], label [[END:%.*]], label [[TAKEN:%.*]]
546 ; CHECK-NEXT: [[C:%.*]] = select i1 [[X:%.*]], i32 20, i32 0
547 ; CHECK-NEXT: call void @foo(i32 [[C]])
548 ; CHECK-NEXT: br label [[END]]
550 ; CHECK-NEXT: ret void
552 %cmp1 = icmp eq i32 %a, %b
553 br i1 %cmp1, label %end, label %taken
556 %cmp2 = icmp eq i32 %a, %b
557 %and = or i1 %cmp2, %x
558 %c = select i1 %and, i32 20, i32 0
559 call void @foo(i32 %c)