Changes to support making the shift instructions be true BinaryOperators.
[oota-llvm.git] / test / Transforms / InstCombine / add.ll
1 ; This test makes sure that add instructions are properly eliminated.
2
3 ; RUN: llvm-upgrade < %s | llvm-as | opt -instcombine -disable-output &&
4 ; RUN: llvm-upgrade < %s | llvm-as | opt -instcombine | llvm-dis | \
5 ; RUN:    grep -v OK | not grep add
6
7 implementation
8
9 int %test1(int %A) {
10         %B = add int %A, 0
11         ret int %B
12 }
13
14 int %test2(int %A) {
15         %B = add int %A, 5
16         %C = add int %B, -5
17         ret int %C
18 }
19
20 int %test3(int %A) {
21         %B = add int %A, 5
22         %C = sub int %B, 5   ;; This should get converted to an add
23         ret int %C
24 }
25
26 int %test4(int %A, int %B) {
27         %C = sub int 0, %A
28         %D = add int %B, %C      ; D = B + -A = B - A
29         ret int %D
30 }
31
32 int %test5(int %A, int %B) {
33         %C = sub int 0, %A
34         %D = add int %C, %B      ; D = -A + B = B - A
35         ret int %D
36 }
37
38 int %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
44 int %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
50 ; (A & C1)+(B & C2) -> (A & C1)|(B & C2) iff C1&C2 == 0
51 int %test8(int %A, int %B) {     
52         %A1 = and int %A, 7
53         %B1 = and int %B, 128
54         %C = add int %A1, %B1
55         ret int %C
56 }
57
58 int %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 }
63
64 bool %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
70 bool %test11(ubyte %A) {
71         %B = add ubyte %A, 255
72         %c = setne ubyte %B, 0    ; === A != 1
73         ret bool %c
74 }
75
76 int %test12(int %A, int %B) {
77         %C_OK = add int %B, %A       ; Should be transformed into shl A, 1
78         br label %X
79 X:
80         %D = add int %C_OK, %A 
81         ret int %D
82 }
83
84 int %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
91 uint %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
99 ubyte %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
105 ubyte %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
111 int %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
117 ubyte %test18(ubyte %A) {
118         %B = xor ubyte %A, 255
119         %C = add ubyte %B, 17      ; == sub ubyte 16, %A
120         ret ubyte %C
121 }
122
123 int %test19(bool %C) {
124         %A = select bool %C, int 1000, int 10
125         %V = add int %A, 123
126         ret int %V
127 }
128
129 int %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 }
135
136 bool %test21(uint %x) {
137         %t = add uint %x, 4
138         %y = seteq uint %t, 123
139         ret bool %y
140 }
141
142 int %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         ]
148 Default:
149         ret int 123
150 Lab1:
151         ret int 12312
152 Lab2:
153         ret int 1231231
154 }
155
156 int %test23(bool %C, int %a) {
157 entry:
158         br bool %C, label %endif, label %else
159
160 else:
161         br label %endif
162
163 endif:
164         %b.0 = phi int [ 0, %entry ], [ 1, %else ]
165         %tmp.4 = add int %b.0, 1
166         ret int %tmp.4
167 }
168
169 int %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 }
175
176 long %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
183 int %test26(int %A, int %B) {
184         %C = add int %A, %B
185         %D = sub int %C, %B
186         ret int %D
187 }
188
189 int %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
197 int %test28(int %X) {
198         %Y = add int %X, 1234
199         %Z = sub int 42, %Y
200         ret int %Z
201 }
202
203 uint %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 }
212
213 long %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 }
219
220 int %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 }
226
227 int %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 }
233
234 ubyte %test33(ubyte %A) {   ;; OR A, 1
235         %B = and ubyte %A, 254
236         %C = add ubyte %B, 1
237         ret ubyte %C
238 }
239
240 ubyte %test34(ubyte %A) {
241         %B = add ubyte %A, 64   ;; dead
242         %C = and ubyte %B, 12
243         ret ubyte %C
244 }
245
246 int %test35(int %a) {  ;; -> -1
247         %tmpnot = xor int %a, -1
248         %tmp2 = add int %tmpnot, %a
249         ret int %tmp2
250 }
251