aboutsummaryrefslogtreecommitdiff
path: root/llvm/test/Transforms/InstCombine/fold-selective-shift.ll
blob: 2b2296541f14a226b15d925737b5484c7f4e1791 (plain)
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
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
; NOTE: Assertions have been autogenerated by utils/update_test_checks.py UTC_ARGS: --version 6
; RUN: opt -passes=instcombine %s -S | FileCheck %s

declare void @clobber.i32(i32)

define i16 @selective_shift_16(i32 %mask, i16 %upper, i16 %lower) {
; CHECK-LABEL: define i16 @selective_shift_16(
; CHECK-SAME: i32 [[MASK:%.*]], i16 [[UPPER:%.*]], i16 [[LOWER:%.*]]) {
; CHECK-NEXT:    [[MASK_BIT:%.*]] = and i32 [[MASK]], 16
; CHECK-NEXT:    [[MASK_BIT_Z:%.*]] = icmp eq i32 [[MASK_BIT]], 0
; CHECK-NEXT:    [[SEL_V:%.*]] = select i1 [[MASK_BIT_Z]], i16 [[LOWER]], i16 [[UPPER]]
; CHECK-NEXT:    ret i16 [[SEL_V]]
;
  %upper.zext = zext i16 %upper to i32
  %upper.shl = shl nuw i32 %upper.zext, 16
  %lower.zext = zext i16 %lower to i32
  %pack = or disjoint i32 %upper.shl, %lower.zext
  %mask.bit = and i32 %mask, 16
  %sel = lshr i32 %pack, %mask.bit
  %trunc = trunc i32 %sel to i16
  ret i16 %trunc
}

define i16 @selective_shift_16.commute(i32 %mask, i16 %upper, i16 %lower) {
; CHECK-LABEL: define i16 @selective_shift_16.commute(
; CHECK-SAME: i32 [[MASK:%.*]], i16 [[UPPER:%.*]], i16 [[LOWER:%.*]]) {
; CHECK-NEXT:    [[MASK_BIT:%.*]] = and i32 [[MASK]], 16
; CHECK-NEXT:    [[MASK_BIT_Z:%.*]] = icmp eq i32 [[MASK_BIT]], 0
; CHECK-NEXT:    [[SEL_V:%.*]] = select i1 [[MASK_BIT_Z]], i16 [[LOWER]], i16 [[UPPER]]
; CHECK-NEXT:    ret i16 [[SEL_V]]
;
  %upper.zext = zext i16 %upper to i32
  %upper.shl = shl nuw i32 %upper.zext, 16
  %lower.zext = zext i16 %lower to i32
  %pack = or disjoint i32 %lower.zext, %upper.shl
  %mask.bit = and i32 %mask, 16
  %sel = lshr i32 %pack, %mask.bit
  %trunc = trunc i32 %sel to i16
  ret i16 %trunc
}

define i16 @selective_shift_16.range(i32 %mask, i32 %upper, i32 range(i32 0, 65536) %lower) {
; CHECK-LABEL: define i16 @selective_shift_16.range(
; CHECK-SAME: i32 [[MASK:%.*]], i32 [[UPPER:%.*]], i32 range(i32 0, 65536) [[LOWER:%.*]]) {
; CHECK-NEXT:    [[MASK_BIT:%.*]] = and i32 [[MASK]], 16
; CHECK-NEXT:    [[MASK_BIT_Z:%.*]] = icmp eq i32 [[MASK_BIT]], 0
; CHECK-NEXT:    [[SEL:%.*]] = select i1 [[MASK_BIT_Z]], i32 [[LOWER]], i32 [[UPPER]]
; CHECK-NEXT:    [[TRUNC:%.*]] = trunc i32 [[SEL]] to i16
; CHECK-NEXT:    ret i16 [[TRUNC]]
;
  %upper.shl = shl nuw i32 %upper, 16
  %pack = or disjoint i32 %upper.shl, %lower
  %mask.bit = and i32 %mask, 16
  %sel = lshr i32 %pack, %mask.bit
  %trunc = trunc i32 %sel to i16
  ret i16 %trunc
}

define i16 @selective_shift_16.range.commute(i32 %mask, i32 %upper, i32 range(i32 0, 65536) %lower) {
; CHECK-LABEL: define i16 @selective_shift_16.range.commute(
; CHECK-SAME: i32 [[MASK:%.*]], i32 [[UPPER:%.*]], i32 range(i32 0, 65536) [[LOWER:%.*]]) {
; CHECK-NEXT:    [[MASK_BIT:%.*]] = and i32 [[MASK]], 16
; CHECK-NEXT:    [[MASK_BIT_Z:%.*]] = icmp eq i32 [[MASK_BIT]], 0
; CHECK-NEXT:    [[SEL:%.*]] = select i1 [[MASK_BIT_Z]], i32 [[LOWER]], i32 [[UPPER]]
; CHECK-NEXT:    [[TRUNC:%.*]] = trunc i32 [[SEL]] to i16
; CHECK-NEXT:    ret i16 [[TRUNC]]
;
  %upper.shl = shl nuw i32 %upper, 16
  %pack = or disjoint i32 %lower, %upper.shl
  %mask.bit = and i32 %mask, 16
  %sel = lshr i32 %pack, %mask.bit
  %trunc = trunc i32 %sel to i16
  ret i16 %trunc
}

define i32 @selective_shift_16.masked(i32 %mask, i16 %upper, i16 %lower) {
; CHECK-LABEL: define i32 @selective_shift_16.masked(
; CHECK-SAME: i32 [[MASK:%.*]], i16 [[UPPER:%.*]], i16 [[LOWER:%.*]]) {
; CHECK-NEXT:    [[MASK_BIT:%.*]] = and i32 [[MASK]], 16
; CHECK-NEXT:    [[MASK_BIT_Z:%.*]] = icmp eq i32 [[MASK_BIT]], 0
; CHECK-NEXT:    [[SEL_V:%.*]] = select i1 [[MASK_BIT_Z]], i16 [[LOWER]], i16 [[UPPER]]
; CHECK-NEXT:    [[SEL:%.*]] = zext i16 [[SEL_V]] to i32
; CHECK-NEXT:    ret i32 [[SEL]]
;
  %upper.zext = zext i16 %upper to i32
  %upper.shl = shl nuw i32 %upper.zext, 16
  %lower.zext = zext i16 %lower to i32
  %pack = or disjoint i32 %lower.zext, %upper.shl
  %mask.bit = and i32 %mask, 16
  %sel = lshr i32 %pack, %mask.bit
  %sel.masked = and i32 %sel, 65535
  ret i32 %sel.masked
}

define i32 @selective_shift_16.masked.commute(i32 %mask, i16 %upper, i16 %lower) {
; CHECK-LABEL: define i32 @selective_shift_16.masked.commute(
; CHECK-SAME: i32 [[MASK:%.*]], i16 [[UPPER:%.*]], i16 [[LOWER:%.*]]) {
; CHECK-NEXT:    [[MASK_BIT:%.*]] = and i32 [[MASK]], 16
; CHECK-NEXT:    [[MASK_BIT_Z:%.*]] = icmp eq i32 [[MASK_BIT]], 0
; CHECK-NEXT:    [[SEL_V:%.*]] = select i1 [[MASK_BIT_Z]], i16 [[LOWER]], i16 [[UPPER]]
; CHECK-NEXT:    [[SEL:%.*]] = zext i16 [[SEL_V]] to i32
; CHECK-NEXT:    ret i32 [[SEL]]
;
  %upper.zext = zext i16 %upper to i32
  %upper.shl = shl nuw i32 %upper.zext, 16
  %lower.zext = zext i16 %lower to i32
  %pack = or disjoint i32 %upper.shl, %lower.zext
  %mask.bit = and i32 %mask, 16
  %sel = lshr i32 %pack, %mask.bit
  %sel.masked = and i32 %sel, 65535
  ret i32 %sel.masked
}

define <2 x i16> @selective_shift.v16(<2 x i32> %mask, <2 x i16> %upper, <2 x i16> %lower) {
; CHECK-LABEL: define <2 x i16> @selective_shift.v16(
; CHECK-SAME: <2 x i32> [[MASK:%.*]], <2 x i16> [[UPPER:%.*]], <2 x i16> [[LOWER:%.*]]) {
; CHECK-NEXT:    [[MASK_BIT:%.*]] = and <2 x i32> [[MASK]], splat (i32 16)
; CHECK-NEXT:    [[MASK_BIT_Z:%.*]] = icmp eq <2 x i32> [[MASK_BIT]], zeroinitializer
; CHECK-NEXT:    [[SEL_V:%.*]] = select <2 x i1> [[MASK_BIT_Z]], <2 x i16> [[LOWER]], <2 x i16> [[UPPER]]
; CHECK-NEXT:    ret <2 x i16> [[SEL_V]]
;
  %upper.zext = zext <2 x i16> %upper to <2 x i32>
  %upper.shl = shl nuw <2 x i32> %upper.zext, splat(i32 16)
  %lower.zext = zext <2 x i16> %lower to <2 x i32>
  %pack = or disjoint <2 x i32> %upper.shl, %lower.zext
  %mask.bit = and <2 x i32> %mask, splat(i32 16)
  %sel = lshr <2 x i32> %pack, %mask.bit
  %trunc = trunc <2 x i32> %sel to <2 x i16>
  ret <2 x i16> %trunc
}

define i16 @selective_shift_16.wide(i64 %mask, i16 %upper, i16 %lower) {
; CHECK-LABEL: define i16 @selective_shift_16.wide(
; CHECK-SAME: i64 [[MASK:%.*]], i16 [[UPPER:%.*]], i16 [[LOWER:%.*]]) {
; CHECK-NEXT:    [[MASK_BIT:%.*]] = and i64 [[MASK]], 16
; CHECK-NEXT:    [[MASK_BIT_Z:%.*]] = icmp eq i64 [[MASK_BIT]], 0
; CHECK-NEXT:    [[SEL_V:%.*]] = select i1 [[MASK_BIT_Z]], i16 [[LOWER]], i16 [[UPPER]]
; CHECK-NEXT:    ret i16 [[SEL_V]]
;
  %upper.zext = zext i16 %upper to i64
  %upper.shl = shl nuw i64 %upper.zext, 16
  %lower.zext = zext i16 %lower to i64
  %pack = or disjoint i64 %upper.shl, %lower.zext
  %mask.bit = and i64 %mask, 16
  %sel = lshr i64 %pack, %mask.bit
  %trunc = trunc i64 %sel to i16
  ret i16 %trunc
}

; narrow zext type blocks fold
define i16 @selective_shift_16.narrow(i24 %mask, i16 %upper, i16 %lower) {
; CHECK-LABEL: define i16 @selective_shift_16.narrow(
; CHECK-SAME: i24 [[MASK:%.*]], i16 [[UPPER:%.*]], i16 [[LOWER:%.*]]) {
; CHECK-NEXT:    [[UPPER_ZEXT:%.*]] = zext i16 [[UPPER]] to i24
; CHECK-NEXT:    [[UPPER_SHL:%.*]] = shl i24 [[UPPER_ZEXT]], 16
; CHECK-NEXT:    [[LOWER_ZEXT:%.*]] = zext i16 [[LOWER]] to i24
; CHECK-NEXT:    [[PACK:%.*]] = or disjoint i24 [[UPPER_SHL]], [[LOWER_ZEXT]]
; CHECK-NEXT:    [[MASK_BIT:%.*]] = and i24 [[MASK]], 16
; CHECK-NEXT:    [[SEL:%.*]] = lshr i24 [[PACK]], [[MASK_BIT]]
; CHECK-NEXT:    [[TRUNC:%.*]] = trunc i24 [[SEL]] to i16
; CHECK-NEXT:    ret i16 [[TRUNC]]
;
  %upper.zext = zext i16 %upper to i24
  %upper.shl = shl i24 %upper.zext, 16
  %lower.zext = zext i16 %lower to i24
  %pack = or disjoint i24 %upper.shl, %lower.zext
  %mask.bit = and i24 %mask, 16
  %sel = lshr i24 %pack, %mask.bit
  %trunc = trunc i24 %sel to i16
  ret i16 %trunc
}

; %lower's upper bits block fold
define i16 @selective_shift_16_norange(i32 %mask, i32 %upper, i32 %lower) {
; CHECK-LABEL: define i16 @selective_shift_16_norange(
; CHECK-SAME: i32 [[MASK:%.*]], i32 [[UPPER:%.*]], i32 [[LOWER:%.*]]) {
; CHECK-NEXT:    [[UPPER_SHL:%.*]] = shl nuw i32 [[UPPER]], 16
; CHECK-NEXT:    [[PACK:%.*]] = or i32 [[UPPER_SHL]], [[LOWER]]
; CHECK-NEXT:    [[MASK_BIT:%.*]] = and i32 [[MASK]], 16
; CHECK-NEXT:    [[SEL:%.*]] = lshr i32 [[PACK]], [[MASK_BIT]]
; CHECK-NEXT:    [[TRUNC:%.*]] = trunc i32 [[SEL]] to i16
; CHECK-NEXT:    ret i16 [[TRUNC]]
;
  %upper.shl = shl nuw i32 %upper, 16
  %pack = or i32 %upper.shl, %lower
  %mask.bit = and i32 %mask, 16
  %sel = lshr i32 %pack, %mask.bit
  %trunc = trunc i32 %sel to i16
  ret i16 %trunc
}

define i16 @selective_shift_16.mu.0(i32 %mask, i16 %upper, i16 %lower) {
; CHECK-LABEL: define i16 @selective_shift_16.mu.0(
; CHECK-SAME: i32 [[MASK:%.*]], i16 [[UPPER:%.*]], i16 [[LOWER:%.*]]) {
; CHECK-NEXT:    [[UPPER_ZEXT:%.*]] = zext i16 [[UPPER]] to i32
; CHECK-NEXT:    call void @clobber.i32(i32 [[UPPER_ZEXT]])
; CHECK-NEXT:    [[LOWER_ZEXT:%.*]] = zext i16 [[LOWER]] to i32
; CHECK-NEXT:    call void @clobber.i32(i32 [[LOWER_ZEXT]])
; CHECK-NEXT:    [[MASK_BIT:%.*]] = and i32 [[MASK]], 16
; CHECK-NEXT:    [[MASK_BIT_Z:%.*]] = icmp eq i32 [[MASK_BIT]], 0
; CHECK-NEXT:    [[TRUNC:%.*]] = select i1 [[MASK_BIT_Z]], i16 [[LOWER]], i16 [[UPPER]]
; CHECK-NEXT:    ret i16 [[TRUNC]]
;
  %upper.zext = zext i16 %upper to i32
  call void @clobber.i32(i32 %upper.zext)
  %upper.shl = shl nuw i32 %upper.zext, 16
  %lower.zext = zext i16 %lower to i32
  call void @clobber.i32(i32 %lower.zext)
  %pack = or disjoint i32 %upper.shl, %lower.zext
  %mask.bit = and i32 %mask, 16
  %sel = lshr i32 %pack, %mask.bit
  %trunc = trunc i32 %sel to i16
  ret i16 %trunc
}

; multi-use of %pack blocks fold
define i16 @selective_shift_16.mu.1(i32 %mask, i16 %upper, i16 %lower) {
; CHECK-LABEL: define i16 @selective_shift_16.mu.1(
; CHECK-SAME: i32 [[MASK:%.*]], i16 [[UPPER:%.*]], i16 [[LOWER:%.*]]) {
; CHECK-NEXT:    [[UPPER_ZEXT:%.*]] = zext i16 [[UPPER]] to i32
; CHECK-NEXT:    [[UPPER_SHL:%.*]] = shl nuw i32 [[UPPER_ZEXT]], 16
; CHECK-NEXT:    [[LOWER_ZEXT:%.*]] = zext i16 [[LOWER]] to i32
; CHECK-NEXT:    [[PACK:%.*]] = or disjoint i32 [[UPPER_SHL]], [[LOWER_ZEXT]]
; CHECK-NEXT:    call void @clobber.i32(i32 [[PACK]])
; CHECK-NEXT:    [[MASK_BIT:%.*]] = and i32 [[MASK]], 16
; CHECK-NEXT:    [[SEL:%.*]] = lshr i32 [[PACK]], [[MASK_BIT]]
; CHECK-NEXT:    [[TRUNC:%.*]] = trunc i32 [[SEL]] to i16
; CHECK-NEXT:    ret i16 [[TRUNC]]
;
  %upper.zext = zext i16 %upper to i32
  %upper.shl = shl nuw i32 %upper.zext, 16
  %lower.zext = zext i16 %lower to i32
  %pack = or disjoint i32 %upper.shl, %lower.zext
  call void @clobber.i32(i32 %pack)
  %mask.bit = and i32 %mask, 16
  %sel = lshr i32 %pack, %mask.bit
  %trunc = trunc i32 %sel to i16
  ret i16 %trunc
}

; non-truncated use of %sel blocks fold
define i16 @selective_shift_16.mu.2(i32 %mask, i16 %upper, i16 %lower) {
; CHECK-LABEL: define i16 @selective_shift_16.mu.2(
; CHECK-SAME: i32 [[MASK:%.*]], i16 [[UPPER:%.*]], i16 [[LOWER:%.*]]) {
; CHECK-NEXT:    [[UPPER_ZEXT:%.*]] = zext i16 [[UPPER]] to i32
; CHECK-NEXT:    [[UPPER_SHL:%.*]] = shl nuw i32 [[UPPER_ZEXT]], 16
; CHECK-NEXT:    [[LOWER_ZEXT:%.*]] = zext i16 [[LOWER]] to i32
; CHECK-NEXT:    [[PACK:%.*]] = or disjoint i32 [[UPPER_SHL]], [[LOWER_ZEXT]]
; CHECK-NEXT:    [[MASK_BIT:%.*]] = and i32 [[MASK]], 16
; CHECK-NEXT:    [[SEL:%.*]] = lshr i32 [[PACK]], [[MASK_BIT]]
; CHECK-NEXT:    call void @clobber.i32(i32 [[SEL]])
; CHECK-NEXT:    [[TRUNC:%.*]] = trunc i32 [[SEL]] to i16
; CHECK-NEXT:    ret i16 [[TRUNC]]
;
  %upper.zext = zext i16 %upper to i32
  %upper.shl = shl nuw i32 %upper.zext, 16
  %lower.zext = zext i16 %lower to i32
  %pack = or disjoint i32 %upper.shl, %lower.zext
  %mask.bit = and i32 %mask, 16
  %sel = lshr i32 %pack, %mask.bit
  call void @clobber.i32(i32 %sel)
  %trunc = trunc i32 %sel to i16
  ret i16 %trunc
}

; bitwidth must be a power of 2 to fold
define i24 @selective_shift_24(i48 %mask, i24 %upper, i24 %lower) {
; CHECK-LABEL: define i24 @selective_shift_24(
; CHECK-SAME: i48 [[MASK:%.*]], i24 [[UPPER:%.*]], i24 [[LOWER:%.*]]) {
; CHECK-NEXT:    [[UPPER_ZEXT:%.*]] = zext i24 [[UPPER]] to i48
; CHECK-NEXT:    [[UPPER_SHL:%.*]] = shl nuw i48 [[UPPER_ZEXT]], 24
; CHECK-NEXT:    [[LOWER_ZEXT:%.*]] = zext i24 [[LOWER]] to i48
; CHECK-NEXT:    [[PACK:%.*]] = or disjoint i48 [[UPPER_SHL]], [[LOWER_ZEXT]]
; CHECK-NEXT:    [[MASK_BIT:%.*]] = and i48 [[MASK]], 24
; CHECK-NEXT:    [[SEL:%.*]] = lshr i48 [[PACK]], [[MASK_BIT]]
; CHECK-NEXT:    [[TRUNC:%.*]] = trunc i48 [[SEL]] to i24
; CHECK-NEXT:    ret i24 [[TRUNC]]
;
  %upper.zext = zext i24 %upper to i48
  %upper.shl = shl nuw i48 %upper.zext, 24
  %lower.zext = zext i24 %lower to i48
  %pack = or disjoint i48 %upper.shl, %lower.zext
  %mask.bit = and i48 %mask, 24
  %sel = lshr i48 %pack, %mask.bit
  %trunc = trunc i48 %sel to i24
  ret i24 %trunc
}

define i32 @selective_shift_32(i64 %mask, i32 %upper, i32 %lower) {
; CHECK-LABEL: define i32 @selective_shift_32(
; CHECK-SAME: i64 [[MASK:%.*]], i32 [[UPPER:%.*]], i32 [[LOWER:%.*]]) {
; CHECK-NEXT:    [[MASK_BIT:%.*]] = and i64 [[MASK]], 32
; CHECK-NEXT:    [[MASK_BIT_Z:%.*]] = icmp eq i64 [[MASK_BIT]], 0
; CHECK-NEXT:    [[SEL_V:%.*]] = select i1 [[MASK_BIT_Z]], i32 [[LOWER]], i32 [[UPPER]]
; CHECK-NEXT:    ret i32 [[SEL_V]]
;
  %upper.zext = zext i32 %upper to i64
  %upper.shl = shl nuw i64 %upper.zext, 32
  %lower.zext = zext i32 %lower to i64
  %pack = or disjoint i64 %upper.shl, %lower.zext
  %mask.bit = and i64 %mask, 32
  %sel = lshr i64 %pack, %mask.bit
  %trunc = trunc i64 %sel to i32
  ret i32 %trunc
}

define i32 @selective_shift_32.commute(i64 %mask, i32 %upper, i32 %lower) {
; CHECK-LABEL: define i32 @selective_shift_32.commute(
; CHECK-SAME: i64 [[MASK:%.*]], i32 [[UPPER:%.*]], i32 [[LOWER:%.*]]) {
; CHECK-NEXT:    [[MASK_BIT:%.*]] = and i64 [[MASK]], 32
; CHECK-NEXT:    [[MASK_BIT_Z:%.*]] = icmp eq i64 [[MASK_BIT]], 0
; CHECK-NEXT:    [[SEL_V:%.*]] = select i1 [[MASK_BIT_Z]], i32 [[LOWER]], i32 [[UPPER]]
; CHECK-NEXT:    ret i32 [[SEL_V]]
;
  %upper.zext = zext i32 %upper to i64
  %upper.shl = shl nuw i64 %upper.zext, 32
  %lower.zext = zext i32 %lower to i64
  %pack = or disjoint i64 %lower.zext, %upper.shl
  %mask.bit = and i64 %mask, 32
  %sel = lshr i64 %pack, %mask.bit
  %trunc = trunc i64 %sel to i32
  ret i32 %trunc
}