1 ; NOTE: Assertions have been autogenerated by utils/update_test_checks.py
2 ; RUN: opt < %s -nary-reassociate -S | FileCheck %s
3 ; RUN: opt < %s -passes='nary-reassociate' -S | FileCheck %s
5 declare i32 @llvm.smin.i32(i32 %a, i32 %b)
7 ; m1 = smin(a,b) ; has side uses
8 ; m2 = smin(smin((b,c), a) -> m2 = smin(m1, c)
9 define i32 @smin_test1(i32 %a, i32 %b, i32 %c) {
10 ; CHECK-LABEL: @smin_test1(
11 ; CHECK-NEXT: [[C1:%.*]] = icmp slt i32 [[A:%.*]], [[B:%.*]]
12 ; CHECK-NEXT: [[SMIN1:%.*]] = select i1 [[C1]], i32 [[A]], i32 [[B]]
13 ; CHECK-NEXT: [[SMIN3_NARY:%.*]] = call i32 @llvm.smin.i32(i32 [[SMIN1]], i32 [[C:%.*]])
14 ; CHECK-NEXT: [[RES:%.*]] = add i32 [[SMIN1]], [[SMIN3_NARY]]
15 ; CHECK-NEXT: ret i32 [[RES]]
17 %c1 = icmp slt i32 %a, %b
18 %smin1 = select i1 %c1, i32 %a, i32 %b
19 %c2 = icmp slt i32 %b, %c
20 %smin2 = select i1 %c2, i32 %b, i32 %c
21 %c3 = icmp slt i32 %smin2, %a
22 %smin3 = select i1 %c3, i32 %smin2, i32 %a
23 %res = add i32 %smin1, %smin3
27 ; m1 = smin(a,b) ; has side uses
28 ; m2 = smin(b, (smin(a, c))) -> m2 = smin(m1, c)
29 define i32 @smin_test2(i32 %a, i32 %b, i32 %c) {
30 ; CHECK-LABEL: @smin_test2(
31 ; CHECK-NEXT: [[C1:%.*]] = icmp slt i32 [[A:%.*]], [[B:%.*]]
32 ; CHECK-NEXT: [[SMIN1:%.*]] = select i1 [[C1]], i32 [[A]], i32 [[B]]
33 ; CHECK-NEXT: [[SMIN3_NARY:%.*]] = call i32 @llvm.smin.i32(i32 [[SMIN1]], i32 [[C:%.*]])
34 ; CHECK-NEXT: [[RES:%.*]] = add i32 [[SMIN1]], [[SMIN3_NARY]]
35 ; CHECK-NEXT: ret i32 [[RES]]
37 %c1 = icmp slt i32 %a, %b
38 %smin1 = select i1 %c1, i32 %a, i32 %b
39 %c2 = icmp slt i32 %a, %c
40 %smin2 = select i1 %c2, i32 %a, i32 %c
41 %c3 = icmp slt i32 %b, %smin2
42 %smin3 = select i1 %c3, i32 %b, i32 %smin2
43 %res = add i32 %smin1, %smin3
47 ; Same test as smin_test1 but uses @llvm.smin intrinsic
48 define i32 @smin_test3(i32 %a, i32 %b, i32 %c) {
49 ; CHECK-LABEL: @smin_test3(
50 ; CHECK-NEXT: [[SMIN1:%.*]] = call i32 @llvm.smin.i32(i32 [[A:%.*]], i32 [[B:%.*]])
51 ; CHECK-NEXT: [[SMIN3_NARY:%.*]] = call i32 @llvm.smin.i32(i32 [[SMIN1]], i32 [[C:%.*]])
52 ; CHECK-NEXT: [[RES:%.*]] = add i32 [[SMIN1]], [[SMIN3_NARY]]
53 ; CHECK-NEXT: ret i32 [[RES]]
55 %smin1 = call i32 @llvm.smin.i32(i32 %a, i32 %b)
56 %smin2 = call i32 @llvm.smin.i32(i32 %b, i32 %c)
57 %smin3 = call i32 @llvm.smin.i32(i32 %smin2, i32 %a)
58 %res = add i32 %smin1, %smin3
62 ; m1 = smin(a,b) ; has side uses
63 ; m2 = smin(smin_or_eq((b,c), a) -> m2 = smin(m1, c)
64 define i32 @umin_test4(i32 %a, i32 %b, i32 %c) {
65 ; CHECK-LABEL: @umin_test4(
66 ; CHECK-NEXT: [[C1:%.*]] = icmp slt i32 [[A:%.*]], [[B:%.*]]
67 ; CHECK-NEXT: [[SMIN1:%.*]] = select i1 [[C1]], i32 [[A]], i32 [[B]]
68 ; CHECK-NEXT: [[SMIN3_NARY:%.*]] = call i32 @llvm.smin.i32(i32 [[SMIN1]], i32 [[C:%.*]])
69 ; CHECK-NEXT: [[RES:%.*]] = add i32 [[SMIN1]], [[SMIN3_NARY]]
70 ; CHECK-NEXT: ret i32 [[RES]]
72 %c1 = icmp slt i32 %a, %b
73 %smin1 = select i1 %c1, i32 %a, i32 %b
74 %c2 = icmp sle i32 %b, %c
75 %smin_or_eq2 = select i1 %c2, i32 %b, i32 %c
76 %c3 = icmp slt i32 %smin_or_eq2, %a
77 %smin3 = select i1 %c3, i32 %smin_or_eq2, i32 %a
78 %res = add i32 %smin1, %smin3
82 ; m1 = smin_or_eq(a,b) ; has side uses
83 ; m2 = smin_or_eq(smin((b,c), a) -> m2 = smin(m1, c)
84 define i32 @smin_test5(i32 %a, i32 %b, i32 %c) {
85 ; CHECK-LABEL: @smin_test5(
86 ; CHECK-NEXT: [[C1:%.*]] = icmp sle i32 [[A:%.*]], [[B:%.*]]
87 ; CHECK-NEXT: [[SMIN_OR_EQ1:%.*]] = select i1 [[C1]], i32 [[A]], i32 [[B]]
88 ; CHECK-NEXT: [[SMIN_OR_EQ3_NARY:%.*]] = call i32 @llvm.smin.i32(i32 [[SMIN_OR_EQ1]], i32 [[C:%.*]])
89 ; CHECK-NEXT: [[RES:%.*]] = add i32 [[SMIN_OR_EQ1]], [[SMIN_OR_EQ3_NARY]]
90 ; CHECK-NEXT: ret i32 [[RES]]
92 %c1 = icmp sle i32 %a, %b
93 %smin_or_eq1 = select i1 %c1, i32 %a, i32 %b
94 %c2 = icmp slt i32 %b, %c
95 %smin2 = select i1 %c2, i32 %b, i32 %c
96 %c3 = icmp sle i32 %smin2, %a
97 %smin_or_eq3 = select i1 %c3, i32 %smin2, i32 %a
98 %res = add i32 %smin_or_eq1, %smin_or_eq3
102 ; m1 = smin(a,b) ; has side uses
103 ; m2 = smin(umin((b,c), a) ; check that signed and unsigned mins are not mixed
104 define i32 @smin_test6(i32 %a, i32 %b, i32 %c) {
105 ; CHECK-LABEL: @smin_test6(
106 ; CHECK-NEXT: [[C1:%.*]] = icmp slt i32 [[A:%.*]], [[B:%.*]]
107 ; CHECK-NEXT: [[SMIN1:%.*]] = select i1 [[C1]], i32 [[A]], i32 [[B]]
108 ; CHECK-NEXT: [[C2:%.*]] = icmp ult i32 [[B]], [[C:%.*]]
109 ; CHECK-NEXT: [[UMIN2:%.*]] = select i1 [[C2]], i32 [[B]], i32 [[C]]
110 ; CHECK-NEXT: [[C3:%.*]] = icmp slt i32 [[UMIN2]], [[A]]
111 ; CHECK-NEXT: [[SMIN3:%.*]] = select i1 [[C3]], i32 [[UMIN2]], i32 [[A]]
112 ; CHECK-NEXT: [[RES:%.*]] = add i32 [[SMIN1]], [[SMIN3]]
113 ; CHECK-NEXT: ret i32 [[RES]]
115 %c1 = icmp slt i32 %a, %b
116 %smin1 = select i1 %c1, i32 %a, i32 %b
117 %c2 = icmp ult i32 %b, %c
118 %umin2 = select i1 %c2, i32 %b, i32 %c
119 %c3 = icmp slt i32 %umin2, %a
120 %smin3 = select i1 %c3, i32 %umin2, i32 %a
121 %res = add i32 %smin1, %smin3
125 ; m1 = smin(a,b) ; has side uses
126 ; m2 = smin(smax((b,c), a) ; check that min and max are not mixed
127 define i32 @smin_test7(i32 %a, i32 %b, i32 %c) {
128 ; CHECK-LABEL: @smin_test7(
129 ; CHECK-NEXT: [[C1:%.*]] = icmp slt i32 [[A:%.*]], [[B:%.*]]
130 ; CHECK-NEXT: [[SMIN1:%.*]] = select i1 [[C1]], i32 [[A]], i32 [[B]]
131 ; CHECK-NEXT: [[C2:%.*]] = icmp sgt i32 [[B]], [[C:%.*]]
132 ; CHECK-NEXT: [[SMAX2:%.*]] = select i1 [[C2]], i32 [[B]], i32 [[C]]
133 ; CHECK-NEXT: [[C3:%.*]] = icmp slt i32 [[SMAX2]], [[A]]
134 ; CHECK-NEXT: [[SMIN3:%.*]] = select i1 [[C3]], i32 [[SMAX2]], i32 [[A]]
135 ; CHECK-NEXT: [[RES:%.*]] = add i32 [[SMIN1]], [[SMIN3]]
136 ; CHECK-NEXT: ret i32 [[RES]]
138 %c1 = icmp slt i32 %a, %b
139 %smin1 = select i1 %c1, i32 %a, i32 %b
140 %c2 = icmp sgt i32 %b, %c
141 %smax2 = select i1 %c2, i32 %b, i32 %c
142 %c3 = icmp slt i32 %smax2, %a
143 %smin3 = select i1 %c3, i32 %smax2, i32 %a
144 %res = add i32 %smin1, %smin3
148 ; Pointer types are not supported yet
149 define i32* @smin_test8(i32* %a, i32* %b, i32* %c) {
150 ; CHECK-LABEL: @smin_test8(
151 ; CHECK-NEXT: [[C1:%.*]] = icmp slt i32* [[A:%.*]], [[B:%.*]]
152 ; CHECK-NEXT: [[UMIN1:%.*]] = select i1 [[C1]], i32* [[A]], i32* [[B]]
153 ; CHECK-NEXT: [[C2:%.*]] = icmp slt i32* [[B]], [[C:%.*]]
154 ; CHECK-NEXT: [[UMIN2:%.*]] = select i1 [[C2]], i32* [[B]], i32* [[C]]
155 ; CHECK-NEXT: [[C3:%.*]] = icmp slt i32* [[UMIN2]], [[A]]
156 ; CHECK-NEXT: [[UMIN3:%.*]] = select i1 [[C3]], i32* [[UMIN2]], i32* [[A]]
157 ; CHECK-NEXT: [[C4:%.*]] = icmp slt i32* [[UMIN1]], [[UMIN3]]
158 ; CHECK-NEXT: [[RES:%.*]] = select i1 [[C4]], i32* [[UMIN1]], i32* [[UMIN3]]
159 ; CHECK-NEXT: ret i32* [[RES]]
161 %c1 = icmp slt i32* %a, %b
162 %umin1 = select i1 %c1, i32* %a, i32* %b
163 %c2 = icmp slt i32* %b, %c
164 %umin2 = select i1 %c2, i32* %b, i32* %c
165 %c3 = icmp slt i32* %umin2, %a
166 %umin3 = select i1 %c3, i32* %umin2, i32* %a
167 %c4 = icmp slt i32* %umin1, %umin3
168 %res = select i1 %c4, i32* %umin1, i32* %umin3