or.ll
7.23 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
; NOTE: Assertions have been autogenerated by utils/update_test_checks.py
; RUN: opt < %s -instsimplify -S | FileCheck %s
define i32 @test1(i32 %A) {
; CHECK-LABEL: @test1(
; CHECK-NEXT: ret i32 [[A:%.*]]
;
%B = or i32 %A, 0
ret i32 %B
}
define i32 @all_ones(i32 %A) {
; CHECK-LABEL: @all_ones(
; CHECK-NEXT: ret i32 -1
;
%B = or i32 %A, -1
ret i32 %B
}
define <3 x i8> @all_ones_vec_with_undef_elt(<3 x i8> %A) {
; CHECK-LABEL: @all_ones_vec_with_undef_elt(
; CHECK-NEXT: ret <3 x i8> <i8 -1, i8 -1, i8 -1>
;
%B = or <3 x i8> %A, <i8 -1, i8 undef, i8 -1>
ret <3 x i8> %B
}
define i1 @test3(i1 %A) {
; CHECK-LABEL: @test3(
; CHECK-NEXT: ret i1 [[A:%.*]]
;
%B = or i1 %A, false
ret i1 %B
}
define i1 @test4(i1 %A) {
; CHECK-LABEL: @test4(
; CHECK-NEXT: ret i1 true
;
%B = or i1 %A, true
ret i1 %B
}
define i1 @test5(i1 %A) {
; CHECK-LABEL: @test5(
; CHECK-NEXT: ret i1 [[A:%.*]]
;
%B = or i1 %A, %A
ret i1 %B
}
define i32 @test6(i32 %A) {
; CHECK-LABEL: @test6(
; CHECK-NEXT: ret i32 [[A:%.*]]
;
%B = or i32 %A, %A
ret i32 %B
}
; A | ~A == -1
define i32 @test7(i32 %A) {
; CHECK-LABEL: @test7(
; CHECK-NEXT: ret i32 -1
;
%NotA = xor i32 %A, -1
%B = or i32 %A, %NotA
ret i32 %B
}
define i8 @test8(i8 %A) {
; CHECK-LABEL: @test8(
; CHECK-NEXT: ret i8 -1
;
%B = or i8 %A, -2
%C = or i8 %B, 1
ret i8 %C
}
; Test that (A|c1)|(B|c2) == (A|B)|(c1|c2)
define i8 @test9(i8 %A, i8 %B) {
; CHECK-LABEL: @test9(
; CHECK-NEXT: ret i8 -1
;
%C = or i8 %A, 1
%D = or i8 %B, -2
%E = or i8 %C, %D
ret i8 %E
}
; (X & C1) | C2 --> (X | C2) & (C1|C2)
define i8 @test10(i8 %A) {
; CHECK-LABEL: @test10(
; CHECK-NEXT: ret i8 -2
;
%B = or i8 %A, 1
%C = and i8 %B, -2
%D = or i8 %C, -2
ret i8 %D
}
; The following two cases only get folded by InstCombine,
; see InstCombine/or-xor.ll.
; (X ^ C1) | C2 --> (X | C2) ^ (C1&~C2)
define i8 @test11(i8 %A) {
; CHECK-LABEL: @test11(
; CHECK-NEXT: [[B:%.*]] = or i8 [[A:%.*]], -2
; CHECK-NEXT: [[C:%.*]] = xor i8 [[B]], 13
; CHECK-NEXT: [[D:%.*]] = or i8 [[C]], 1
; CHECK-NEXT: [[E:%.*]] = xor i8 [[D]], 12
; CHECK-NEXT: ret i8 [[E]]
;
%B = or i8 %A, -2
%C = xor i8 %B, 13
%D = or i8 %C, 1
%E = xor i8 %D, 12
ret i8 %E
}
define i8 @test11v(<2 x i8> %A) {
; CHECK-LABEL: @test11v(
; CHECK-NEXT: [[B:%.*]] = or <2 x i8> [[A:%.*]], <i8 -2, i8 0>
; CHECK-NEXT: [[CV:%.*]] = xor <2 x i8> [[B]], <i8 13, i8 13>
; CHECK-NEXT: [[C:%.*]] = extractelement <2 x i8> [[CV]], i32 0
; CHECK-NEXT: [[D:%.*]] = or i8 [[C]], 1
; CHECK-NEXT: [[E:%.*]] = xor i8 [[D]], 12
; CHECK-NEXT: ret i8 [[E]]
;
%B = or <2 x i8> %A, <i8 -2, i8 0>
%CV = xor <2 x i8> %B, <i8 13, i8 13>
%C = extractelement <2 x i8> %CV, i32 0
%D = or i8 %C, 1
%E = xor i8 %D, 12
ret i8 %E
}
; Test the case where integer BitWidth <= 64 && BitWidth % 2 != 0.
; If we have: ((V + N) & C1) | (V & C2)
; .. and C2 = ~C1 and C2 is 0+1+ and (N & C2) == 0
; replace with V+N.
define i39 @test1_apint(i39 %V, i39 %M) {
; CHECK-LABEL: @test1_apint(
; CHECK-NEXT: [[N:%.*]] = and i39 [[M:%.*]], -274877906944
; CHECK-NEXT: [[A:%.*]] = add i39 [[V:%.*]], [[N]]
; CHECK-NEXT: ret i39 [[A]]
;
%C1 = xor i39 274877906943, -1 ;; C2 = 274877906943
%N = and i39 %M, 274877906944
%A = add i39 %V, %N
%B = and i39 %A, %C1
%D = and i39 %V, 274877906943
%R = or i39 %B, %D
ret i39 %R
}
define i7 @test2_apint(i7 %X) {
; CHECK-LABEL: @test2_apint(
; CHECK-NEXT: ret i7 [[X:%.*]]
;
%Y = or i7 %X, 0
ret i7 %Y
}
define i17 @test3_apint(i17 %X) {
; CHECK-LABEL: @test3_apint(
; CHECK-NEXT: ret i17 -1
;
%Y = or i17 %X, -1
ret i17 %Y
}
; Test the case where Integer BitWidth > 64 && BitWidth <= 1024.
; If we have: ((V + N) & C1) | (V & C2)
; .. and C2 = ~C1 and C2 is 0+1+ and (N & C2) == 0
; replace with V+N.
define i399 @test4_apint(i399 %V, i399 %M) {
; CHECK-LABEL: @test4_apint(
; CHECK-NEXT: [[N:%.*]] = and i399 [[M:%.*]], 18446742974197923840
; CHECK-NEXT: [[A:%.*]] = add i399 [[V:%.*]], [[N]]
; CHECK-NEXT: ret i399 [[A]]
;
%C1 = xor i399 274877906943, -1 ;; C2 = 274877906943
%N = and i399 %M, 18446742974197923840
%A = add i399 %V, %N
%B = and i399 %A, %C1
%D = and i399 %V, 274877906943
%R = or i399 %D, %B
ret i399 %R
}
define i777 @test5_apint(i777 %X) {
; CHECK-LABEL: @test5_apint(
; CHECK-NEXT: ret i777 [[X:%.*]]
;
%Y = or i777 %X, 0
ret i777 %Y
}
define i117 @test6_apint(i117 %X) {
; CHECK-LABEL: @test6_apint(
; CHECK-NEXT: ret i117 -1
;
%Y = or i117 %X, -1
ret i117 %Y
}
; Test the case where integer BitWidth <= 64 && BitWidth % 2 != 0.
; Vector version of test1_apint with the add commuted
; If we have: ((V + N) & C1) | (V & C2)
; .. and C2 = ~C1 and C2 is 0+1+ and (N & C2) == 0
; replace with V+N.
define <2 x i39> @test7_apint(<2 x i39> %V, <2 x i39> %M) {
; CHECK-LABEL: @test7_apint(
; CHECK-NEXT: [[N:%.*]] = and <2 x i39> [[M:%.*]], <i39 -274877906944, i39 -274877906944>
; CHECK-NEXT: [[A:%.*]] = add <2 x i39> [[N]], [[V:%.*]]
; CHECK-NEXT: ret <2 x i39> [[A]]
;
%C1 = xor <2 x i39> <i39 274877906943, i39 274877906943>, <i39 -1, i39 -1> ;; C2 = 274877906943
%N = and <2 x i39> %M, <i39 274877906944, i39 274877906944>
%A = add <2 x i39> %N, %V
%B = and <2 x i39> %A, %C1
%D = and <2 x i39> %V, <i39 274877906943, i39 274877906943>
%R = or <2 x i39> %B, %D
ret <2 x i39> %R
}
; Test the case where Integer BitWidth > 64 && BitWidth <= 1024.
; Vector version of test4_apint with the add and the or commuted
; If we have: ((V + N) & C1) | (V & C2)
; .. and C2 = ~C1 and C2 is 0+1+ and (N & C2) == 0
; replace with V+N.
define <2 x i399> @test8_apint(<2 x i399> %V, <2 x i399> %M) {
; CHECK-LABEL: @test8_apint(
; CHECK-NEXT: [[N:%.*]] = and <2 x i399> [[M:%.*]], <i399 18446742974197923840, i399 18446742974197923840>
; CHECK-NEXT: [[A:%.*]] = add <2 x i399> [[N]], [[V:%.*]]
; CHECK-NEXT: ret <2 x i399> [[A]]
;
%C1 = xor <2 x i399> <i399 274877906943, i399 274877906943>, <i399 -1, i399 -1> ;; C2 = 274877906943
%N = and <2 x i399> %M, <i399 18446742974197923840, i399 18446742974197923840>
%A = add <2 x i399> %N, %V
%B = and <2 x i399> %A, %C1
%D = and <2 x i399> %V, <i399 274877906943, i399 274877906943>
%R = or <2 x i399> %D, %B
ret <2 x i399> %R
}
; A | ~(A & B) = -1
define i1 @or_with_not_op_commute1(i1 %a, i1 %b) {
; CHECK-LABEL: @or_with_not_op_commute1(
; CHECK-NEXT: ret i1 true
;
%ab = and i1 %a, %b
%not = xor i1 %ab, -1
%r = or i1 %a, %not
ret i1 %r
}
; A | ~(B & A) = -1
define i8 @or_with_not_op_commute2(i8 %a, i8 %b) {
; CHECK-LABEL: @or_with_not_op_commute2(
; CHECK-NEXT: ret i8 -1
;
%ab = and i8 %b, %a
%not = xor i8 %ab, -1
%r = or i8 %a, %not
ret i8 %r
}
; ~(A & B) | A = -1
define <3 x i17> @or_with_not_op_commute3(<3 x i17> %a, <3 x i17> %b) {
; CHECK-LABEL: @or_with_not_op_commute3(
; CHECK-NEXT: ret <3 x i17> <i17 -1, i17 -1, i17 -1>
;
%ab = and <3 x i17> %a, %b
%not = xor <3 x i17> %ab, <i17 -1, i17 -1, i17 -1>
%r = or <3 x i17> %not, %a
ret <3 x i17> %r
}
; ~(B & A) | A = -1
define <2 x i1> @or_with_not_op_commute4(<2 x i1> %a, <2 x i1> %b) {
; CHECK-LABEL: @or_with_not_op_commute4(
; CHECK-NEXT: ret <2 x i1> <i1 true, i1 true>
;
%ab = and <2 x i1> %b, %a
%not = xor <2 x i1> %ab, <i1 -1, i1 undef>
%r = or <2 x i1> %not, %a
ret <2 x i1> %r
}