• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1; RUN: opt -S -instcombine < %s | FileCheck %s
2
3define i32 @compute_min_2(i32 %x, i32 %y) {
4; CHECK-LABEL: compute_min_2
5 entry:
6  %not_x = sub i32 -1, %x
7  %not_y = sub i32 -1, %y
8  %cmp = icmp sgt i32 %not_x, %not_y
9  %not_min = select i1 %cmp, i32 %not_x, i32 %not_y
10  %min = sub i32 -1, %not_min
11  ret i32 %min
12
13; CHECK: %0 = icmp slt i32 %x, %y
14; CHECK-NEXT: %1 = select i1 %0, i32 %x, i32 %y
15; CHECK-NEXT: ret i32 %1
16}
17
18define i32 @compute_min_3(i32 %x, i32 %y, i32 %z) {
19; CHECK-LABEL: compute_min_3
20 entry:
21  %not_x = sub i32 -1, %x
22  %not_y = sub i32 -1, %y
23  %not_z = sub i32 -1, %z
24  %cmp_1 = icmp sgt i32 %not_x, %not_y
25  %not_min_1 = select i1 %cmp_1, i32 %not_x, i32 %not_y
26  %cmp_2 = icmp sgt i32 %not_min_1, %not_z
27  %not_min_2 = select i1 %cmp_2, i32 %not_min_1, i32 %not_z
28  %min = sub i32 -1, %not_min_2
29  ret i32 %min
30
31; CHECK: %0 = icmp slt i32 %x, %y
32; CHECK-NEXT: %1 = select i1 %0, i32 %x, i32 %y
33; CHECK-NEXT: %2 = icmp slt i32 %1, %z
34; CHECK-NEXT: %3 = select i1 %2, i32 %1, i32 %z
35; CHECK-NEXT: ret i32 %3
36}
37
38define i32 @compute_min_arithmetic(i32 %x, i32 %y) {
39; CHECK-LABEL: compute_min_arithmetic
40 entry:
41  %not_value = sub i32 3, %x
42  %not_y = sub i32 -1, %y
43  %cmp = icmp sgt i32 %not_value, %not_y
44  %not_min = select i1 %cmp, i32 %not_value, i32 %not_y
45  ret i32 %not_min
46
47; CHECK: %0 = add i32 %x, -4
48; CHECK-NEXT: %1 = icmp slt i32 %0, %y
49; CHECK-NEXT: %2 = select i1 %1, i32 %0, i32 %y
50; CHECK-NEXT: %3 = xor i32 %2, -1
51; CHECK-NEXT: ret i32 %3
52}
53
54declare void @fake_use(i32)
55
56define i32 @compute_min_pessimization(i32 %x, i32 %y) {
57; CHECK-LABEL: compute_min_pessimization
58 entry:
59  %not_value = sub i32 3, %x
60  call void @fake_use(i32 %not_value)
61  %not_y = sub i32 -1, %y
62  %cmp = icmp sgt i32 %not_value, %not_y
63; CHECK: %not_value = sub i32 3, %x
64; CHECK: %cmp = icmp sgt i32 %not_value, %not_y
65  %not_min = select i1 %cmp, i32 %not_value, i32 %not_y
66  %min = sub i32 -1, %not_min
67  ret i32 %min
68}
69
70define i32 @max_of_nots(i32 %x, i32 %y) {
71; CHECK-LABEL: @max_of_nots(
72; CHECK-NEXT: icmp
73; CHECK-NEXT: select
74; CHECK-NEXT: icmp
75; CHECK-NEXT: select
76; CHECK-NEXT: xor
77; CHECK-NEXT: ret
78  %c0 = icmp sgt i32 %y, 0
79  %xor_y = xor i32 %y, -1
80  %s0 = select i1 %c0, i32 %xor_y, i32 -1
81  %xor_x = xor i32 %x, -1
82  %c1 = icmp slt i32 %s0, %xor_x
83  %smax96 = select i1 %c1, i32 %xor_x, i32 %s0
84  ret i32 %smax96
85}
86