1 ; NOTE: Assertions have been autogenerated by utils/update_test_checks.py
2 ; RUN: opt < %s -passes=instcombine -S | FileCheck %s
4 define i1 @f(i1 %cond, i32 %x, i32 %x2) {
6 ; CHECK-NEXT: [[C:%.*]] = icmp eq i32 [[X:%.*]], [[X2:%.*]]
7 ; CHECK-NEXT: ret i1 [[C]]
9 %y = select i1 %cond, i32 poison, i32 %x
10 %c = icmp eq i32 %y, %x2
14 define i1 @icmp_ne_common_op00(i1 %c, i6 %x, i6 %y, i6 %z) {
15 ; CHECK-LABEL: @icmp_ne_common_op00(
16 ; CHECK-NEXT: [[R_V:%.*]] = select i1 [[C:%.*]], i6 [[Y:%.*]], i6 [[Z:%.*]]
17 ; CHECK-NEXT: [[R:%.*]] = icmp ne i6 [[R_V]], [[X:%.*]]
18 ; CHECK-NEXT: ret i1 [[R]]
20 %cmp1 = icmp ne i6 %x, %y
21 %cmp2 = icmp ne i6 %x, %z
22 %r = select i1 %c, i1 %cmp1, i1 %cmp2
26 define i1 @icmp_ne_common_op01(i1 %c, i3 %x, i3 %y, i3 %z) {
27 ; CHECK-LABEL: @icmp_ne_common_op01(
28 ; CHECK-NEXT: [[R_V:%.*]] = select i1 [[C:%.*]], i3 [[Y:%.*]], i3 [[Z:%.*]]
29 ; CHECK-NEXT: [[R:%.*]] = icmp ne i3 [[R_V]], [[X:%.*]]
30 ; CHECK-NEXT: ret i1 [[R]]
32 %cmp1 = icmp ne i3 %x, %y
33 %cmp2 = icmp ne i3 %z, %x
34 %r = select i1 %c, i1 %cmp1, i1 %cmp2
38 define i1 @icmp_ne_common_op10(i1 %c, i4 %x, i4 %y, i4 %z) {
39 ; CHECK-LABEL: @icmp_ne_common_op10(
40 ; CHECK-NEXT: [[R_V:%.*]] = select i1 [[C:%.*]], i4 [[Y:%.*]], i4 [[Z:%.*]]
41 ; CHECK-NEXT: [[R:%.*]] = icmp ne i4 [[R_V]], [[X:%.*]]
42 ; CHECK-NEXT: ret i1 [[R]]
44 %cmp1 = icmp ne i4 %y, %x
45 %cmp2 = icmp ne i4 %x, %z
46 %r = select i1 %c, i1 %cmp1, i1 %cmp2
50 define <3 x i1> @icmp_ne_common_op11(<3 x i1> %c, <3 x i17> %x, <3 x i17> %y, <3 x i17> %z) {
51 ; CHECK-LABEL: @icmp_ne_common_op11(
52 ; CHECK-NEXT: [[R_V:%.*]] = select <3 x i1> [[C:%.*]], <3 x i17> [[Y:%.*]], <3 x i17> [[Z:%.*]]
53 ; CHECK-NEXT: [[R:%.*]] = icmp ne <3 x i17> [[R_V]], [[X:%.*]]
54 ; CHECK-NEXT: ret <3 x i1> [[R]]
56 %cmp1 = icmp ne <3 x i17> %y, %x
57 %cmp2 = icmp ne <3 x i17> %z, %x
58 %r = select <3 x i1> %c, <3 x i1> %cmp1, <3 x i1> %cmp2
62 define i1 @icmp_eq_common_op00(i1 %c, i5 %x, i5 %y, i5 %z) {
63 ; CHECK-LABEL: @icmp_eq_common_op00(
64 ; CHECK-NEXT: [[R_V:%.*]] = select i1 [[C:%.*]], i5 [[Y:%.*]], i5 [[Z:%.*]]
65 ; CHECK-NEXT: [[R:%.*]] = icmp eq i5 [[R_V]], [[X:%.*]]
66 ; CHECK-NEXT: ret i1 [[R]]
68 %cmp1 = icmp eq i5 %x, %y
69 %cmp2 = icmp eq i5 %x, %z
70 %r = select i1 %c, i1 %cmp1, i1 %cmp2
74 define <5 x i1> @icmp_eq_common_op01(<5 x i1> %c, <5 x i7> %x, <5 x i7> %y, <5 x i7> %z) {
75 ; CHECK-LABEL: @icmp_eq_common_op01(
76 ; CHECK-NEXT: [[R_V:%.*]] = select <5 x i1> [[C:%.*]], <5 x i7> [[Y:%.*]], <5 x i7> [[Z:%.*]]
77 ; CHECK-NEXT: [[R:%.*]] = icmp eq <5 x i7> [[R_V]], [[X:%.*]]
78 ; CHECK-NEXT: ret <5 x i1> [[R]]
80 %cmp1 = icmp eq <5 x i7> %x, %y
81 %cmp2 = icmp eq <5 x i7> %z, %x
82 %r = select <5 x i1> %c, <5 x i1> %cmp1, <5 x i1> %cmp2
86 define i1 @icmp_eq_common_op10(i1 %c, i32 %x, i32 %y, i32 %z) {
87 ; CHECK-LABEL: @icmp_eq_common_op10(
88 ; CHECK-NEXT: [[R_V:%.*]] = select i1 [[C:%.*]], i32 [[Y:%.*]], i32 [[Z:%.*]]
89 ; CHECK-NEXT: [[R:%.*]] = icmp eq i32 [[R_V]], [[X:%.*]]
90 ; CHECK-NEXT: ret i1 [[R]]
92 %cmp1 = icmp eq i32 %y, %x
93 %cmp2 = icmp eq i32 %x, %z
94 %r = select i1 %c, i1 %cmp1, i1 %cmp2
98 define i1 @icmp_eq_common_op11(i1 %c, i64 %x, i64 %y, i64 %z) {
99 ; CHECK-LABEL: @icmp_eq_common_op11(
100 ; CHECK-NEXT: [[R_V:%.*]] = select i1 [[C:%.*]], i64 [[Y:%.*]], i64 [[Z:%.*]]
101 ; CHECK-NEXT: [[R:%.*]] = icmp eq i64 [[R_V]], [[X:%.*]]
102 ; CHECK-NEXT: ret i1 [[R]]
104 %cmp1 = icmp eq i64 %y, %x
105 %cmp2 = icmp eq i64 %z, %x
106 %r = select i1 %c, i1 %cmp1, i1 %cmp2
110 define i1 @icmp_common_one_use_1(i1 %c, i8 %x, i8 %y, i8 %z) {
111 ; CHECK-LABEL: @icmp_common_one_use_1(
112 ; CHECK-NEXT: [[CMP1:%.*]] = icmp eq i8 [[Y:%.*]], [[X:%.*]]
113 ; CHECK-NEXT: call void @use(i1 [[CMP1]])
114 ; CHECK-NEXT: [[R_V:%.*]] = select i1 [[C:%.*]], i8 [[Y]], i8 [[Z:%.*]]
115 ; CHECK-NEXT: [[R:%.*]] = icmp eq i8 [[R_V]], [[X]]
116 ; CHECK-NEXT: ret i1 [[R]]
118 %cmp1 = icmp eq i8 %y, %x
119 call void @use(i1 %cmp1)
120 %cmp2 = icmp eq i8 %z, %x
121 %r = select i1 %c, i1 %cmp1, i1 %cmp2
125 define i1 @icmp_slt_common(i1 %c, i6 %x, i6 %y, i6 %z) {
126 ; CHECK-LABEL: @icmp_slt_common(
127 ; CHECK-NEXT: [[R_V:%.*]] = select i1 [[C:%.*]], i6 [[Y:%.*]], i6 [[Z:%.*]]
128 ; CHECK-NEXT: [[R:%.*]] = icmp sgt i6 [[R_V]], [[X:%.*]]
129 ; CHECK-NEXT: ret i1 [[R]]
131 %cmp1 = icmp slt i6 %x, %y
132 %cmp2 = icmp slt i6 %x, %z
133 %r = select i1 %c, i1 %cmp1, i1 %cmp2
137 define i1 @icmp_sgt_common(i1 %c, i6 %x, i6 %y, i6 %z) {
138 ; CHECK-LABEL: @icmp_sgt_common(
139 ; CHECK-NEXT: [[R_V:%.*]] = select i1 [[C:%.*]], i6 [[Y:%.*]], i6 [[Z:%.*]]
140 ; CHECK-NEXT: [[R:%.*]] = icmp slt i6 [[R_V]], [[X:%.*]]
141 ; CHECK-NEXT: ret i1 [[R]]
143 %cmp1 = icmp sgt i6 %x, %y
144 %cmp2 = icmp sgt i6 %x, %z
145 %r = select i1 %c, i1 %cmp1, i1 %cmp2
149 define i1 @icmp_sle_common(i1 %c, i6 %x, i6 %y, i6 %z) {
150 ; CHECK-LABEL: @icmp_sle_common(
151 ; CHECK-NEXT: [[R_V:%.*]] = select i1 [[C:%.*]], i6 [[Y:%.*]], i6 [[Z:%.*]]
152 ; CHECK-NEXT: [[R:%.*]] = icmp sle i6 [[R_V]], [[X:%.*]]
153 ; CHECK-NEXT: ret i1 [[R]]
155 %cmp1 = icmp sle i6 %y, %x
156 %cmp2 = icmp sle i6 %z, %x
157 %r = select i1 %c, i1 %cmp1, i1 %cmp2
161 define i1 @icmp_sge_common(i1 %c, i6 %x, i6 %y, i6 %z) {
162 ; CHECK-LABEL: @icmp_sge_common(
163 ; CHECK-NEXT: [[R_V:%.*]] = select i1 [[C:%.*]], i6 [[Y:%.*]], i6 [[Z:%.*]]
164 ; CHECK-NEXT: [[R:%.*]] = icmp sge i6 [[R_V]], [[X:%.*]]
165 ; CHECK-NEXT: ret i1 [[R]]
167 %cmp1 = icmp sge i6 %y, %x
168 %cmp2 = icmp sge i6 %z, %x
169 %r = select i1 %c, i1 %cmp1, i1 %cmp2
173 define i1 @icmp_slt_sgt_common(i1 %c, i6 %x, i6 %y, i6 %z) {
174 ; CHECK-LABEL: @icmp_slt_sgt_common(
175 ; CHECK-NEXT: [[R_V:%.*]] = select i1 [[C:%.*]], i6 [[Y:%.*]], i6 [[Z:%.*]]
176 ; CHECK-NEXT: [[R:%.*]] = icmp sgt i6 [[R_V]], [[X:%.*]]
177 ; CHECK-NEXT: ret i1 [[R]]
179 %cmp1 = icmp slt i6 %x, %y
180 %cmp2 = icmp sgt i6 %z, %x
181 %r = select i1 %c, i1 %cmp1, i1 %cmp2
185 define i1 @icmp_sle_sge_common(i1 %c, i6 %x, i6 %y, i6 %z) {
186 ; CHECK-LABEL: @icmp_sle_sge_common(
187 ; CHECK-NEXT: [[R_V:%.*]] = select i1 [[C:%.*]], i6 [[Y:%.*]], i6 [[Z:%.*]]
188 ; CHECK-NEXT: [[R:%.*]] = icmp sle i6 [[R_V]], [[X:%.*]]
189 ; CHECK-NEXT: ret i1 [[R]]
191 %cmp1 = icmp sle i6 %y, %x
192 %cmp2 = icmp sge i6 %x, %z
193 %r = select i1 %c, i1 %cmp1, i1 %cmp2
197 define i1 @icmp_ult_common(i1 %c, i6 %x, i6 %y, i6 %z) {
198 ; CHECK-LABEL: @icmp_ult_common(
199 ; CHECK-NEXT: [[R_V:%.*]] = select i1 [[C:%.*]], i6 [[Y:%.*]], i6 [[Z:%.*]]
200 ; CHECK-NEXT: [[R:%.*]] = icmp ugt i6 [[R_V]], [[X:%.*]]
201 ; CHECK-NEXT: ret i1 [[R]]
203 %cmp1 = icmp ult i6 %x, %y
204 %cmp2 = icmp ult i6 %x, %z
205 %r = select i1 %c, i1 %cmp1, i1 %cmp2
209 define i1 @icmp_ule_common(i1 %c, i6 %x, i6 %y, i6 %z) {
210 ; CHECK-LABEL: @icmp_ule_common(
211 ; CHECK-NEXT: [[R_V:%.*]] = select i1 [[C:%.*]], i6 [[Y:%.*]], i6 [[Z:%.*]]
212 ; CHECK-NEXT: [[R:%.*]] = icmp ule i6 [[R_V]], [[X:%.*]]
213 ; CHECK-NEXT: ret i1 [[R]]
215 %cmp1 = icmp ule i6 %y, %x
216 %cmp2 = icmp ule i6 %z, %x
217 %r = select i1 %c, i1 %cmp1, i1 %cmp2
221 define i1 @icmp_ugt_common(i1 %c, i8 %x, i8 %y, i8 %z) {
222 ; CHECK-LABEL: @icmp_ugt_common(
223 ; CHECK-NEXT: [[R_V:%.*]] = select i1 [[C:%.*]], i8 [[Y:%.*]], i8 [[Z:%.*]]
224 ; CHECK-NEXT: [[R:%.*]] = icmp ugt i8 [[R_V]], [[X:%.*]]
225 ; CHECK-NEXT: ret i1 [[R]]
227 %cmp1 = icmp ugt i8 %y, %x
228 %cmp2 = icmp ugt i8 %z, %x
229 %r = select i1 %c, i1 %cmp1, i1 %cmp2
233 define i1 @icmp_uge_common(i1 %c, i6 %x, i6 %y, i6 %z) {
234 ; CHECK-LABEL: @icmp_uge_common(
235 ; CHECK-NEXT: [[R_V:%.*]] = select i1 [[C:%.*]], i6 [[Y:%.*]], i6 [[Z:%.*]]
236 ; CHECK-NEXT: [[R:%.*]] = icmp uge i6 [[R_V]], [[X:%.*]]
237 ; CHECK-NEXT: ret i1 [[R]]
239 %cmp1 = icmp uge i6 %y, %x
240 %cmp2 = icmp uge i6 %z, %x
241 %r = select i1 %c, i1 %cmp1, i1 %cmp2
245 define i1 @icmp_ult_ugt_common(i1 %c, i6 %x, i6 %y, i6 %z) {
246 ; CHECK-LABEL: @icmp_ult_ugt_common(
247 ; CHECK-NEXT: [[R_V:%.*]] = select i1 [[C:%.*]], i6 [[Y:%.*]], i6 [[Z:%.*]]
248 ; CHECK-NEXT: [[R:%.*]] = icmp ugt i6 [[R_V]], [[X:%.*]]
249 ; CHECK-NEXT: ret i1 [[R]]
251 %cmp1 = icmp ult i6 %x, %y
252 %cmp2 = icmp ugt i6 %z, %x
253 %r = select i1 %c, i1 %cmp1, i1 %cmp2
257 define i1 @icmp_ule_uge_common(i1 %c, i6 %x, i6 %y, i6 %z) {
258 ; CHECK-LABEL: @icmp_ule_uge_common(
259 ; CHECK-NEXT: [[R_V:%.*]] = select i1 [[C:%.*]], i6 [[Y:%.*]], i6 [[Z:%.*]]
260 ; CHECK-NEXT: [[R:%.*]] = icmp ule i6 [[R_V]], [[X:%.*]]
261 ; CHECK-NEXT: ret i1 [[R]]
263 %cmp1 = icmp ule i6 %y, %x
264 %cmp2 = icmp uge i6 %x, %z
265 %r = select i1 %c, i1 %cmp1, i1 %cmp2
269 ; negative test: pred is not the same
271 define i1 @icmp_common_pred_different(i1 %c, i8 %x, i8 %y, i8 %z) {
272 ; CHECK-LABEL: @icmp_common_pred_different(
273 ; CHECK-NEXT: [[CMP1:%.*]] = icmp eq i8 [[Y:%.*]], [[X:%.*]]
274 ; CHECK-NEXT: [[CMP2:%.*]] = icmp ne i8 [[Z:%.*]], [[X]]
275 ; CHECK-NEXT: [[R:%.*]] = select i1 [[C:%.*]], i1 [[CMP1]], i1 [[CMP2]]
276 ; CHECK-NEXT: ret i1 [[R]]
278 %cmp1 = icmp eq i8 %y, %x
279 %cmp2 = icmp ne i8 %z, %x
280 %r = select i1 %c, i1 %cmp1, i1 %cmp2
284 ; negative test for non-equality: two pred is not swap
286 define i1 @icmp_common_pred_not_swap(i1 %c, i8 %x, i8 %y, i8 %z) {
287 ; CHECK-LABEL: @icmp_common_pred_not_swap(
288 ; CHECK-NEXT: [[CMP1:%.*]] = icmp slt i8 [[Y:%.*]], [[X:%.*]]
289 ; CHECK-NEXT: [[CMP2:%.*]] = icmp sle i8 [[Z:%.*]], [[X]]
290 ; CHECK-NEXT: [[R:%.*]] = select i1 [[C:%.*]], i1 [[CMP1]], i1 [[CMP2]]
291 ; CHECK-NEXT: ret i1 [[R]]
293 %cmp1 = icmp slt i8 %y, %x
294 %cmp2 = icmp sle i8 %z, %x
295 %r = select i1 %c, i1 %cmp1, i1 %cmp2
299 ; negative test for non-equality: not commute pred
301 define i1 @icmp_common_pred_not_commute_pred(i1 %c, i8 %x, i8 %y, i8 %z) {
302 ; CHECK-LABEL: @icmp_common_pred_not_commute_pred(
303 ; CHECK-NEXT: [[CMP1:%.*]] = icmp slt i8 [[Y:%.*]], [[X:%.*]]
304 ; CHECK-NEXT: [[CMP2:%.*]] = icmp sgt i8 [[Z:%.*]], [[X]]
305 ; CHECK-NEXT: [[R:%.*]] = select i1 [[C:%.*]], i1 [[CMP1]], i1 [[CMP2]]
306 ; CHECK-NEXT: ret i1 [[R]]
308 %cmp1 = icmp slt i8 %y, %x
309 %cmp2 = icmp sgt i8 %z, %x
310 %r = select i1 %c, i1 %cmp1, i1 %cmp2
314 ; negative test: both icmp is not one-use
316 define i1 @icmp_common_one_use_0(i1 %c, i8 %x, i8 %y, i8 %z) {
317 ; CHECK-LABEL: @icmp_common_one_use_0(
318 ; CHECK-NEXT: [[CMP1:%.*]] = icmp eq i8 [[Y:%.*]], [[X:%.*]]
319 ; CHECK-NEXT: call void @use(i1 [[CMP1]])
320 ; CHECK-NEXT: [[CMP2:%.*]] = icmp eq i8 [[Z:%.*]], [[X]]
321 ; CHECK-NEXT: call void @use(i1 [[CMP2]])
322 ; CHECK-NEXT: [[R:%.*]] = select i1 [[C:%.*]], i1 [[CMP1]], i1 [[CMP2]]
323 ; CHECK-NEXT: ret i1 [[R]]
325 %cmp1 = icmp eq i8 %y, %x
326 call void @use(i1 %cmp1)
327 %cmp2 = icmp eq i8 %z, %x
328 call void @use(i1 %cmp2)
329 %r = select i1 %c, i1 %cmp1, i1 %cmp2
333 ; negative test: no common op
335 define i1 @icmp_no_common(i1 %c, i8 %x, i8 %y, i8 %z) {
336 ; CHECK-LABEL: @icmp_no_common(
337 ; CHECK-NEXT: [[CMP1:%.*]] = icmp eq i8 [[Y:%.*]], 0
338 ; CHECK-NEXT: [[CMP2:%.*]] = icmp eq i8 [[Z:%.*]], [[X:%.*]]
339 ; CHECK-NEXT: [[R:%.*]] = select i1 [[C:%.*]], i1 [[CMP1]], i1 [[CMP2]]
340 ; CHECK-NEXT: ret i1 [[R]]
342 %cmp1 = icmp eq i8 %y, 0
343 %cmp2 = icmp eq i8 %z, %x
344 %r = select i1 %c, i1 %cmp1, i1 %cmp2
348 declare void @use(i1)