ec3aea3c877fecdd7f41595bb7de00f8c8269435
[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 | grep -v OK | not grep add
5
6 implementation
7
8 int %test1(int %A) {
9         %B = add int %A, 0
10         ret int %B
11 }
12
13 int %test2(int %A) {
14         %B = add int %A, 5
15         %C = add int %B, -5
16         ret int %C
17 }
18
19 int %test3(int %A) {
20         %B = add int %A, 5
21         %C = sub int %B, 5   ;; This should get converted to an add
22         ret int %C
23 }
24
25 int %test4(int %A, int %B) {
26         %C = sub int 0, %A
27         %D = add int %B, %C      ; D = B + -A = B - A
28         ret int %D
29 }
30
31 int %test5(int %A, int %B) {
32         %C = sub int 0, %A
33         %D = add int %C, %B      ; D = -A + B = B - A
34         ret int %D
35 }
36
37 int %test6(int %A) {
38         %B = mul int 7, %A
39         %C = add int %B, %A      ; C = 7*A+A == 8*A == A << 3
40         ret int %C
41 }
42
43 int %test7(int %A) {
44         %B = mul int 7, %A
45         %C = add int %A, %B      ; C = A+7*A == 8*A == A << 3
46         ret int %C
47 }
48
49 int %test8(int %A, int %B) {     ; (A & C1)+(B & C2) -> (A & C1)|(B & C2) iff C1&C2 == 0
50         %A1 = and int %A, 7
51         %B1 = and int %B, 128
52         %C = add int %A1, %B1
53         ret int %C
54 }
55
56 int %test9(int %A) {
57         %B = shl int %A, ubyte 4
58         %C = add int %B, %B      ; === shl int %A, 5
59         ret int %C
60 }
61
62 bool %test10(ubyte %A, ubyte %b) {
63         %B = add ubyte %A, %b
64         %c = setne ubyte %B, 0    ; === A != -b
65         ret bool %c
66 }
67
68 bool %test11(ubyte %A) {
69         %B = add ubyte %A, 255
70         %c = setne ubyte %B, 0    ; === A != 1
71         ret bool %c
72 }
73
74 int %test12(int %A, int %B) {
75         %C_OK = add int %B, %A       ; Should be transformed into shl A, 1
76         br label %X
77 X:
78         %D = add int %C_OK, %A 
79         ret int %D
80 }
81
82 int %test13(int %A, int %B, int %C) {
83         %D_OK = add int %A, %B
84         %E_OK = add int %D_OK, %C
85         %F = add int %E_OK, %A        ;; shl A, 1
86         ret int %F
87 }
88
89 uint %test14(uint %offset, uint %difference) {
90         %tmp.2 = and uint %difference, 3
91         %tmp.3_OK = add uint %tmp.2, %offset
92         %tmp.5.mask = and uint %difference, 4294967292
93         %tmp.8 = add uint %tmp.3_OK, %tmp.5.mask ; == add %offset, %difference
94         ret uint %tmp.8
95 }
96
97 ubyte %test15(ubyte %A) {
98         %B = add ubyte %A, 192  ; Does not effect result
99         %C = and ubyte %B, 16   ; Only one bit set
100         ret ubyte %C
101 }
102
103 ubyte %test16(ubyte %A) {
104         %B = add ubyte %A, 16   ; Turn this into a XOR
105         %C = and ubyte %B, 16   ; Only one bit set
106         ret ubyte %C
107 }
108
109 int %test17(int %A) {
110         %B = xor int %A, -1
111         %C = add int %B, 1      ; == sub int 0, %A
112         ret int %C
113 }
114
115 ubyte %test18(ubyte %A) {
116         %B = xor ubyte %A, 255
117         %C = add ubyte %B, 17      ; == sub ubyte 16, %A
118         ret ubyte %C
119 }
120
121 int %test19(bool %C) {
122         %A = select bool %C, int 1000, int 10
123         %V = add int %A, 123
124         ret int %V
125 }
126
127 int %test20(int %x) {
128         %tmp.2 = xor int %x, -2147483648
129         ;; Add of sign bit -> xor of sign bit.
130         %tmp.4 = add int %tmp.2, -2147483648
131         ret int %tmp.4
132 }
133
134 bool %test21(uint %x) {
135         %t = add uint %x, 4
136         %y = seteq uint %t, 123
137         ret bool %y
138 }
139
140 int %test22(uint %V) {
141         %V2 = add uint %V, 10
142         switch uint %V2, label %Default [
143                 uint 20, label %Lab1
144                 uint 30, label %Lab2
145         ]
146 Default:
147         ret int 123
148 Lab1:
149         ret int 12312
150 Lab2:
151         ret int 1231231
152 }
153
154 int %test23(bool %C, int %a) {
155 entry:
156         br bool %C, label %endif, label %else
157
158 else:
159         br label %endif
160
161 endif:
162         %b.0 = phi int [ 0, %entry ], [ 1, %else ]
163         %tmp.4 = add int %b.0, 1
164         ret int %tmp.4
165 }
166
167 int %test24(int %A) {
168         %B = add int %A, 1
169         %C = shl int %B, ubyte 1
170         %D = sub int %C, 2
171         ret int %D             ;; A << 1
172 }
173
174 long %test25(long %Y) {
175         %tmp.4 = shl long %Y, ubyte 2
176         %tmp.12 = shl long %Y, ubyte 2
177         %tmp.8 = add long %tmp.4, %tmp.12 ;; Y << 3
178         ret long %tmp.8
179 }
180
181 int %test26(int %A, int %B) {
182         %C = add int %A, %B
183         %D = sub int %C, %B
184         ret int %D
185 }
186
187 int %test27(bool %C, int %X, int %Y) {
188         %A = add int %X, %Y
189         %B = add int %Y, 123
190         %C = select bool %C, int %A, int %B  ;; Fold add through select.
191         %D = sub int %C, %Y
192         ret int %D
193 }
194
195 int %test28(int %X) {
196         %Y = add int %X, 1234
197         %Z = sub int 42, %Y
198         ret int %Z
199 }
200
201 uint %test29(uint %X, uint %x) {
202         %tmp.2 = sub uint %X, %x
203         %tmp.2.mask = and uint %tmp.2, 63               ; <uint> [#uses=1]
204         %tmp.6 = add uint %tmp.2.mask, %x               ; <uint> [#uses=1]
205         %tmp.7 = and uint %tmp.6, 63            ; <uint> [#uses=1]
206         %tmp.9 = and uint %tmp.2, 4294967232            ; <uint> [#uses=1]
207         %tmp.10 = or uint %tmp.7, %tmp.9                ; <uint> [#uses=1]
208         ret uint %tmp.10
209 }
210
211 long %test30(long %x) {
212         %tmp.2 = xor long %x, -9223372036854775808
213         ;; Add of sign bit -> xor of sign bit.
214         %tmp.4 = add long %tmp.2, -9223372036854775808
215         ret long %tmp.4
216 }
217
218 int %test31(int %A) {
219         %B = add int %A, 4
220         %C = mul int %B, 5
221         %D = sub int %C, 20
222         ret int %D
223 }
224
225 int %test32(int %A) {
226         %B = add int %A, 4
227         %C = shl int %B, ubyte 2
228         %D = sub int %C, 16
229         ret int %D
230 }
231
232 ubyte %test33(ubyte %A) {   ;; OR A, 1
233         %B = and ubyte %A, 254
234         %C = add ubyte %B, 1
235         ret ubyte %C
236 }
237
238 ubyte %test34(ubyte %A) {
239         %B = add ubyte %A, 64   ;; dead
240         %C = and ubyte %B, 12
241         ret ubyte %C
242 }
243
244 i32 %test34(i32 %a) {  ;; -> -1
245         %tmpnot = xor i32 %a, -1
246         %tmp2 = add i32 %tmpnot, %a
247         ret i32 %tmp2
248 }
249