1; NOTE: Assertions have been autogenerated by utils/update_test_checks.py
2; RUN: opt < %s -instcombine -S | FileCheck %s
3
4define i23 @test1(i23 %A) {
5; CHECK-LABEL: @test1(
6; CHECK-NEXT:    ret i23 0
7;
8  %B = sub i23 %A, %A
9  ret i23 %B
10}
11
12define i47 @test2(i47 %A) {
13; CHECK-LABEL: @test2(
14; CHECK-NEXT:    ret i47 %A
15;
16  %B = sub i47 %A, 0
17  ret i47 %B
18}
19
20define i97 @test3(i97 %A) {
21; CHECK-LABEL: @test3(
22; CHECK-NEXT:    ret i97 %A
23;
24  %B = sub i97 0, %A
25  %C = sub i97 0, %B
26  ret i97 %C
27}
28
29define i108 @test4(i108 %A, i108 %x) {
30; CHECK-LABEL: @test4(
31; CHECK-NEXT:    [[C:%.*]] = add i108 %x, %A
32; CHECK-NEXT:    ret i108 [[C]]
33;
34  %B = sub i108 0, %A
35  %C = sub i108 %x, %B
36  ret i108 %C
37}
38
39define i19 @test5(i19 %A, i19 %Bok, i19 %Cok) {
40; CHECK-LABEL: @test5(
41; CHECK-NEXT:    [[D1:%.*]] = sub i19 %Cok, %Bok
42; CHECK-NEXT:    [[E:%.*]] = add i19 [[D1]], %A
43; CHECK-NEXT:    ret i19 [[E]]
44;
45  %D = sub i19 %Bok, %Cok
46  %E = sub i19 %A, %D
47  ret i19 %E
48}
49
50define i57 @test6(i57 %A, i57 %B) {
51; CHECK-LABEL: @test6(
52; CHECK-NEXT:    [[B_NOT:%.*]] = xor i57 %B, -1
53; CHECK-NEXT:    [[D:%.*]] = and i57 [[B_NOT]], %A
54; CHECK-NEXT:    ret i57 [[D]]
55;
56  %C = and i57 %A, %B
57  %D = sub i57 %A, %C
58  ret i57 %D
59}
60
61define i77 @test7(i77 %A) {
62; CHECK-LABEL: @test7(
63; CHECK-NEXT:    [[B:%.*]] = xor i77 %A, -1
64; CHECK-NEXT:    ret i77 [[B]]
65;
66  %B = sub i77 -1, %A
67  ret i77 %B
68}
69
70define i27 @test8(i27 %A) {
71; CHECK-LABEL: @test8(
72; CHECK-NEXT:    [[C:%.*]] = shl i27 %A, 3
73; CHECK-NEXT:    ret i27 [[C]]
74;
75  %B = mul i27 9, %A
76  %C = sub i27 %B, %A
77  ret i27 %C
78}
79
80define i42 @test9(i42 %A) {
81; CHECK-LABEL: @test9(
82; CHECK-NEXT:    [[C:%.*]] = mul i42 %A, -2
83; CHECK-NEXT:    ret i42 [[C]]
84;
85  %B = mul i42 3, %A
86  %C = sub i42 %A, %B
87  ret i42 %C
88}
89
90define i1 @test11(i9 %A, i9 %B) {
91; CHECK-LABEL: @test11(
92; CHECK-NEXT:    [[CD:%.*]] = icmp ne i9 %A, %B
93; CHECK-NEXT:    ret i1 [[CD]]
94;
95  %C = sub i9 %A, %B
96  %cD = icmp ne i9 %C, 0
97  ret i1 %cD
98}
99
100define i43 @test12(i43 %A) {
101; CHECK-LABEL: @test12(
102; CHECK-NEXT:    [[C:%.*]] = lshr i43 %A, 42
103; CHECK-NEXT:    ret i43 [[C]]
104;
105  %B = ashr i43 %A, 42
106  %C = sub i43 0, %B
107  ret i43 %C
108}
109
110define i79 @test13(i79 %A) {
111; CHECK-LABEL: @test13(
112; CHECK-NEXT:    [[C:%.*]] = ashr i79 %A, 78
113; CHECK-NEXT:    ret i79 [[C]]
114;
115  %B = lshr i79 %A, 78
116  %C = sub i79 0, %B
117  ret i79 %C
118}
119
120define i1024 @test14(i1024 %A) {
121; CHECK-LABEL: @test14(
122; CHECK-NEXT:    [[D:%.*]] = ashr i1024 %A, 1023
123; CHECK-NEXT:    ret i1024 [[D]]
124;
125  %B = lshr i1024 %A, 1023
126  %C = bitcast i1024 %B to i1024
127  %D = sub i1024 0, %C
128  ret i1024 %D
129}
130
131define i51 @test16(i51 %A) {
132; CHECK-LABEL: @test16(
133; CHECK-NEXT:    [[Y:%.*]] = sdiv i51 %A, -1123
134; CHECK-NEXT:    ret i51 [[Y]]
135;
136  %X = sdiv i51 %A, 1123
137  %Y = sub i51 0, %X
138  ret i51 %Y
139}
140
141; Can't fold subtract here because negation might overflow.
142; PR3142
143define i25 @test17(i25 %Aok) {
144; CHECK-LABEL: @test17(
145; CHECK-NEXT:    [[B:%.*]] = sub i25 0, %Aok
146; CHECK-NEXT:    [[C:%.*]] = sdiv i25 [[B]], 1234
147; CHECK-NEXT:    ret i25 [[C]]
148;
149  %B = sub i25 0, %Aok
150  %C = sdiv i25 %B, 1234
151  ret i25 %C
152}
153
154define i128 @test18(i128 %Y) {
155; CHECK-LABEL: @test18(
156; CHECK-NEXT:    ret i128 0
157;
158  %t1 = shl i128 %Y, 2
159  %t2 = shl i128 %Y, 2
160  %t3 = sub i128 %t1, %t2
161  ret i128 %t3
162}
163
164define i39 @test19(i39 %X, i39 %Y) {
165; CHECK-LABEL: @test19(
166; CHECK-NEXT:    ret i39 %X
167;
168  %Z = sub i39 %X, %Y
169  %Q = add i39 %Z, %Y
170  ret i39 %Q
171}
172
173define i1 @test20(i33 %g, i33 %h) {
174; CHECK-LABEL: @test20(
175; CHECK-NEXT:    [[T4:%.*]] = icmp ne i33 %h, 0
176; CHECK-NEXT:    ret i1 [[T4]]
177;
178  %t2 = sub i33 %g, %h
179  %t4 = icmp ne i33 %t2, %g
180  ret i1 %t4
181}
182
183define i1 @test21(i256 %g, i256 %h) {
184; CHECK-LABEL: @test21(
185; CHECK-NEXT:    [[T4:%.*]] = icmp ne i256 %h, 0
186; CHECK-NEXT:    ret i1 [[T4]]
187;
188  %t2 = sub i256 %g, %h
189  %t4 = icmp ne i256 %t2, %g
190  ret i1 %t4
191}
192