Add some new tests
[oota-llvm.git] / test / Transforms / InstCombine / add.ll
1 ; This test makes sure that add instructions are properly eliminated.
2
3 ; RUN: llvm-as < %s | opt -instcombine | llvm-dis | grep -v OK | not grep add
4
5 implementation
6
7 int %test1(int %A) {
8         %B = add int %A, 0
9         ret int %B
10 }
11
12 int %test2(int %A) {
13         %B = add int %A, 5
14         %C = add int %B, -5
15         ret int %C
16 }
17
18 int %test3(int %A) {
19         %B = add int %A, 5
20         %C = sub int %B, 5   ;; This should get converted to an add
21         ret int %C
22 }
23
24 int %test4(int %A, int %B) {
25         %C = sub int 0, %A
26         %D = add int %B, %C      ; D = B + -A = B - A
27         ret int %D
28 }
29
30 int %test5(int %A, int %B) {
31         %C = sub int 0, %A
32         %D = add int %C, %B      ; D = -A + B = B - A
33         ret int %D
34 }
35
36 int %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
42 int %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
48 int %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 }
54
55 int %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 }
60
61 bool %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
67 bool %test11(ubyte %A) {
68         %B = add ubyte %A, 255
69         %c = setne ubyte %B, 0    ; === A != 1
70         ret bool %c
71 }
72
73 int %test12(int %A, int %B) {
74         %C_OK = add int %B, %A       ; Should be transformed into shl A, 1
75         br label %X
76 X:
77         %D = add int %C_OK, %A 
78         ret int %D
79 }
80
81 int %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
88 uint %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
96 ubyte %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
102 ubyte %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
108 int %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
114 ubyte %test18(ubyte %A) {
115         %B = xor ubyte %A, 255
116         %C = add ubyte %B, 17      ; == sub ubyte 16, %A
117         ret ubyte %C
118 }
119
120 int %test19(bool %C) {
121         %A = select bool %C, int 1000, int 10
122         %V = add int %A, 123
123         ret int %V
124 }
125
126 int %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 }
132
133 bool %test21(uint %x) {
134         %t = add uint %x, 4
135         %y = seteq uint %t, 123
136         ret bool %y
137 }
138
139 int %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         ]
145 Default:
146         ret int 123
147 Lab1:
148         ret int 12312
149 Lab2:
150         ret int 1231231
151 }
152
153 int %test23(bool %C, int %a) {
154 entry:
155         br bool %C, label %endif, label %else
156
157 else:
158         br label %endif
159
160 endif:
161         %b.0 = phi int [ 0, %entry ], [ 1, %else ]
162         %tmp.4 = add int %b.0, 1
163         ret int %tmp.4
164 }
165
166 int %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 }
172
173 long %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
180 int %test26(int %A, int %B) {
181         %C = add int %A, %B
182         %D = sub int %C, %B
183         ret int %D
184 }
185
186 int %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