1 ; NOTE: Assertions have been autogenerated by utils/update_test_checks.py
2 ; RUN: opt < %s -passes=instcombine -S | FileCheck %s
4 define i8 @zext_or_icmp_icmp(i8 %a, i8 %b) {
5 ; CHECK-LABEL: @zext_or_icmp_icmp(
6 ; CHECK-NEXT: [[MASK:%.*]] = and i8 [[A:%.*]], 1
7 ; CHECK-NEXT: [[TOBOOL1:%.*]] = icmp eq i8 [[MASK]], 0
8 ; CHECK-NEXT: [[TOBOOL2:%.*]] = icmp eq i8 [[B:%.*]], 0
9 ; CHECK-NEXT: [[BOTHCOND:%.*]] = or i1 [[TOBOOL1]], [[TOBOOL2]]
10 ; CHECK-NEXT: [[ZEXT:%.*]] = zext i1 [[BOTHCOND]] to i8
11 ; CHECK-NEXT: ret i8 [[ZEXT]]
14 %toBool1 = icmp eq i8 %mask, 0
15 %toBool2 = icmp eq i8 %b, 0
16 %bothCond = or i1 %toBool1, %toBool2
17 %zext = zext i1 %bothCond to i8
21 define i8 @zext_or_icmp_icmp_logical(i8 %a, i8 %b) {
22 ; CHECK-LABEL: @zext_or_icmp_icmp_logical(
23 ; CHECK-NEXT: [[MASK:%.*]] = and i8 [[A:%.*]], 1
24 ; CHECK-NEXT: [[TOBOOL1:%.*]] = icmp eq i8 [[MASK]], 0
25 ; CHECK-NEXT: [[TOBOOL2:%.*]] = icmp eq i8 [[B:%.*]], 0
26 ; CHECK-NEXT: [[BOTHCOND:%.*]] = select i1 [[TOBOOL1]], i1 true, i1 [[TOBOOL2]]
27 ; CHECK-NEXT: [[ZEXT:%.*]] = zext i1 [[BOTHCOND]] to i8
28 ; CHECK-NEXT: ret i8 [[ZEXT]]
31 %toBool1 = icmp eq i8 %mask, 0
32 %toBool2 = icmp eq i8 %b, 0
33 %bothCond = select i1 %toBool1, i1 true, i1 %toBool2
34 %zext = zext i1 %bothCond to i8
38 ; Here, widening the or from i1 to i32 and removing one of the icmps would
39 ; widen an undef value (created by the out-of-range shift), increasing the
40 ; range of valid values for the return, so we can't do it.
42 define i32 @dont_widen_undef() {
43 ; CHECK-LABEL: @dont_widen_undef(
45 ; CHECK-NEXT: br label [[BLOCK2:%.*]]
47 ; CHECK-NEXT: br label [[BLOCK2]]
49 ; CHECK-NEXT: ret i32 1
58 %m.011 = phi i32 [ 33, %entry ], [ 0, %block1 ]
59 %cmp.i = icmp ugt i32 %m.011, 1
60 %m.1.op = lshr i32 1, %m.011
61 %sext.mask = and i32 %m.1.op, 65535
62 %cmp115 = icmp ne i32 %sext.mask, 0
63 %cmp1 = or i1 %cmp.i, %cmp115
64 %conv2 = zext i1 %cmp1 to i32
68 define i32 @dont_widen_undef_logical() {
69 ; CHECK-LABEL: @dont_widen_undef_logical(
71 ; CHECK-NEXT: br label [[BLOCK2:%.*]]
73 ; CHECK-NEXT: br label [[BLOCK2]]
75 ; CHECK-NEXT: ret i32 1
84 %m.011 = phi i32 [ 33, %entry ], [ 0, %block1 ]
85 %cmp.i = icmp ugt i32 %m.011, 1
86 %m.1.op = lshr i32 1, %m.011
87 %sext.mask = and i32 %m.1.op, 65535
88 %cmp115 = icmp ne i32 %sext.mask, 0
89 %cmp1 = select i1 %cmp.i, i1 true, i1 %cmp115
90 %conv2 = zext i1 %cmp1 to i32
94 ; A limitation of knownbits with overshift prevents reducing to 'false'.
96 define i1 @knownbits_out_of_range_shift(i32 %x) {
97 ; CHECK-LABEL: @knownbits_out_of_range_shift(
99 ; CHECK-NEXT: br label [[BLOCK2:%.*]]
101 ; CHECK-NEXT: br label [[BLOCK2]]
103 ; CHECK-NEXT: [[P:%.*]] = phi i32 [ 63, [[ENTRY:%.*]] ], [ 31, [[BLOCK1:%.*]] ]
104 ; CHECK-NEXT: [[L:%.*]] = lshr i32 [[X:%.*]], [[P]]
105 ; CHECK-NEXT: [[R:%.*]] = icmp eq i32 [[L]], 2
106 ; CHECK-NEXT: ret i1 [[R]]
115 %p = phi i32 [ 63, %entry ], [ 31, %block1 ]
117 %r = icmp eq i32 %l, 2
123 define i32 @zext_or_eq_ult_add(i32 %i) {
124 ; CHECK-LABEL: @zext_or_eq_ult_add(
125 ; CHECK-NEXT: [[TMP1:%.*]] = add i32 [[I:%.*]], -3
126 ; CHECK-NEXT: [[TMP2:%.*]] = icmp ult i32 [[TMP1]], 3
127 ; CHECK-NEXT: [[R:%.*]] = zext i1 [[TMP2]] to i32
128 ; CHECK-NEXT: ret i32 [[R]]
131 %c1 = icmp ult i32 %a, 3
132 %c2 = icmp eq i32 %i, 5
134 %r = zext i1 %o to i32
138 define i32 @select_zext_or_eq_ult_add(i32 %i) {
139 ; CHECK-LABEL: @select_zext_or_eq_ult_add(
140 ; CHECK-NEXT: [[TMP1:%.*]] = add i32 [[I:%.*]], -3
141 ; CHECK-NEXT: [[TMP2:%.*]] = icmp ult i32 [[TMP1]], 3
142 ; CHECK-NEXT: [[R:%.*]] = zext i1 [[TMP2]] to i32
143 ; CHECK-NEXT: ret i32 [[R]]
146 %c1 = icmp ult i32 %a, 2
147 %c2 = icmp eq i32 %i, 5
148 %z = zext i1 %c2 to i32
149 %r = select i1 %c1, i32 1, i32 %z
153 ; This should not end with more instructions than it started from.
155 define i32 @PR49475(i32 %x, i16 %y) {
156 ; CHECK-LABEL: @PR49475(
157 ; CHECK-NEXT: [[M:%.*]] = and i16 [[Y:%.*]], 1
158 ; CHECK-NEXT: [[B1:%.*]] = icmp eq i32 [[X:%.*]], 0
159 ; CHECK-NEXT: [[B2:%.*]] = icmp eq i16 [[M]], 0
160 ; CHECK-NEXT: [[T1:%.*]] = or i1 [[B1]], [[B2]]
161 ; CHECK-NEXT: [[Z:%.*]] = zext i1 [[T1]] to i32
162 ; CHECK-NEXT: ret i32 [[Z]]
165 %b1 = icmp eq i32 %x, 0
166 %b2 = icmp eq i16 %m, 0
168 %z = zext i1 %t1 to i32
172 ; This would infinite-loop.
174 define i8 @PR49475_infloop(i32 %t0, i16 %insert, i64 %e, i8 %i162) {
175 ; CHECK-LABEL: @PR49475_infloop(
176 ; CHECK-NEXT: [[B:%.*]] = icmp eq i32 [[T0:%.*]], 0
177 ; CHECK-NEXT: [[B2:%.*]] = icmp eq i16 [[INSERT:%.*]], 0
178 ; CHECK-NEXT: [[T1:%.*]] = or i1 [[B]], [[B2]]
179 ; CHECK-NEXT: [[EXT:%.*]] = zext i1 [[T1]] to i32
180 ; CHECK-NEXT: [[AND:%.*]] = and i32 [[EXT]], [[T0]]
181 ; CHECK-NEXT: [[TMP1:%.*]] = or i32 [[AND]], 140
182 ; CHECK-NEXT: [[XOR1:%.*]] = zext i32 [[TMP1]] to i64
183 ; CHECK-NEXT: [[CONV16:%.*]] = sext i8 [[I162:%.*]] to i64
184 ; CHECK-NEXT: [[SUB17:%.*]] = sub i64 [[CONV16]], [[E:%.*]]
185 ; CHECK-NEXT: [[SEXT:%.*]] = shl i64 [[SUB17]], 32
186 ; CHECK-NEXT: [[CONV18:%.*]] = ashr exact i64 [[SEXT]], 32
187 ; CHECK-NEXT: [[CMP:%.*]] = icmp sle i64 [[CONV18]], [[XOR1]]
188 ; CHECK-NEXT: [[CONV19:%.*]] = zext i1 [[CMP]] to i16
189 ; CHECK-NEXT: [[OR21:%.*]] = or i16 [[CONV19]], [[INSERT]]
190 ; CHECK-NEXT: [[TRUNC44:%.*]] = trunc i16 [[OR21]] to i8
191 ; CHECK-NEXT: [[INC:%.*]] = or i8 [[TRUNC44]], [[I162]]
192 ; CHECK-NEXT: [[TOBOOL23_NOT:%.*]] = icmp eq i16 [[OR21]], 0
193 ; CHECK-NEXT: call void @llvm.assume(i1 [[TOBOOL23_NOT]])
194 ; CHECK-NEXT: ret i8 [[INC]]
196 %b = icmp eq i32 %t0, 0
197 %b2 = icmp eq i16 %insert, 0
199 %ext = zext i1 %t1 to i32
200 %and = and i32 %t0, %ext
201 %conv13 = zext i32 %and to i64
202 %xor = xor i64 %conv13, 140
203 %conv16 = sext i8 %i162 to i64
204 %sub17 = sub i64 %conv16, %e
205 %sext = shl i64 %sub17, 32
206 %conv18 = ashr exact i64 %sext, 32
207 %cmp = icmp sge i64 %xor, %conv18
208 %conv19 = zext i1 %cmp to i16
209 %or21 = or i16 %insert, %conv19
210 %trunc44 = trunc i16 %or21 to i8
211 %inc = add i8 %i162, %trunc44
212 %tobool23.not = icmp eq i16 %or21, 0
213 call void @llvm.assume(i1 %tobool23.not)
217 ; This would infinite loop because knownbits changed between checking
218 ; if a transform was profitable and actually doing the transform.
220 define i1 @PR51762(i32 *%i, i32 %t0, i16 %t1, i64* %p, i32* %d, i32* %f, i32 %p2, i1 %c1) {
221 ; CHECK-LABEL: @PR51762(
223 ; CHECK-NEXT: br label [[FOR_COND:%.*]]
225 ; CHECK-NEXT: [[I_SROA_8_0:%.*]] = phi i32 [ poison, [[ENTRY:%.*]] ], [ [[I_SROA_8_0_EXTRACT_TRUNC:%.*]], [[COND_TRUE:%.*]] ]
226 ; CHECK-NEXT: br i1 [[C1:%.*]], label [[COND_TRUE]], label [[FOR_END11:%.*]]
228 ; CHECK-NEXT: [[I_SROA_8_0_EXTRACT_TRUNC]] = ashr i32 [[T0:%.*]], 31
229 ; CHECK-NEXT: br label [[FOR_COND]]
231 ; CHECK-NEXT: [[S1:%.*]] = sext i16 [[T1:%.*]] to i64
232 ; CHECK-NEXT: [[SROA38:%.*]] = load i32, i32* [[I:%.*]], align 8
233 ; CHECK-NEXT: [[INSERT_EXT51:%.*]] = zext i32 [[I_SROA_8_0]] to i64
234 ; CHECK-NEXT: [[INSERT_SHIFT52:%.*]] = shl nuw i64 [[INSERT_EXT51]], 32
235 ; CHECK-NEXT: [[INSERT_EXT39:%.*]] = zext i32 [[SROA38]] to i64
236 ; CHECK-NEXT: [[INSERT_INSERT41:%.*]] = or i64 [[INSERT_SHIFT52]], [[INSERT_EXT39]]
237 ; CHECK-NEXT: [[REM:%.*]] = urem i64 [[S1]], [[INSERT_INSERT41]]
238 ; CHECK-NEXT: [[NE:%.*]] = icmp ne i64 [[REM]], 0
239 ; CHECK-NEXT: [[CMP:%.*]] = icmp eq i64 [[INSERT_INSERT41]], 0
240 ; CHECK-NEXT: [[SPEC_SELECT57:%.*]] = or i1 [[NE]], [[CMP]]
241 ; CHECK-NEXT: [[LOR_EXT:%.*]] = zext i1 [[SPEC_SELECT57]] to i32
242 ; CHECK-NEXT: [[T2:%.*]] = load i32, i32* [[D:%.*]], align 4
243 ; CHECK-NEXT: [[CONV15:%.*]] = sext i16 [[T1]] to i32
244 ; CHECK-NEXT: [[CMP16:%.*]] = icmp sge i32 [[T2]], [[CONV15]]
245 ; CHECK-NEXT: [[CONV17:%.*]] = zext i1 [[CMP16]] to i32
246 ; CHECK-NEXT: [[T3:%.*]] = load i32, i32* [[F:%.*]], align 4
247 ; CHECK-NEXT: [[ADD:%.*]] = add nsw i32 [[T3]], [[CONV17]]
248 ; CHECK-NEXT: store i32 [[ADD]], i32* [[F]], align 4
249 ; CHECK-NEXT: [[REM18:%.*]] = srem i32 [[LOR_EXT]], [[ADD]]
250 ; CHECK-NEXT: [[CONV19:%.*]] = zext i32 [[REM18]] to i64
251 ; CHECK-NEXT: store i32 0, i32* [[D]], align 8
252 ; CHECK-NEXT: [[R:%.*]] = icmp ult i64 [[INSERT_INSERT41]], [[CONV19]]
253 ; CHECK-NEXT: call void @llvm.assume(i1 [[R]])
254 ; CHECK-NEXT: ret i1 true
260 %i.sroa.8.0 = phi i32 [ poison, %entry ], [ %i.sroa.8.0.extract.trunc, %cond.true ]
261 br i1 %c1, label %cond.true, label %for.end11
264 %i.sroa.8.0.extract.trunc = ashr i32 %t0, 31
268 %s1 = sext i16 %t1 to i64
269 %sroa38 = load i32, i32* %i, align 8
270 %insert.ext51 = zext i32 %i.sroa.8.0 to i64
271 %insert.shift52 = shl nuw i64 %insert.ext51, 32
272 %insert.ext39 = zext i32 %sroa38 to i64
273 %insert.insert41 = or i64 %insert.shift52, %insert.ext39
274 %rem = urem i64 %s1, %insert.insert41
275 %ne = icmp ne i64 %rem, 0
276 %cmp = icmp eq i64 %insert.insert41, 0
277 %spec.select57 = or i1 %ne, %cmp
279 %lor.ext = zext i1 %spec.select57 to i32
280 %t2 = load i32, i32* %d, align 4
281 %conv15 = sext i16 %t1 to i32
282 %cmp16 = icmp sge i32 %t2, %conv15
283 %conv17 = zext i1 %cmp16 to i32
284 %t3 = load i32, i32* %f, align 4
285 %add = add nsw i32 %t3, %conv17
286 store i32 %add, i32* %f, align 4
287 %rem18 = srem i32 %lor.ext, %add
288 %conv19 = zext i32 %rem18 to i64
289 %div = udiv i64 %insert.insert41, %conv19
290 %trunc33 = trunc i64 %div to i32
291 store i32 %trunc33, i32* %d, align 8
292 %r = icmp ult i64 %insert.insert41, %conv19
293 call void @llvm.assume(i1 %r)
297 declare void @llvm.assume(i1 noundef)