blob: 382596ba87209d88cb0e6dcc1d76a6def4c2941a [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
Chris Lattnerb66f4242004-02-28 05:26:06 +00003; RUN: llvm-as < %s | opt -instcombine | llvm-dis | grep -v OK | not grep add
Chris Lattner7bfa8b62002-04-18 17:35:39 +00004
5implementation
6
Chris Lattnerde293ee2003-02-18 19:43:53 +00007int %test1(int %A) {
Chris Lattner7bfa8b62002-04-18 17:35:39 +00008 %B = add int %A, 0
9 ret int %B
Chris Lattnerde293ee2003-02-18 19:43:53 +000010}
Chris Lattner7bfa8b62002-04-18 17:35:39 +000011
Chris Lattnerde293ee2003-02-18 19:43:53 +000012int %test2(int %A) {
Chris Lattner7bfa8b62002-04-18 17:35:39 +000013 %B = add int %A, 5
14 %C = add int %B, -5
15 ret int %C
Chris Lattnerde293ee2003-02-18 19:43:53 +000016}
Chris Lattner7bfa8b62002-04-18 17:35:39 +000017
Chris Lattnerde293ee2003-02-18 19:43:53 +000018int %test3(int %A) {
Chris Lattner7bfa8b62002-04-18 17:35:39 +000019 %B = add int %A, 5
20 %C = sub int %B, 5 ;; This should get converted to an add
21 ret int %C
Chris Lattnerde293ee2003-02-18 19:43:53 +000022}
Chris Lattner7bfa8b62002-04-18 17:35:39 +000023
Chris Lattner2ae37b02003-02-18 19:43:21 +000024int %test4(int %A, int %B) {
Chris Lattnere0c3fbb2002-05-06 16:44:53 +000025 %C = sub int 0, %A
26 %D = add int %B, %C ; D = B + -A = B - A
27 ret int %D
28}
29
Chris Lattner2ae37b02003-02-18 19:43:21 +000030int %test5(int %A, int %B) {
Chris Lattnere0c3fbb2002-05-06 16:44:53 +000031 %C = sub int 0, %A
32 %D = add int %C, %B ; D = -A + B = B - A
33 ret int %D
34}
35
Chris Lattner39593402003-02-18 19:55:31 +000036int %test6(int %A) {
37 %B = mul int 7, %A
38 %C = add int %B, %A ; C = 7*A+A == 8*A == A << 3
39 ret int %C
40}
41
42int %test7(int %A) {
43 %B = mul int 7, %A
44 %C = add int %A, %B ; C = A+7*A == 8*A == A << 3
45 ret int %C
46}
47
Chris Lattnerdec13672003-03-10 23:52:54 +000048int %test8(int %A, int %B) { ; (A & C1)+(B & C2) -> (A & C1)|(B & C2) iff C1&C2 == 0
49 %A1 = and int %A, 7
50 %B1 = and int %B, 128
51 %C = add int %A1, %B1
52 ret int %C
53}
Chris Lattnercb269142003-03-11 00:10:59 +000054
Chris Lattnerdaff6442003-07-24 19:44:51 +000055int %test9(int %A) {
56 %B = shl int %A, ubyte 4
57 %C = add int %B, %B ; === shl int %A, 5
58 ret int %C
59}
Chris Lattner9b741f12003-08-13 05:27:57 +000060
61bool %test10(ubyte %A, ubyte %b) {
62 %B = add ubyte %A, %b
63 %c = setne ubyte %B, 0 ; === A != -b
64 ret bool %c
65}
66
67bool %test11(ubyte %A) {
68 %B = add ubyte %A, 255
69 %c = setne ubyte %B, 0 ; === A != 1
70 ret bool %c
71}
Chris Lattner895746a2003-08-13 19:01:09 +000072
73int %test12(int %A, int %B) {
74 %C_OK = add int %B, %A ; Should be transformed into shl A, 1
75 br label %X
76X:
77 %D = add int %C_OK, %A
78 ret int %D
79}
80
81int %test13(int %A, int %B, int %C) {
82 %D_OK = add int %A, %B
83 %E_OK = add int %D_OK, %C
84 %F = add int %E_OK, %A ;; shl A, 1
85 ret int %F
86}
87
88uint %test14(uint %offset, uint %difference) {
89 %tmp.2 = and uint %difference, 3
90 %tmp.3_OK = add uint %tmp.2, %offset
91 %tmp.5.mask = and uint %difference, 4294967292
92 %tmp.8 = add uint %tmp.3_OK, %tmp.5.mask ; == add %offset, %difference
93 ret uint %tmp.8
94}
95
Chris Lattnerdef041c2003-09-19 15:33:46 +000096ubyte %test15(ubyte %A) {
97 %B = add ubyte %A, 192 ; Does not effect result
98 %C = and ubyte %B, 16 ; Only one bit set
99 ret ubyte %C
100}
101
102ubyte %test16(ubyte %A) {
103 %B = add ubyte %A, 16 ; Turn this into a XOR
104 %C = and ubyte %B, 16 ; Only one bit set
105 ret ubyte %C
106}
107
Chris Lattnerd15f2732003-10-02 15:11:09 +0000108int %test17(int %A) {
109 %B = xor int %A, -1
110 %C = add int %B, 1 ; == sub int 0, %A
111 ret int %C
112}
113
114ubyte %test18(ubyte %A) {
115 %B = xor ubyte %A, 255
116 %C = add ubyte %B, 17 ; == sub ubyte 16, %A
117 ret ubyte %C
118}
Chris Lattnere4ad3e52004-04-09 23:47:52 +0000119
120int %test19(bool %C) {
121 %A = select bool %C, int 1000, int 10
122 %V = add int %A, 123
123 ret int %V
124}
125
Chris Lattner825a0012004-04-10 22:01:27 +0000126int %test20(int %x) {
127 %tmp.2 = xor int %x, -2147483648
128 ;; Add of sign bit -> xor of sign bit.
129 %tmp.4 = add int %tmp.2, -2147483648
130 ret int %tmp.4
131}
Chris Lattnere4ad3e52004-04-09 23:47:52 +0000132
Chris Lattner0da061f2004-06-27 22:51:19 +0000133bool %test21(uint %x) {
134 %t = add uint %x, 4
135 %y = seteq uint %t, 123
136 ret bool %y
137}
Chris Lattner3a03c902004-07-03 00:25:31 +0000138
139int %test22(uint %V) {
140 %V2 = add uint %V, 10
141 switch uint %V2, label %Default [
142 uint 20, label %Lab1
143 uint 30, label %Lab2
144 ]
145Default:
146 ret int 123
147Lab1:
148 ret int 12312
149Lab2:
150 ret int 1231231
151}
Chris Lattner5e0ab5c2004-09-29 05:06:12 +0000152
153int %test23(bool %C, int %a) {
154entry:
155 br bool %C, label %endif, label %else
156
157else:
158 br label %endif
159
160endif:
161 %b.0 = phi int [ 0, %entry ], [ 1, %else ]
162 %tmp.4 = add int %b.0, 1
163 ret int %tmp.4
164}
165
Chris Lattner7bfe4032004-10-08 03:41:59 +0000166int %test24(int %A) {
167 %B = add int %A, 1
168 %C = shl int %B, ubyte 1
169 %D = sub int %C, 2
170 ret int %D ;; A << 1
171}
Chris Lattnerf6392b42004-11-13 19:49:39 +0000172
173long %test25(long %Y) {
174 %tmp.4 = shl long %Y, ubyte 2
175 %tmp.12 = shl long %Y, ubyte 2
176 %tmp.8 = add long %tmp.4, %tmp.12 ;; Y << 3
177 ret long %tmp.8
178}
179
Chris Lattner2547f052005-01-19 21:48:31 +0000180int %test26(int %A, int %B) {
181 %C = add int %A, %B
182 %D = sub int %C, %B
183 ret int %D
184}
185
186int %test27(bool %C, int %X, int %Y) {
187 %A = add int %X, %Y
188 %B = add int %Y, 123
189 %C = select bool %C, int %A, int %B ;; Fold add through select.
190 %D = sub int %C, %Y
191 ret int %D
192}
193
Chris Lattnerdd831832005-04-07 16:24:59 +0000194int %test28(int %X) {
195 %Y = add int %X, 1234
196 %Z = sub int 42, %Y
197 ret int %Z
198}
Chris Lattner9136c832005-09-18 04:22:59 +0000199
200uint %test29(uint %X, uint %x) {
201 %tmp.2 = sub uint %X, %x
202 %tmp.2.mask = and uint %tmp.2, 63 ; <uint> [#uses=1]
203 %tmp.6 = add uint %tmp.2.mask, %x ; <uint> [#uses=1]
204 %tmp.7 = and uint %tmp.6, 63 ; <uint> [#uses=1]
205 %tmp.9 = and uint %tmp.2, 4294967232 ; <uint> [#uses=1]
206 %tmp.10 = or uint %tmp.7, %tmp.9 ; <uint> [#uses=1]
207 ret uint %tmp.10
208}
Andrew Lenharth8426ee12005-11-02 18:34:05 +0000209
210long %test30(long %x) {
211 %tmp.2 = xor long %x, -9223372036854775808
212 ;; Add of sign bit -> xor of sign bit.
213 %tmp.4 = add long %tmp.2, -9223372036854775808
214 ret long %tmp.4
215}
Chris Lattnerba66f8e2006-03-04 06:02:36 +0000216
217int %test31(int %A) {
218 %B = add int %A, 4
219 %C = mul int %B, 5
220 %D = sub int %C, 20
221 ret int %D
222}
Chris Lattnercbde1002006-03-05 23:53:04 +0000223
224int %test32(int %A) {
225 %B = add int %A, 4
226 %C = shl int %B, ubyte 2
227 %D = sub int %C, 16
228 ret int %D
229}
Chris Lattner92fc0c062006-11-09 05:11:23 +0000230
231ubyte %test33(ubyte %A) { ;; OR A, 1
232 %B = and ubyte %A, 254
233 %C = add ubyte %B, 1
234 ret ubyte %C
235}
236
237ubyte %test34(ubyte %A) {
238 %B = add ubyte %A, 64 ;; dead
239 %C = and ubyte %B, 12
240 ret ubyte %C
241}
242