blob: df99e96f5fe1606661e66525e0c7da22adff0508 [file] [log] [blame]
Chris Lattner7bfa8b62002-04-18 17:35:39 +00001; This test makes sure that add instructions are properly eliminated.
Chris Lattner7bfa8b62002-04-18 17:35:39 +00002
Reid Spencer2341c222007-02-02 02:16:23 +00003; RUN: llvm-upgrade < %s | llvm-as | opt -instcombine | llvm-dis | \
4; RUN: grep -v OK | not grep add
Reid Spencerede8c3b2007-04-15 07:38:21 +00005; END.
Chris Lattner7bfa8b62002-04-18 17:35:39 +00006
7implementation
8
Chris Lattnerde293ee2003-02-18 19:43:53 +00009int %test1(int %A) {
Chris Lattner7bfa8b62002-04-18 17:35:39 +000010 %B = add int %A, 0
11 ret int %B
Chris Lattnerde293ee2003-02-18 19:43:53 +000012}
Chris Lattner7bfa8b62002-04-18 17:35:39 +000013
Chris Lattnerde293ee2003-02-18 19:43:53 +000014int %test2(int %A) {
Chris Lattner7bfa8b62002-04-18 17:35:39 +000015 %B = add int %A, 5
16 %C = add int %B, -5
17 ret int %C
Chris Lattnerde293ee2003-02-18 19:43:53 +000018}
Chris Lattner7bfa8b62002-04-18 17:35:39 +000019
Chris Lattnerde293ee2003-02-18 19:43:53 +000020int %test3(int %A) {
Chris Lattner7bfa8b62002-04-18 17:35:39 +000021 %B = add int %A, 5
22 %C = sub int %B, 5 ;; This should get converted to an add
23 ret int %C
Chris Lattnerde293ee2003-02-18 19:43:53 +000024}
Chris Lattner7bfa8b62002-04-18 17:35:39 +000025
Chris Lattner2ae37b02003-02-18 19:43:21 +000026int %test4(int %A, int %B) {
Chris Lattnere0c3fbb2002-05-06 16:44:53 +000027 %C = sub int 0, %A
28 %D = add int %B, %C ; D = B + -A = B - A
29 ret int %D
30}
31
Chris Lattner2ae37b02003-02-18 19:43:21 +000032int %test5(int %A, int %B) {
Chris Lattnere0c3fbb2002-05-06 16:44:53 +000033 %C = sub int 0, %A
34 %D = add int %C, %B ; D = -A + B = B - A
35 ret int %D
36}
37
Chris Lattner39593402003-02-18 19:55:31 +000038int %test6(int %A) {
39 %B = mul int 7, %A
40 %C = add int %B, %A ; C = 7*A+A == 8*A == A << 3
41 ret int %C
42}
43
44int %test7(int %A) {
45 %B = mul int 7, %A
46 %C = add int %A, %B ; C = A+7*A == 8*A == A << 3
47 ret int %C
48}
49
Reid Spencer2341c222007-02-02 02:16:23 +000050; (A & C1)+(B & C2) -> (A & C1)|(B & C2) iff C1&C2 == 0
51int %test8(int %A, int %B) {
Chris Lattnerdec13672003-03-10 23:52:54 +000052 %A1 = and int %A, 7
53 %B1 = and int %B, 128
54 %C = add int %A1, %B1
55 ret int %C
56}
Chris Lattnercb269142003-03-11 00:10:59 +000057
Chris Lattnerdaff6442003-07-24 19:44:51 +000058int %test9(int %A) {
59 %B = shl int %A, ubyte 4
60 %C = add int %B, %B ; === shl int %A, 5
61 ret int %C
62}
Chris Lattner9b741f12003-08-13 05:27:57 +000063
64bool %test10(ubyte %A, ubyte %b) {
65 %B = add ubyte %A, %b
66 %c = setne ubyte %B, 0 ; === A != -b
67 ret bool %c
68}
69
70bool %test11(ubyte %A) {
71 %B = add ubyte %A, 255
72 %c = setne ubyte %B, 0 ; === A != 1
73 ret bool %c
74}
Chris Lattner895746a2003-08-13 19:01:09 +000075
76int %test12(int %A, int %B) {
77 %C_OK = add int %B, %A ; Should be transformed into shl A, 1
78 br label %X
79X:
80 %D = add int %C_OK, %A
81 ret int %D
82}
83
84int %test13(int %A, int %B, int %C) {
85 %D_OK = add int %A, %B
86 %E_OK = add int %D_OK, %C
87 %F = add int %E_OK, %A ;; shl A, 1
88 ret int %F
89}
90
91uint %test14(uint %offset, uint %difference) {
92 %tmp.2 = and uint %difference, 3
93 %tmp.3_OK = add uint %tmp.2, %offset
94 %tmp.5.mask = and uint %difference, 4294967292
95 %tmp.8 = add uint %tmp.3_OK, %tmp.5.mask ; == add %offset, %difference
96 ret uint %tmp.8
97}
98
Chris Lattnerdef041c2003-09-19 15:33:46 +000099ubyte %test15(ubyte %A) {
100 %B = add ubyte %A, 192 ; Does not effect result
101 %C = and ubyte %B, 16 ; Only one bit set
102 ret ubyte %C
103}
104
105ubyte %test16(ubyte %A) {
106 %B = add ubyte %A, 16 ; Turn this into a XOR
107 %C = and ubyte %B, 16 ; Only one bit set
108 ret ubyte %C
109}
110
Chris Lattnerd15f2732003-10-02 15:11:09 +0000111int %test17(int %A) {
112 %B = xor int %A, -1
113 %C = add int %B, 1 ; == sub int 0, %A
114 ret int %C
115}
116
117ubyte %test18(ubyte %A) {
118 %B = xor ubyte %A, 255
119 %C = add ubyte %B, 17 ; == sub ubyte 16, %A
120 ret ubyte %C
121}
Chris Lattnere4ad3e52004-04-09 23:47:52 +0000122
123int %test19(bool %C) {
124 %A = select bool %C, int 1000, int 10
125 %V = add int %A, 123
126 ret int %V
127}
128
Chris Lattner825a0012004-04-10 22:01:27 +0000129int %test20(int %x) {
130 %tmp.2 = xor int %x, -2147483648
131 ;; Add of sign bit -> xor of sign bit.
132 %tmp.4 = add int %tmp.2, -2147483648
133 ret int %tmp.4
134}
Chris Lattnere4ad3e52004-04-09 23:47:52 +0000135
Chris Lattner0da061f2004-06-27 22:51:19 +0000136bool %test21(uint %x) {
137 %t = add uint %x, 4
138 %y = seteq uint %t, 123
139 ret bool %y
140}
Chris Lattner3a03c902004-07-03 00:25:31 +0000141
142int %test22(uint %V) {
143 %V2 = add uint %V, 10
144 switch uint %V2, label %Default [
145 uint 20, label %Lab1
146 uint 30, label %Lab2
147 ]
148Default:
149 ret int 123
150Lab1:
151 ret int 12312
152Lab2:
153 ret int 1231231
154}
Chris Lattner5e0ab5c2004-09-29 05:06:12 +0000155
156int %test23(bool %C, int %a) {
157entry:
158 br bool %C, label %endif, label %else
159
160else:
161 br label %endif
162
163endif:
164 %b.0 = phi int [ 0, %entry ], [ 1, %else ]
165 %tmp.4 = add int %b.0, 1
166 ret int %tmp.4
167}
168
Chris Lattner7bfe4032004-10-08 03:41:59 +0000169int %test24(int %A) {
170 %B = add int %A, 1
171 %C = shl int %B, ubyte 1
172 %D = sub int %C, 2
173 ret int %D ;; A << 1
174}
Chris Lattnerf6392b42004-11-13 19:49:39 +0000175
176long %test25(long %Y) {
177 %tmp.4 = shl long %Y, ubyte 2
178 %tmp.12 = shl long %Y, ubyte 2
179 %tmp.8 = add long %tmp.4, %tmp.12 ;; Y << 3
180 ret long %tmp.8
181}
182
Chris Lattner2547f052005-01-19 21:48:31 +0000183int %test26(int %A, int %B) {
184 %C = add int %A, %B
185 %D = sub int %C, %B
186 ret int %D
187}
188
189int %test27(bool %C, int %X, int %Y) {
190 %A = add int %X, %Y
191 %B = add int %Y, 123
192 %C = select bool %C, int %A, int %B ;; Fold add through select.
193 %D = sub int %C, %Y
194 ret int %D
195}
196
Chris Lattnerdd831832005-04-07 16:24:59 +0000197int %test28(int %X) {
198 %Y = add int %X, 1234
199 %Z = sub int 42, %Y
200 ret int %Z
201}
Chris Lattner9136c832005-09-18 04:22:59 +0000202
203uint %test29(uint %X, uint %x) {
204 %tmp.2 = sub uint %X, %x
205 %tmp.2.mask = and uint %tmp.2, 63 ; <uint> [#uses=1]
206 %tmp.6 = add uint %tmp.2.mask, %x ; <uint> [#uses=1]
207 %tmp.7 = and uint %tmp.6, 63 ; <uint> [#uses=1]
208 %tmp.9 = and uint %tmp.2, 4294967232 ; <uint> [#uses=1]
209 %tmp.10 = or uint %tmp.7, %tmp.9 ; <uint> [#uses=1]
210 ret uint %tmp.10
211}
Andrew Lenharth8426ee12005-11-02 18:34:05 +0000212
213long %test30(long %x) {
214 %tmp.2 = xor long %x, -9223372036854775808
215 ;; Add of sign bit -> xor of sign bit.
216 %tmp.4 = add long %tmp.2, -9223372036854775808
217 ret long %tmp.4
218}
Chris Lattnerba66f8e2006-03-04 06:02:36 +0000219
220int %test31(int %A) {
221 %B = add int %A, 4
222 %C = mul int %B, 5
223 %D = sub int %C, 20
224 ret int %D
225}
Chris Lattnercbde1002006-03-05 23:53:04 +0000226
227int %test32(int %A) {
228 %B = add int %A, 4
229 %C = shl int %B, ubyte 2
230 %D = sub int %C, 16
231 ret int %D
232}
Chris Lattner92fc0c062006-11-09 05:11:23 +0000233
234ubyte %test33(ubyte %A) { ;; OR A, 1
235 %B = and ubyte %A, 254
236 %C = add ubyte %B, 1
237 ret ubyte %C
238}
239
240ubyte %test34(ubyte %A) {
241 %B = add ubyte %A, 64 ;; dead
242 %C = and ubyte %B, 12
243 ret ubyte %C
244}
245
Reid Spenceraf6a4082007-01-30 16:16:01 +0000246int %test35(int %a) { ;; -> -1
Reid Spencerce380562007-01-26 08:25:06 +0000247 %tmpnot = xor int %a, -1
248 %tmp2 = add int %tmpnot, %a
249 ret int %tmp2
Chris Lattnerce0f91e2007-01-05 02:16:36 +0000250}
251