1 ; NOTE: Assertions have been autogenerated by utils/update_test_checks.py
2 ; RUN: opt -S -passes=instsimplify < %s | FileCheck %s
4 define i1 @or_implied(i8 %x, i1 %c) {
5 ; CHECK-LABEL: @or_implied(
6 ; CHECK-NEXT: [[CMP2:%.*]] = icmp ne i8 [[X:%.*]], 1
7 ; CHECK-NEXT: ret i1 [[CMP2]]
9 %cmp = icmp eq i8 %x, 0
10 %cmp2 = icmp ne i8 %x, 1
11 %and = and i1 %cmp, %c
12 %or = or i1 %and, %cmp2
16 define i1 @or_implied_comm1(i8 %x, i1 %c) {
17 ; CHECK-LABEL: @or_implied_comm1(
18 ; CHECK-NEXT: [[CMP2:%.*]] = icmp ne i8 [[X:%.*]], 1
19 ; CHECK-NEXT: ret i1 [[CMP2]]
21 %cmp = icmp eq i8 %x, 0
22 %cmp2 = icmp ne i8 %x, 1
23 %and = and i1 %cmp, %c
24 %or = or i1 %cmp2, %and
28 define i1 @or_implied_comm2(i8 %x, i1 %c) {
29 ; CHECK-LABEL: @or_implied_comm2(
30 ; CHECK-NEXT: [[CMP2:%.*]] = icmp ne i8 [[X:%.*]], 1
31 ; CHECK-NEXT: ret i1 [[CMP2]]
33 %cmp = icmp eq i8 %x, 0
34 %cmp2 = icmp ne i8 %x, 1
35 %and = and i1 %c, %cmp
36 %or = or i1 %and, %cmp2
40 define i1 @or_implied_comm3(i8 %x, i1 %c) {
41 ; CHECK-LABEL: @or_implied_comm3(
42 ; CHECK-NEXT: [[CMP2:%.*]] = icmp ne i8 [[X:%.*]], 1
43 ; CHECK-NEXT: ret i1 [[CMP2]]
45 %cmp = icmp eq i8 %x, 0
46 %cmp2 = icmp ne i8 %x, 1
47 %and = and i1 %c, %cmp
48 %or = or i1 %cmp2, %and
52 define i1 @or_not_implied(i8 %x, i1 %c) {
53 ; CHECK-LABEL: @or_not_implied(
54 ; CHECK-NEXT: [[CMP:%.*]] = icmp eq i8 [[X:%.*]], 0
55 ; CHECK-NEXT: [[CMP2:%.*]] = icmp ne i8 [[X]], 0
56 ; CHECK-NEXT: [[AND:%.*]] = and i1 [[CMP]], [[C:%.*]]
57 ; CHECK-NEXT: [[OR:%.*]] = or i1 [[AND]], [[CMP2]]
58 ; CHECK-NEXT: ret i1 [[OR]]
60 %cmp = icmp eq i8 %x, 0
61 %cmp2 = icmp ne i8 %x, 0
62 %and = and i1 %cmp, %c
63 %or = or i1 %and, %cmp2
67 define i1 @and_implied(i8 %x, i1 %c) {
68 ; CHECK-LABEL: @and_implied(
69 ; CHECK-NEXT: [[CMP2:%.*]] = icmp eq i8 [[X:%.*]], 1
70 ; CHECK-NEXT: ret i1 [[CMP2]]
72 %cmp = icmp ne i8 %x, 0
73 %cmp2 = icmp eq i8 %x, 1
75 %and = and i1 %or, %cmp2
79 define i1 @and_implied_comm1(i8 %x, i1 %c) {
80 ; CHECK-LABEL: @and_implied_comm1(
81 ; CHECK-NEXT: [[CMP2:%.*]] = icmp eq i8 [[X:%.*]], 1
82 ; CHECK-NEXT: ret i1 [[CMP2]]
84 %cmp = icmp ne i8 %x, 0
85 %cmp2 = icmp eq i8 %x, 1
87 %and = and i1 %cmp2, %or
91 define i1 @and_implied_comm2(i8 %x, i1 %c) {
92 ; CHECK-LABEL: @and_implied_comm2(
93 ; CHECK-NEXT: [[CMP2:%.*]] = icmp eq i8 [[X:%.*]], 1
94 ; CHECK-NEXT: ret i1 [[CMP2]]
96 %cmp = icmp ne i8 %x, 0
97 %cmp2 = icmp eq i8 %x, 1
99 %and = and i1 %or, %cmp2
103 define i1 @and_implied_comm3(i8 %x, i1 %c) {
104 ; CHECK-LABEL: @and_implied_comm3(
105 ; CHECK-NEXT: [[CMP2:%.*]] = icmp eq i8 [[X:%.*]], 1
106 ; CHECK-NEXT: ret i1 [[CMP2]]
108 %cmp = icmp ne i8 %x, 0
109 %cmp2 = icmp eq i8 %x, 1
111 %and = and i1 %cmp2, %or
115 define i1 @and_not_implied(i8 %x, i1 %c) {
116 ; CHECK-LABEL: @and_not_implied(
117 ; CHECK-NEXT: [[CMP:%.*]] = icmp ne i8 [[X:%.*]], 0
118 ; CHECK-NEXT: [[CMP2:%.*]] = icmp eq i8 [[X]], 0
119 ; CHECK-NEXT: [[OR:%.*]] = or i1 [[CMP]], [[C:%.*]]
120 ; CHECK-NEXT: [[AND:%.*]] = and i1 [[OR]], [[CMP2]]
121 ; CHECK-NEXT: ret i1 [[AND]]
123 %cmp = icmp ne i8 %x, 0
124 %cmp2 = icmp eq i8 %x, 0
126 %and = and i1 %or, %cmp2
130 define i1 @uaddo_and(i64 %a, i64 %b){
131 ; CHECK-LABEL: @uaddo_and(
132 ; CHECK-NEXT: [[S:%.*]] = add i64 [[A:%.*]], [[B:%.*]]
133 ; CHECK-NEXT: [[COND_A:%.*]] = icmp uge i64 [[S]], [[A]]
134 ; CHECK-NEXT: ret i1 [[COND_A]]
137 %cond_a = icmp uge i64 %s, %a
138 %cond_b = icmp uge i64 %s, %b
139 %cond = and i1 %cond_a, %cond_b
143 define i1 @uaddo_and_commuted1(i64 %a, i64 %b){
144 ; CHECK-LABEL: @uaddo_and_commuted1(
145 ; CHECK-NEXT: [[S:%.*]] = add i64 [[A:%.*]], [[B:%.*]]
146 ; CHECK-NEXT: [[COND_A:%.*]] = icmp ule i64 [[A]], [[S]]
147 ; CHECK-NEXT: ret i1 [[COND_A]]
150 %cond_a = icmp ule i64 %a, %s
151 %cond_b = icmp uge i64 %s, %b
152 %cond = and i1 %cond_a, %cond_b
156 define i1 @uaddo_and_commuted2(i64 %a, i64 %b){
157 ; CHECK-LABEL: @uaddo_and_commuted2(
158 ; CHECK-NEXT: [[S:%.*]] = add i64 [[A:%.*]], [[B:%.*]]
159 ; CHECK-NEXT: [[COND_A:%.*]] = icmp uge i64 [[S]], [[A]]
160 ; CHECK-NEXT: ret i1 [[COND_A]]
163 %cond_a = icmp uge i64 %s, %a
164 %cond_b = icmp ule i64 %b, %s
165 %cond = and i1 %cond_a, %cond_b
169 define i1 @uaddo_and_commuted3(i64 %a, i64 %b){
170 ; CHECK-LABEL: @uaddo_and_commuted3(
171 ; CHECK-NEXT: [[S:%.*]] = add i64 [[A:%.*]], [[B:%.*]]
172 ; CHECK-NEXT: [[COND_A:%.*]] = icmp ule i64 [[A]], [[S]]
173 ; CHECK-NEXT: ret i1 [[COND_A]]
176 %cond_a = icmp ule i64 %a, %s
177 %cond_b = icmp ule i64 %b, %s
178 %cond = and i1 %cond_a, %cond_b
182 define i1 @uaddo_or(i64 %a, i64 %b){
183 ; CHECK-LABEL: @uaddo_or(
184 ; CHECK-NEXT: [[S:%.*]] = add i64 [[A:%.*]], [[B:%.*]]
185 ; CHECK-NEXT: [[COND_A:%.*]] = icmp ult i64 [[S]], [[A]]
186 ; CHECK-NEXT: ret i1 [[COND_A]]
189 %cond_a = icmp ult i64 %s, %a
190 %cond_b = icmp ult i64 %s, %b
191 %cond = or i1 %cond_a, %cond_b
195 define i1 @uaddo_or_commuted1(i64 %a, i64 %b){
196 ; CHECK-LABEL: @uaddo_or_commuted1(
197 ; CHECK-NEXT: [[S:%.*]] = add i64 [[A:%.*]], [[B:%.*]]
198 ; CHECK-NEXT: [[COND_A:%.*]] = icmp ugt i64 [[A]], [[S]]
199 ; CHECK-NEXT: ret i1 [[COND_A]]
202 %cond_a = icmp ugt i64 %a, %s
203 %cond_b = icmp ult i64 %s, %b
204 %cond = or i1 %cond_a, %cond_b
208 define i1 @uaddo_or_commuted2(i64 %a, i64 %b){
209 ; CHECK-LABEL: @uaddo_or_commuted2(
210 ; CHECK-NEXT: [[S:%.*]] = add i64 [[A:%.*]], [[B:%.*]]
211 ; CHECK-NEXT: [[COND_A:%.*]] = icmp ult i64 [[S]], [[A]]
212 ; CHECK-NEXT: ret i1 [[COND_A]]
215 %cond_a = icmp ult i64 %s, %a
216 %cond_b = icmp ugt i64 %b, %s
217 %cond = or i1 %cond_a, %cond_b
221 define i1 @uaddo_or_commuted3(i64 %a, i64 %b){
222 ; CHECK-LABEL: @uaddo_or_commuted3(
223 ; CHECK-NEXT: [[S:%.*]] = add i64 [[A:%.*]], [[B:%.*]]
224 ; CHECK-NEXT: [[COND_A:%.*]] = icmp ugt i64 [[A]], [[S]]
225 ; CHECK-NEXT: ret i1 [[COND_A]]
228 %cond_a = icmp ugt i64 %a, %s
229 %cond_b = icmp ugt i64 %b, %s
230 %cond = or i1 %cond_a, %cond_b
234 define i1 @pr69050(i32 %arg, i32 %arg1) {
235 ; CHECK-LABEL: @pr69050(
236 ; CHECK-NEXT: [[XOR:%.*]] = xor i32 [[ARG:%.*]], -1
237 ; CHECK-NEXT: [[AND:%.*]] = and i32 [[XOR]], [[ARG1:%.*]]
238 ; CHECK-NEXT: [[ICMP:%.*]] = icmp ne i32 [[AND]], 0
239 ; CHECK-NEXT: ret i1 [[ICMP]]
241 %xor = xor i32 %arg, -1
242 %and = and i32 %xor, %arg1
243 %icmp = icmp ne i32 %and, 0
244 %icmp2 = icmp ne i32 %arg, -1
245 %and3 = and i1 %icmp2, %icmp
249 define i1 @pr69091(i32 %arg, i32 %arg1) {
250 ; CHECK-LABEL: @pr69091(
251 ; CHECK-NEXT: [[ICMP:%.*]] = icmp ne i32 [[ARG:%.*]], -1
252 ; CHECK-NEXT: ret i1 [[ICMP]]
254 %icmp = icmp ne i32 %arg, -1
255 %add = add i32 %arg, 1
256 %mul = mul i32 %add, %arg1
257 %icmp2 = icmp ne i32 %mul, 0
258 %or = or i1 %icmp, %icmp2
262 declare void @barrier()
264 define i1 @or_icmp_implies_ub(i32 %x) {
265 ; CHECK-LABEL: @or_icmp_implies_ub(
266 ; CHECK-NEXT: [[CMP1:%.*]] = icmp ne i32 [[X:%.*]], 0
267 ; CHECK-NEXT: call void @barrier()
268 ; CHECK-NEXT: ret i1 [[CMP1]]
270 %cmp1 = icmp ne i32 %x, 0
272 %div = udiv i32 2147483647, %x
273 %cmp2 = icmp ugt i32 %x, %div
274 %or = or i1 %cmp1, %cmp2
278 define i1 @and_icmp_implies_ub(i32 %x) {
279 ; CHECK-LABEL: @and_icmp_implies_ub(
280 ; CHECK-NEXT: call void @barrier()
281 ; CHECK-NEXT: ret i1 false
283 %cmp1 = icmp eq i32 %x, 0
285 %div = udiv i32 2147483647, %x
286 %cmp2 = icmp ugt i32 %x, %div
287 %and = and i1 %cmp1, %cmp2
291 define i1 @or_icmp_implies_poison(i32 %x) {
292 ; CHECK-LABEL: @or_icmp_implies_poison(
293 ; CHECK-NEXT: [[CMP1:%.*]] = icmp ne i32 [[X:%.*]], 32
294 ; CHECK-NEXT: [[SHL:%.*]] = shl i32 1, [[X]]
295 ; CHECK-NEXT: [[CMP2:%.*]] = icmp ugt i32 [[X]], [[SHL]]
296 ; CHECK-NEXT: [[OR:%.*]] = or i1 [[CMP1]], [[CMP2]]
297 ; CHECK-NEXT: ret i1 [[OR]]
299 %cmp1 = icmp ne i32 %x, 32
301 %cmp2 = icmp ugt i32 %x, %shl
302 %or = or i1 %cmp1, %cmp2
306 define i1 @and_icmp_implies_poison(i32 %x) {
307 ; CHECK-LABEL: @and_icmp_implies_poison(
308 ; CHECK-NEXT: [[CMP1:%.*]] = icmp eq i32 [[X:%.*]], 32
309 ; CHECK-NEXT: [[SHL:%.*]] = shl i32 1, [[X]]
310 ; CHECK-NEXT: [[CMP2:%.*]] = icmp ugt i32 [[X]], [[SHL]]
311 ; CHECK-NEXT: [[AND:%.*]] = and i1 [[CMP1]], [[CMP2]]
312 ; CHECK-NEXT: ret i1 [[AND]]
314 %cmp1 = icmp eq i32 %x, 32
316 %cmp2 = icmp ugt i32 %x, %shl
317 %and = and i1 %cmp1, %cmp2
321 define i1 @and_is_constant(ptr %arg, ptr %arg2) {
322 ; CHECK-LABEL: @and_is_constant(
323 ; CHECK-NEXT: [[ICMP:%.*]] = icmp eq ptr [[ARG:%.*]], [[ARG2:%.*]]
324 ; CHECK-NEXT: [[CALL:%.*]] = call i1 @llvm.is.constant.i1(i1 [[ICMP]])
325 ; CHECK-NEXT: [[AND:%.*]] = and i1 [[CALL]], [[ICMP]]
326 ; CHECK-NEXT: ret i1 [[AND]]
328 %icmp = icmp eq ptr %arg, %arg2
329 %call = call i1 @llvm.is.constant.i1(i1 %icmp)
330 %and = and i1 %call, %icmp
334 define i1 @pr98753(i32 noundef %x, i32 %y) {
335 ; CHECK-LABEL: @pr98753(
336 ; CHECK-NEXT: [[CMP1:%.*]] = icmp ne i32 [[X:%.*]], 0
337 ; CHECK-NEXT: [[SEL:%.*]] = select i1 [[CMP1]], i32 [[Y:%.*]], i32 undef
338 ; CHECK-NEXT: [[CMP2:%.*]] = icmp sgt i32 [[SEL]], 0
339 ; CHECK-NEXT: [[AND:%.*]] = and i1 [[CMP1]], [[CMP2]]
340 ; CHECK-NEXT: ret i1 [[AND]]
342 %cmp1 = icmp ne i32 %x, 0
343 %sel = select i1 %cmp1, i32 %y, i32 undef
344 %cmp2 = icmp sgt i32 %sel, 0
345 %and = and i1 %cmp1, %cmp2
349 declare i1 @llvm.is.constant.i1(i1)