Use the llvm-upgrade program to upgrade llvm assembly.
[oota-llvm.git] / test / Transforms / InstCombine / xor.ll
1 ; This test makes sure that these instructions are properly eliminated.
2 ;
3
4 ; RUN: llvm-upgrade < %s | llvm-as | opt -instcombine -disable-output &&
5 ; RUN: llvm-upgrade < %s | llvm-as | opt -instcombine | llvm-dis | not grep 'xor '
6
7 %G1 = global uint 0
8 %G2 = global uint 0
9
10 implementation
11
12 bool %test0(bool %A) {
13         %B = xor bool %A, false
14         ret bool %B
15 }
16
17 int %test1(int %A) {
18         %B = xor int %A, 0
19         ret int %B
20 }
21
22 bool %test2(bool %A) {
23         %B = xor bool %A, %A
24         ret bool %B
25 }
26
27 int %test3(int %A) {
28         %B = xor int %A, %A
29         ret int %B
30 }
31
32 int %test4(int %A) {    ; A ^ ~A == -1
33         %NotA = xor int -1, %A
34         %B = xor int %A, %NotA
35         ret int %B
36 }
37
38 uint %test5(uint %A) { ; (A|B)^B == A & (~B)
39         %t1 = or uint %A, 123
40         %r  = xor uint %t1, 123
41         ret uint %r
42 }
43
44 ubyte %test6(ubyte %A) {
45         %B = xor ubyte %A, 17
46         %C = xor ubyte %B, 17
47         ret ubyte %C
48 }
49
50 ; (A & C1)^(B & C2) -> (A & C1)|(B & C2) iff C1&C2 == 0
51 int %test7(int %A, int %B) {
52
53         %A1 = and int %A, 7
54         %B1 = and int %B, 128
55         %C1 = xor int %A1, %B1
56         ret int %C1
57 }
58
59 ubyte %test8(bool %c) {
60         %d = xor bool %c, true    ; invert the condition
61         br bool %d, label %True, label %False
62 True:
63         ret ubyte 1
64 False:
65         ret ubyte 3
66 }
67
68 bool %test9(ubyte %A) {
69         %B = xor ubyte %A, 123      ; xor can be eliminated
70         %C = seteq ubyte %B, 34
71         ret bool %C
72 }
73
74 ubyte %test10(ubyte %A) {
75         %B = and ubyte %A, 3
76         %C = xor ubyte %B, 4        ; transform into an OR
77         ret ubyte %C
78 }
79
80 ubyte %test11(ubyte %A) {
81         %B = or ubyte %A, 12
82         %C = xor ubyte %B, 4        ; transform into an AND
83         ret ubyte %C
84 }
85
86 bool %test12(ubyte %A) {
87         %B = xor ubyte %A, 4
88         %c = setne ubyte %B, 0
89         ret bool %c
90 }
91
92 bool %test13(ubyte %A, ubyte %B) {
93         %C = setlt ubyte %A, %B
94         %D = setgt ubyte %A, %B
95         %E = xor bool %C, %D        ; E = setne %A, %B
96         ret bool %E
97 }
98
99 bool %test14(ubyte %A, ubyte %B) {
100         %C = seteq ubyte %A, %B
101         %D = setne ubyte %B, %A
102         %E = xor bool %C, %D        ; E = true
103         ret bool %E
104 }
105
106 uint %test15(uint %A) {             ; ~(X-1) == -X
107         %B = add uint %A, 4294967295
108         %C = xor uint %B, 4294967295
109         ret uint %C
110 }
111
112 uint %test16(uint %A) {             ; ~(X+c) == (-c-1)-X
113         %B = add uint %A, 123       ; A generalization of the previous case
114         %C = xor uint %B, 4294967295
115         ret uint %C
116 }
117
118 uint %test17(uint %A) {             ; ~(c-X) == X-(c-1) == X+(-c+1)
119         %B = sub uint 123, %A
120         %C = xor uint %B, 4294967295
121         ret uint %C
122 }
123
124 uint %test18(uint %A) {             ; C - ~X == X + (1+C)
125         %B = xor uint %A, 4294967295; -~X == 0 - ~X == X+1
126         %C = sub uint 123, %B
127         ret uint %C
128 }
129
130 uint %test19(uint %A, uint %B) {
131         %C = xor uint %A, %B
132         %D = xor uint %C, %A  ; A terms cancel, D = B
133         ret uint %D
134 }
135
136 void %test20(uint %A, uint %B) {  ; The "swap idiom"
137         %tmp.2 = xor uint %B, %A
138         %tmp.5 = xor uint %tmp.2, %B
139         %tmp.8 = xor uint %tmp.5, %tmp.2
140         store uint %tmp.8, uint* %G1   ; tmp.8 = B
141         store uint %tmp.5, uint* %G2   ; tmp.5 = A
142         ret void
143 }
144
145 int %test21(bool %C, int %A, int %B) {
146         %C2 = xor bool %C, true
147         %D = select bool %C2, int %A, int %B
148         ret int %D
149 }
150
151 int %test22(bool %X) {
152         %Y = xor bool %X, true
153         %Z = cast bool %Y to int
154         %Q = xor int %Z, 1
155         ret int %Q
156 }
157
158 bool %test23(int %a, int %b) {
159         %tmp.2 = xor int %b, %a
160         %tmp.4 = seteq int %tmp.2, %a
161         ret bool %tmp.4
162 }
163
164 bool %test24(int %c, int %d) {
165         %tmp.2 = xor int %d, %c
166         %tmp.4 = setne int %tmp.2, %c
167         ret bool %tmp.4
168 }
169
170 int %test25(int %g, int %h) {
171         %h2 = xor int %h, -1
172         %tmp2 = and int %h2, %g
173         %tmp4 = xor int %tmp2, %g  ; (h2&g)^g -> ~h2 & g -> h & g
174         ret int %tmp4
175 }
176
177 int %test26(int %a, int %b) {
178         %b2 = xor int %b, -1
179         %tmp2 = xor int %a, %b2
180         %tmp4 = and int %tmp2, %a  ; (a^b2)&a -> ~b2 & a -> b & a
181         ret int %tmp4
182 }
183