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 @sub_to_xor(i32 %x, i32 %y) {
7; CHECK-LABEL: @sub_to_xor(
8; CHECK-NEXT:    [[SUB:%.*]] = xor i32 [[X:%.*]], [[Y:%.*]]
9; CHECK-NEXT:    ret i32 [[SUB]]
10;
11  %or = or i32 %x, %y
12  %and = and i32 %x, %y
13  %sub = sub i32 %or, %and
14  ret i32 %sub
15}
16
17define i32 @sub_to_xor_extra_use_sub(i32 %x, i32 %y) {
18; CHECK-LABEL: @sub_to_xor_extra_use_sub(
19; CHECK-NEXT:    [[SUB:%.*]] = xor i32 [[X:%.*]], [[Y:%.*]]
20; CHECK-NEXT:    call void @use(i32 [[SUB]])
21; CHECK-NEXT:    ret i32 [[SUB]]
22;
23  %or = or i32 %x, %y
24  %and = and i32 %x, %y
25  %sub = sub i32 %or, %and
26  call void @use(i32 %sub)
27  ret i32 %sub
28}
29
30define i32 @sub_to_xor_extra_use_and(i32 %x, i32 %y) {
31; CHECK-LABEL: @sub_to_xor_extra_use_and(
32; CHECK-NEXT:    [[AND:%.*]] = and i32 [[X:%.*]], [[Y:%.*]]
33; CHECK-NEXT:    call void @use(i32 [[AND]])
34; CHECK-NEXT:    [[SUB:%.*]] = xor i32 [[X]], [[Y]]
35; CHECK-NEXT:    ret i32 [[SUB]]
36;
37  %or = or i32 %x, %y
38  %and = and i32 %x, %y
39  call void @use(i32 %and)
40  %sub = sub i32 %or, %and
41  ret i32 %sub
42}
43
44define i32 @sub_to_xor_extra_use_or(i32 %x, i32 %y) {
45; CHECK-LABEL: @sub_to_xor_extra_use_or(
46; CHECK-NEXT:    [[OR:%.*]] = or i32 [[X:%.*]], [[Y:%.*]]
47; CHECK-NEXT:    call void @use(i32 [[OR]])
48; CHECK-NEXT:    [[SUB:%.*]] = xor i32 [[X]], [[Y]]
49; CHECK-NEXT:    ret i32 [[SUB]]
50;
51  %or = or i32 %x, %y
52  call void @use(i32 %or)
53  %and = and i32 %x, %y
54  %sub = sub i32 %or, %and
55  ret i32 %sub
56}
57
58define i32 @sub_to_xor_or_commuted(i32 %x, i32 %y) {
59; CHECK-LABEL: @sub_to_xor_or_commuted(
60; CHECK-NEXT:    [[SUB:%.*]] = xor i32 [[X:%.*]], [[Y:%.*]]
61; CHECK-NEXT:    ret i32 [[SUB]]
62;
63  %or = or i32 %y, %x
64  %and = and i32 %x, %y
65  %sub = sub i32 %or, %and
66  ret i32 %sub
67}
68
69define i32 @sub_to_xor_and_commuted(i32 %x, i32 %y) {
70; CHECK-LABEL: @sub_to_xor_and_commuted(
71; CHECK-NEXT:    [[SUB:%.*]] = xor i32 [[Y:%.*]], [[X:%.*]]
72; CHECK-NEXT:    ret i32 [[SUB]]
73;
74  %or = or i32 %x, %y
75  %and = and i32 %y, %x
76  %sub = sub i32 %or, %and
77  ret i32 %sub
78}
79
80define <2 x i32> @sub_to_xor_vec(<2 x i32> %x, <2 x i32> %y) {
81; CHECK-LABEL: @sub_to_xor_vec(
82; CHECK-NEXT:    [[SUB:%.*]] = xor <2 x i32> [[Y:%.*]], [[X:%.*]]
83; CHECK-NEXT:    ret <2 x i32> [[SUB]]
84;
85  %or = or <2 x i32> %x, %y
86  %and = and <2 x i32> %y, %x
87  %sub = sub <2 x i32> %or, %and
88  ret <2 x i32> %sub
89}
90
91; Negative tests
92
93define i32 @sub_to_xor_wrong_arg(i32 %x, i32 %y, i32 %z) {
94; CHECK-LABEL: @sub_to_xor_wrong_arg(
95; CHECK-NEXT:    [[OR:%.*]] = or i32 [[X:%.*]], [[Y:%.*]]
96; CHECK-NEXT:    [[AND:%.*]] = and i32 [[X]], [[Z:%.*]]
97; CHECK-NEXT:    [[SUB:%.*]] = sub i32 [[AND]], [[OR]]
98; CHECK-NEXT:    ret i32 [[SUB]]
99;
100  %or = or i32 %x, %y
101  %and = and i32 %x, %z
102  %sub = sub i32 %and, %or
103  ret i32 %sub
104}
105