1; NOTE: Assertions have been autogenerated by utils/update_test_checks.py
2; RUN: opt -instcombine -S < %s | FileCheck %s
3
4declare void @use(i32)
5
6define i32 @test1(i32 %x) {
7; CHECK-LABEL: @test1(
8; CHECK-NEXT:    [[AND:%.*]] = and i32 [[X:%.*]], 31
9; CHECK-NEXT:    [[SUB:%.*]] = xor i32 [[AND]], 63
10; CHECK-NEXT:    ret i32 [[SUB]]
11;
12  %and = and i32 %x, 31
13  %sub = sub i32 63, %and
14  ret i32 %sub
15}
16
17define <2 x i32> @test1vec(<2 x i32> %x) {
18; CHECK-LABEL: @test1vec(
19; CHECK-NEXT:    [[AND:%.*]] = and <2 x i32> [[X:%.*]], <i32 31, i32 31>
20; CHECK-NEXT:    [[SUB:%.*]] = xor <2 x i32> [[AND]], <i32 63, i32 63>
21; CHECK-NEXT:    ret <2 x i32> [[SUB]]
22;
23  %and = and <2 x i32> %x, <i32 31, i32 31>
24  %sub = sub <2 x i32> <i32 63, i32 63>, %and
25  ret <2 x i32> %sub
26}
27
28declare i32 @llvm.ctlz.i32(i32, i1) nounwind readnone
29
30define i32 @test2(i32 %x) nounwind {
31; CHECK-LABEL: @test2(
32; CHECK-NEXT:    [[COUNT:%.*]] = tail call i32 @llvm.ctlz.i32(i32 [[X:%.*]], i1 true) [[ATTR2:#.*]], [[RNG0:!range !.*]]
33; CHECK-NEXT:    [[SUB:%.*]] = xor i32 [[COUNT]], 31
34; CHECK-NEXT:    ret i32 [[SUB]]
35;
36  %count = tail call i32 @llvm.ctlz.i32(i32 %x, i1 true) nounwind readnone
37  %sub = sub i32 31, %count
38  ret i32 %sub
39}
40
41define i32 @xor_add(i32 %x) {
42; CHECK-LABEL: @xor_add(
43; CHECK-NEXT:    [[AND:%.*]] = and i32 [[X:%.*]], 31
44; CHECK-NEXT:    [[ADD:%.*]] = sub nuw nsw i32 73, [[AND]]
45; CHECK-NEXT:    ret i32 [[ADD]]
46;
47  %and = and i32 %x, 31
48  %xor = xor i32 %and, 31
49  %add = add i32 %xor, 42
50  ret i32 %add
51}
52
53define i32 @xor_add_extra_use(i32 %x) {
54; CHECK-LABEL: @xor_add_extra_use(
55; CHECK-NEXT:    [[AND:%.*]] = and i32 [[X:%.*]], 31
56; CHECK-NEXT:    [[XOR:%.*]] = xor i32 [[AND]], 31
57; CHECK-NEXT:    call void @use(i32 [[XOR]])
58; CHECK-NEXT:    [[ADD:%.*]] = sub nuw nsw i32 73, [[AND]]
59; CHECK-NEXT:    ret i32 [[ADD]]
60;
61  %and = and i32 %x, 31
62  %xor = xor i32 %and, 31
63  call void @use(i32 %xor)
64  %add = add i32 %xor, 42
65  ret i32 %add
66}
67
68define <2 x i8> @xor_add_splat(<2 x i8> %x) {
69; CHECK-LABEL: @xor_add_splat(
70; CHECK-NEXT:    [[AND:%.*]] = and <2 x i8> [[X:%.*]], <i8 24, i8 24>
71; CHECK-NEXT:    [[ADD:%.*]] = sub nuw nsw <2 x i8> <i8 105, i8 105>, [[AND]]
72; CHECK-NEXT:    ret <2 x i8> [[ADD]]
73;
74  %and = and <2 x i8> %x, <i8 24, i8 24>
75  %xor = xor <2 x i8> %and, <i8 63, i8 63>
76  %add = add <2 x i8> %xor, <i8 42, i8 42>
77  ret <2 x i8> %add
78}
79
80define <2 x i8> @xor_add_splat_undef(<2 x i8> %x) {
81; CHECK-LABEL: @xor_add_splat_undef(
82; CHECK-NEXT:    [[AND:%.*]] = and <2 x i8> [[X:%.*]], <i8 24, i8 24>
83; CHECK-NEXT:    [[XOR:%.*]] = xor <2 x i8> [[AND]], <i8 63, i8 undef>
84; CHECK-NEXT:    [[ADD:%.*]] = add <2 x i8> [[XOR]], <i8 42, i8 42>
85; CHECK-NEXT:    ret <2 x i8> [[ADD]]
86;
87  %and = and <2 x i8> %x, <i8 24, i8 24>
88  %xor = xor <2 x i8> %and, <i8 63, i8 undef>
89  %add = add <2 x i8> %xor, <i8 42, i8 42>
90  ret <2 x i8> %add
91}
92