1 ; NOTE: Assertions have been autogenerated by utils/update_test_checks.py
2 ; RUN: opt -S -passes=instcombine < %s | FileCheck %s
4 define i32 @smax_of_smax_smin_commute0(i32 %x, i32 %y) {
5 ; CHECK-LABEL: @smax_of_smax_smin_commute0(
6 ; CHECK-NEXT: [[MAX:%.*]] = call i32 @llvm.smax.i32(i32 [[Y:%.*]], i32 [[X:%.*]])
7 ; CHECK-NEXT: ret i32 [[MAX]]
9 %cmp1 = icmp slt i32 %x, %y
10 %min = select i1 %cmp1, i32 %x, i32 %y
11 %cmp2 = icmp slt i32 %y, %x
12 %max = select i1 %cmp2, i32 %x, i32 %y
13 %cmp3 = icmp sgt i32 %max, %min
14 %r = select i1 %cmp3, i32 %max, i32 %min
18 define i32 @smax_of_smax_smin_commute1(i32 %x, i32 %y) {
19 ; CHECK-LABEL: @smax_of_smax_smin_commute1(
20 ; CHECK-NEXT: [[MAX:%.*]] = call i32 @llvm.smax.i32(i32 [[X:%.*]], i32 [[Y:%.*]])
21 ; CHECK-NEXT: ret i32 [[MAX]]
23 %cmp1 = icmp sgt i32 %x, %y
24 %min = select i1 %cmp1, i32 %y, i32 %x
25 %cmp2 = icmp sgt i32 %x, %y
26 %max = select i1 %cmp2, i32 %x, i32 %y
27 %cmp3 = icmp sgt i32 %max, %min
28 %r = select i1 %cmp3, i32 %max, i32 %min
32 define i32 @smax_of_smax_smin_commute2(i32 %x, i32 %y) {
33 ; CHECK-LABEL: @smax_of_smax_smin_commute2(
34 ; CHECK-NEXT: [[MAX:%.*]] = call i32 @llvm.smax.i32(i32 [[Y:%.*]], i32 [[X:%.*]])
35 ; CHECK-NEXT: ret i32 [[MAX]]
37 %cmp1 = icmp slt i32 %x, %y
38 %min = select i1 %cmp1, i32 %x, i32 %y
39 %cmp2 = icmp slt i32 %y, %x
40 %max = select i1 %cmp2, i32 %x, i32 %y
41 %cmp3 = icmp slt i32 %min, %max
42 %r = select i1 %cmp3, i32 %max, i32 %min
46 define <2 x i32> @smax_of_smax_smin_commute3(<2 x i32> %x, <2 x i32> %y) {
47 ; CHECK-LABEL: @smax_of_smax_smin_commute3(
48 ; CHECK-NEXT: [[MAX:%.*]] = call <2 x i32> @llvm.smax.v2i32(<2 x i32> [[X:%.*]], <2 x i32> [[Y:%.*]])
49 ; CHECK-NEXT: ret <2 x i32> [[MAX]]
51 %cmp1 = icmp sgt <2 x i32> %x, %y
52 %min = select <2 x i1> %cmp1, <2 x i32> %y, <2 x i32> %x
53 %cmp2 = icmp sgt <2 x i32> %x, %y
54 %max = select <2 x i1> %cmp2, <2 x i32> %x, <2 x i32> %y
55 %cmp3 = icmp slt <2 x i32> %min, %max
56 %r = select <2 x i1> %cmp3, <2 x i32> %max, <2 x i32> %min
60 define i32 @smin_of_smin_smax_commute0(i32 %x, i32 %y) {
61 ; CHECK-LABEL: @smin_of_smin_smax_commute0(
62 ; CHECK-NEXT: [[MIN:%.*]] = call i32 @llvm.smin.i32(i32 [[Y:%.*]], i32 [[X:%.*]])
63 ; CHECK-NEXT: ret i32 [[MIN]]
65 %cmp1 = icmp sgt i32 %x, %y
66 %max = select i1 %cmp1, i32 %x, i32 %y
67 %cmp2 = icmp sgt i32 %y, %x
68 %min = select i1 %cmp2, i32 %x, i32 %y
69 %cmp3 = icmp sgt i32 %max, %min
70 %r = select i1 %cmp3, i32 %min, i32 %max
74 define i32 @smin_of_smin_smax_commute1(i32 %x, i32 %y) {
75 ; CHECK-LABEL: @smin_of_smin_smax_commute1(
76 ; CHECK-NEXT: [[MIN:%.*]] = call i32 @llvm.smin.i32(i32 [[X:%.*]], i32 [[Y:%.*]])
77 ; CHECK-NEXT: ret i32 [[MIN]]
79 %cmp1 = icmp slt i32 %x, %y
80 %max = select i1 %cmp1, i32 %y, i32 %x
81 %cmp2 = icmp slt i32 %x, %y
82 %min = select i1 %cmp2, i32 %x, i32 %y
83 %cmp3 = icmp sgt i32 %max, %min
84 %r = select i1 %cmp3, i32 %min, i32 %max
88 define <2 x i32> @smin_of_smin_smax_commute2(<2 x i32> %x, <2 x i32> %y) {
89 ; CHECK-LABEL: @smin_of_smin_smax_commute2(
90 ; CHECK-NEXT: [[MIN:%.*]] = call <2 x i32> @llvm.smin.v2i32(<2 x i32> [[X:%.*]], <2 x i32> [[Y:%.*]])
91 ; CHECK-NEXT: ret <2 x i32> [[MIN]]
93 %cmp1 = icmp sgt <2 x i32> %x, %y
94 %max = select <2 x i1> %cmp1, <2 x i32> %x, <2 x i32> %y
95 %cmp2 = icmp slt <2 x i32> %x, %y
96 %min = select <2 x i1> %cmp2, <2 x i32> %x, <2 x i32> %y
97 %cmp3 = icmp slt <2 x i32> %min, %max
98 %r = select <2 x i1> %cmp3, <2 x i32> %min, <2 x i32> %max
102 define i32 @smin_of_smin_smax_commute3(i32 %x, i32 %y) {
103 ; CHECK-LABEL: @smin_of_smin_smax_commute3(
104 ; CHECK-NEXT: [[MIN:%.*]] = call i32 @llvm.smin.i32(i32 [[Y:%.*]], i32 [[X:%.*]])
105 ; CHECK-NEXT: ret i32 [[MIN]]
107 %cmp1 = icmp slt i32 %x, %y
108 %max = select i1 %cmp1, i32 %y, i32 %x
109 %cmp2 = icmp sgt i32 %y, %x
110 %min = select i1 %cmp2, i32 %x, i32 %y
111 %cmp3 = icmp slt i32 %min, %max
112 %r = select i1 %cmp3, i32 %min, i32 %max
116 define i32 @umax_of_umax_umin_commute0(i32 %x, i32 %y) {
117 ; CHECK-LABEL: @umax_of_umax_umin_commute0(
118 ; CHECK-NEXT: [[MAX:%.*]] = call i32 @llvm.umax.i32(i32 [[Y:%.*]], i32 [[X:%.*]])
119 ; CHECK-NEXT: ret i32 [[MAX]]
121 %cmp1 = icmp ult i32 %x, %y
122 %min = select i1 %cmp1, i32 %x, i32 %y
123 %cmp2 = icmp ult i32 %y, %x
124 %max = select i1 %cmp2, i32 %x, i32 %y
125 %cmp3 = icmp ugt i32 %max, %min
126 %r = select i1 %cmp3, i32 %max, i32 %min
130 define i32 @umax_of_umax_umin_commute1(i32 %x, i32 %y) {
131 ; CHECK-LABEL: @umax_of_umax_umin_commute1(
132 ; CHECK-NEXT: [[MAX:%.*]] = call i32 @llvm.umax.i32(i32 [[X:%.*]], i32 [[Y:%.*]])
133 ; CHECK-NEXT: ret i32 [[MAX]]
135 %cmp1 = icmp ugt i32 %x, %y
136 %min = select i1 %cmp1, i32 %y, i32 %x
137 %cmp2 = icmp ugt i32 %x, %y
138 %max = select i1 %cmp2, i32 %x, i32 %y
139 %cmp3 = icmp ugt i32 %max, %min
140 %r = select i1 %cmp3, i32 %max, i32 %min
144 define i32 @umax_of_umax_umin_commute2(i32 %x, i32 %y) {
145 ; CHECK-LABEL: @umax_of_umax_umin_commute2(
146 ; CHECK-NEXT: [[MAX:%.*]] = call i32 @llvm.umax.i32(i32 [[Y:%.*]], i32 [[X:%.*]])
147 ; CHECK-NEXT: ret i32 [[MAX]]
149 %cmp1 = icmp ult i32 %x, %y
150 %min = select i1 %cmp1, i32 %x, i32 %y
151 %cmp2 = icmp ult i32 %y, %x
152 %max = select i1 %cmp2, i32 %x, i32 %y
153 %cmp3 = icmp ult i32 %min, %max
154 %r = select i1 %cmp3, i32 %max, i32 %min
158 define <2 x i32> @umax_of_umax_umin_commute3(<2 x i32> %x, <2 x i32> %y) {
159 ; CHECK-LABEL: @umax_of_umax_umin_commute3(
160 ; CHECK-NEXT: [[MAX:%.*]] = call <2 x i32> @llvm.umax.v2i32(<2 x i32> [[X:%.*]], <2 x i32> [[Y:%.*]])
161 ; CHECK-NEXT: ret <2 x i32> [[MAX]]
163 %cmp1 = icmp ugt <2 x i32> %x, %y
164 %min = select <2 x i1> %cmp1, <2 x i32> %y, <2 x i32> %x
165 %cmp2 = icmp ugt <2 x i32> %x, %y
166 %max = select <2 x i1> %cmp2, <2 x i32> %x, <2 x i32> %y
167 %cmp3 = icmp ult <2 x i32> %min, %max
168 %r = select <2 x i1> %cmp3, <2 x i32> %max, <2 x i32> %min
172 define i32 @umin_of_umin_umax_commute0(i32 %x, i32 %y) {
173 ; CHECK-LABEL: @umin_of_umin_umax_commute0(
174 ; CHECK-NEXT: [[MIN:%.*]] = call i32 @llvm.umin.i32(i32 [[Y:%.*]], i32 [[X:%.*]])
175 ; CHECK-NEXT: ret i32 [[MIN]]
177 %cmp1 = icmp ugt i32 %x, %y
178 %max = select i1 %cmp1, i32 %x, i32 %y
179 %cmp2 = icmp ugt i32 %y, %x
180 %min = select i1 %cmp2, i32 %x, i32 %y
181 %cmp3 = icmp ugt i32 %max, %min
182 %r = select i1 %cmp3, i32 %min, i32 %max
186 define i32 @umin_of_umin_umax_commute1(i32 %x, i32 %y) {
187 ; CHECK-LABEL: @umin_of_umin_umax_commute1(
188 ; CHECK-NEXT: [[MIN:%.*]] = call i32 @llvm.umin.i32(i32 [[X:%.*]], i32 [[Y:%.*]])
189 ; CHECK-NEXT: ret i32 [[MIN]]
191 %cmp1 = icmp ult i32 %x, %y
192 %max = select i1 %cmp1, i32 %y, i32 %x
193 %cmp2 = icmp ult i32 %x, %y
194 %min = select i1 %cmp2, i32 %x, i32 %y
195 %cmp3 = icmp ugt i32 %max, %min
196 %r = select i1 %cmp3, i32 %min, i32 %max
200 define <2 x i32> @umin_of_umin_umax_commute2(<2 x i32> %x, <2 x i32> %y) {
201 ; CHECK-LABEL: @umin_of_umin_umax_commute2(
202 ; CHECK-NEXT: [[MIN:%.*]] = call <2 x i32> @llvm.umin.v2i32(<2 x i32> [[X:%.*]], <2 x i32> [[Y:%.*]])
203 ; CHECK-NEXT: ret <2 x i32> [[MIN]]
205 %cmp1 = icmp ugt <2 x i32> %x, %y
206 %max = select <2 x i1> %cmp1, <2 x i32> %x, <2 x i32> %y
207 %cmp2 = icmp ult <2 x i32> %x, %y
208 %min = select <2 x i1> %cmp2, <2 x i32> %x, <2 x i32> %y
209 %cmp3 = icmp ult <2 x i32> %min, %max
210 %r = select <2 x i1> %cmp3, <2 x i32> %min, <2 x i32> %max
214 define i32 @umin_of_umin_umax_commute3(i32 %x, i32 %y) {
215 ; CHECK-LABEL: @umin_of_umin_umax_commute3(
216 ; CHECK-NEXT: [[MIN:%.*]] = call i32 @llvm.umin.i32(i32 [[Y:%.*]], i32 [[X:%.*]])
217 ; CHECK-NEXT: ret i32 [[MIN]]
219 %cmp1 = icmp ult i32 %x, %y
220 %max = select i1 %cmp1, i32 %y, i32 %x
221 %cmp2 = icmp ugt i32 %y, %x
222 %min = select i1 %cmp2, i32 %x, i32 %y
223 %cmp3 = icmp ult i32 %min, %max
224 %r = select i1 %cmp3, i32 %min, i32 %max
228 ; Negative test - mismatch of min/max flavor
230 define i32 @umin_of_smin_umax_wrong_pattern(i32 %x, i32 %y) {
231 ; CHECK-LABEL: @umin_of_smin_umax_wrong_pattern(
232 ; CHECK-NEXT: [[MIN:%.*]] = call i32 @llvm.smin.i32(i32 [[Y:%.*]], i32 [[X:%.*]])
233 ; CHECK-NEXT: ret i32 [[MIN]]
235 %cmp1 = icmp ugt i32 %x, %y
236 %max = select i1 %cmp1, i32 %x, i32 %y
237 %cmp2 = icmp sgt i32 %y, %x
238 %min = select i1 %cmp2, i32 %x, i32 %y
239 %cmp3 = icmp ugt i32 %max, %min
240 %r = select i1 %cmp3, i32 %min, i32 %max
244 ; Negative test - mismatch of min/max flavor
246 define i32 @smin_of_umin_umax_wrong_pattern2(i32 %x, i32 %y) {
247 ; CHECK-LABEL: @smin_of_umin_umax_wrong_pattern2(
248 ; CHECK-NEXT: [[R:%.*]] = call i32 @llvm.smin.i32(i32 [[X:%.*]], i32 [[Y:%.*]])
249 ; CHECK-NEXT: ret i32 [[R]]
251 %cmp1 = icmp ult i32 %x, %y
252 %max = select i1 %cmp1, i32 %y, i32 %x
253 %cmp2 = icmp ult i32 %x, %y
254 %min = select i1 %cmp2, i32 %x, i32 %y
255 %cmp3 = icmp sgt i32 %max, %min
256 %r = select i1 %cmp3, i32 %min, i32 %max
260 ; Negative test - operands must match
262 define <2 x i32> @umin_of_umin_umax_wrong_operand(<2 x i32> %x, <2 x i32> %y, <2 x i32> %z) {
263 ; CHECK-LABEL: @umin_of_umin_umax_wrong_operand(
264 ; CHECK-NEXT: [[MIN:%.*]] = call <2 x i32> @llvm.umin.v2i32(<2 x i32> [[X:%.*]], <2 x i32> [[Z:%.*]])
265 ; CHECK-NEXT: ret <2 x i32> [[MIN]]
267 %cmp1 = icmp ugt <2 x i32> %x, %y
268 %max = select <2 x i1> %cmp1, <2 x i32> %x, <2 x i32> %y
269 %cmp2 = icmp ult <2 x i32> %x, %z
270 %min = select <2 x i1> %cmp2, <2 x i32> %x, <2 x i32> %z
271 %cmp3 = icmp ult <2 x i32> %min, %max
272 %r = select <2 x i1> %cmp3, <2 x i32> %min, <2 x i32> %max
276 ; Negative test - operands must match
278 define i32 @umin_of_umin_umax_wrong_operand2(i32 %x, i32 %y, i32 %z) {
279 ; CHECK-LABEL: @umin_of_umin_umax_wrong_operand2(
280 ; CHECK-NEXT: [[MIN:%.*]] = call i32 @llvm.umin.i32(i32 [[Y:%.*]], i32 [[X:%.*]])
281 ; CHECK-NEXT: ret i32 [[MIN]]
283 %cmp1 = icmp ult i32 %x, %z
284 %max = select i1 %cmp1, i32 %z, i32 %x
285 %cmp2 = icmp ugt i32 %y, %x
286 %min = select i1 %cmp2, i32 %x, i32 %y
287 %cmp3 = icmp ult i32 %min, %max
288 %r = select i1 %cmp3, i32 %min, i32 %max