1 ; RUN: opt -passes='require<profile-summary>,function(codegenprepare)' < %s -mtriple=aarch64-apple-ios -S | FileCheck -enable-var-scope %s --check-prefix=OPTALL --check-prefix=OPT --check-prefix=NONSTRESS
2 ; RUN: opt -passes='require<profile-summary>,function(codegenprepare)' < %s -mtriple=aarch64-apple-ios -S -stress-cgp-ext-ld-promotion | FileCheck -enable-var-scope %s --check-prefix=OPTALL --check-prefix=OPT --check-prefix=STRESS
3 ; RUN: opt -passes='require<profile-summary>,function(codegenprepare)' < %s -mtriple=aarch64-apple-ios -S -disable-cgp-ext-ld-promotion | FileCheck -enable-var-scope %s --check-prefix=OPTALL --check-prefix=DISABLE
5 ; CodeGenPrepare should move the zext into the block with the load
6 ; so that SelectionDAG can select it with the load.
9 ; OPTALL: [[LD:%[a-zA-Z_0-9-]+]] = load i8, ptr %p
10 ; OPTALL-NEXT: [[ZEXT:%[a-zA-Z_0-9-]+]] = zext i8 [[LD]] to i32
11 ; OPTALL: store i32 [[ZEXT]], ptr %q
13 define void @foo(ptr %p, ptr %q) {
16 %a = icmp slt i8 %t, 20
17 br i1 %a, label %true, label %false
19 %s = zext i8 %t to i32
26 ; Check that we manage to form a zextload is an operation with only one
27 ; argument to explicitly extend is in the way.
28 ; OPTALL-LABEL: @promoteOneArg
29 ; OPTALL: [[LD:%[a-zA-Z_0-9-]+]] = load i8, ptr %p
30 ; OPT-NEXT: [[ZEXT:%[a-zA-Z_0-9-]+]] = zext i8 [[LD]] to i32
31 ; OPT-NEXT: [[RES:%[a-zA-Z_0-9-]+]] = add nuw i32 [[ZEXT]], 2
32 ; Make sure the operation is not promoted when the promotion pass is disabled.
33 ; DISABLE: [[ADD:%[a-zA-Z_0-9-]+]] = add nuw i8 [[LD]], 2
34 ; DISABLE: [[RES:%[a-zA-Z_0-9-]+]] = zext i8 [[ADD]] to i32
35 ; OPTALL: store i32 [[RES]], ptr %q
37 define void @promoteOneArg(ptr %p, ptr %q) {
40 %add = add nuw i8 %t, 2
41 %a = icmp slt i8 %t, 20
42 br i1 %a, label %true, label %false
44 %s = zext i8 %add to i32
51 ; Check that we manage to form a sextload is an operation with only one
52 ; argument to explicitly extend is in the way.
54 ; OPTALL-LABEL: @promoteOneArgSExt
55 ; OPTALL: [[LD:%[a-zA-Z_0-9-]+]] = load i8, ptr %p
56 ; OPT-NEXT: [[SEXT:%[a-zA-Z_0-9-]+]] = sext i8 [[LD]] to i32
57 ; OPT-NEXT: [[RES:%[a-zA-Z_0-9-]+]] = add nsw i32 [[SEXT]], 2
58 ; DISABLE: [[ADD:%[a-zA-Z_0-9-]+]] = add nsw i8 [[LD]], 2
59 ; DISABLE: [[RES:%[a-zA-Z_0-9-]+]] = sext i8 [[ADD]] to i32
60 ; OPTALL: store i32 [[RES]], ptr %q
62 define void @promoteOneArgSExt(ptr %p, ptr %q) {
65 %add = add nsw i8 %t, 2
66 %a = icmp slt i8 %t, 20
67 br i1 %a, label %true, label %false
69 %s = sext i8 %add to i32
76 ; Check that we manage to form a zextload is an operation with two
77 ; arguments to explicitly extend is in the way.
78 ; Extending %add will create two extensions:
81 ; #1 will not be removed as we do not know anything about %b.
82 ; #2 may not be merged with the load because %t is used in a comparison.
83 ; Since two extensions may be emitted in the end instead of one before the
84 ; transformation, the regular heuristic does not apply the optimization.
86 ; OPTALL-LABEL: @promoteTwoArgZext
87 ; OPTALL: [[LD:%[a-zA-Z_0-9-]+]] = load i8, ptr %p
89 ; STRESS-NEXT: [[ZEXTLD:%[a-zA-Z_0-9-]+]] = zext i8 [[LD]] to i32
90 ; STRESS-NEXT: [[ZEXTB:%[a-zA-Z_0-9-]+]] = zext i8 %b to i32
91 ; STRESS-NEXT: [[RES:%[a-zA-Z_0-9-]+]] = add nuw i32 [[ZEXTLD]], [[ZEXTB]]
93 ; NONSTRESS: [[ADD:%[a-zA-Z_0-9-]+]] = add nuw i8 [[LD]], %b
94 ; NONSTRESS: [[RES:%[a-zA-Z_0-9-]+]] = zext i8 [[ADD]] to i32
96 ; DISABLE: [[ADD:%[a-zA-Z_0-9-]+]] = add nuw i8 [[LD]], %b
97 ; DISABLE: [[RES:%[a-zA-Z_0-9-]+]] = zext i8 [[ADD]] to i32
99 ; OPTALL: store i32 [[RES]], ptr %q
101 define void @promoteTwoArgZext(ptr %p, ptr %q, i8 %b) {
104 %add = add nuw i8 %t, %b
105 %a = icmp slt i8 %t, 20
106 br i1 %a, label %true, label %false
108 %s = zext i8 %add to i32
115 ; Check that we manage to form a sextload is an operation with two
116 ; arguments to explicitly extend is in the way.
118 ; OPTALL-LABEL: @promoteTwoArgSExt
119 ; OPTALL: [[LD:%[a-zA-Z_0-9-]+]] = load i8, ptr %p
121 ; STRESS-NEXT: [[SEXTLD:%[a-zA-Z_0-9-]+]] = sext i8 [[LD]] to i32
122 ; STRESS-NEXT: [[SEXTB:%[a-zA-Z_0-9-]+]] = sext i8 %b to i32
123 ; STRESS-NEXT: [[RES:%[a-zA-Z_0-9-]+]] = add nsw i32 [[SEXTLD]], [[SEXTB]]
125 ; NONSTRESS: [[ADD:%[a-zA-Z_0-9-]+]] = add nsw i8 [[LD]], %b
126 ; NONSTRESS: [[RES:%[a-zA-Z_0-9-]+]] = sext i8 [[ADD]] to i32
128 ; DISABLE: [[ADD:%[a-zA-Z_0-9-]+]] = add nsw i8 [[LD]], %b
129 ; DISABLE: [[RES:%[a-zA-Z_0-9-]+]] = sext i8 [[ADD]] to i32
130 ; OPTALL: store i32 [[RES]], ptr %q
132 define void @promoteTwoArgSExt(ptr %p, ptr %q, i8 %b) {
135 %add = add nsw i8 %t, %b
136 %a = icmp slt i8 %t, 20
137 br i1 %a, label %true, label %false
139 %s = sext i8 %add to i32
146 ; Check that we do not a zextload if we need to introduce more than
147 ; one additional extension.
148 ; OPTALL-LABEL: @promoteThreeArgZext
149 ; OPTALL: [[LD:%[a-zA-Z_0-9-]+]] = load i8, ptr %p
151 ; STRESS-NEXT: [[ZEXTLD:%[a-zA-Z_0-9-]+]] = zext i8 [[LD]] to i32
152 ; STRESS-NEXT: [[ZEXTB:%[a-zA-Z_0-9-]+]] = zext i8 %b to i32
153 ; STRESS-NEXT: [[TMP:%[a-zA-Z_0-9-]+]] = add nuw i32 [[ZEXTLD]], [[ZEXTB]]
154 ; STRESS-NEXT: [[ZEXTC:%[a-zA-Z_0-9-]+]] = zext i8 %c to i32
155 ; STRESS-NEXT: [[RES:%[a-zA-Z_0-9-]+]] = add nuw i32 [[TMP]], [[ZEXTC]]
157 ; NONSTRESS-NEXT: [[TMP:%[a-zA-Z_0-9-]+]] = add nuw i8 [[LD]], %b
158 ; NONSTRESS-NEXT: [[ADD:%[a-zA-Z_0-9-]+]] = add nuw i8 [[TMP]], %c
159 ; NONSTRESS: [[RES:%[a-zA-Z_0-9-]+]] = zext i8 [[ADD]] to i32
161 ; DISABLE: add nuw i8
162 ; DISABLE: [[ADD:%[a-zA-Z_0-9-]+]] = add nuw i8
163 ; DISABLE: [[RES:%[a-zA-Z_0-9-]+]] = zext i8 [[ADD]] to i32
165 ; OPTALL: store i32 [[RES]], ptr %q
167 define void @promoteThreeArgZext(ptr %p, ptr %q, i8 %b, i8 %c) {
170 %tmp = add nuw i8 %t, %b
171 %add = add nuw i8 %tmp, %c
172 %a = icmp slt i8 %t, 20
173 br i1 %a, label %true, label %false
175 %s = zext i8 %add to i32
182 ; Check that we manage to form a zextload after promoting and merging
184 ; OPTALL-LABEL: @promoteMergeExtArgZExt
185 ; OPTALL: [[LD:%[a-zA-Z_0-9-]+]] = load i8, ptr %p
187 ; STRESS-NEXT: [[ZEXTLD:%[a-zA-Z_0-9-]+]] = zext i8 [[LD]] to i32
188 ; STRESS-NEXT: [[ZEXTB:%[a-zA-Z_0-9-]+]] = zext i16 %b to i32
189 ; STRESS-NEXT: [[RES:%[a-zA-Z_0-9-]+]] = add nuw i32 [[ZEXTLD]], [[ZEXTB]]
191 ; NONSTRESS: [[ZEXTLD:%[a-zA-Z_0-9-]+]] = zext i8 [[LD]] to i16
192 ; NONSTRESS: [[ADD:%[a-zA-Z_0-9-]+]] = add nuw i16 [[ZEXTLD]], %b
193 ; NONSTRESS: [[RES:%[a-zA-Z_0-9-]+]] = zext i16 [[ADD]] to i32
195 ; DISABLE: [[ZEXTLD:%[a-zA-Z_0-9-]+]] = zext i8 [[LD]] to i16
196 ; DISABLE: [[ADD:%[a-zA-Z_0-9-]+]] = add nuw i16 [[ZEXTLD]], %b
197 ; DISABLE: [[RES:%[a-zA-Z_0-9-]+]] = zext i16 [[ADD]] to i32
199 ; OPTALL: store i32 [[RES]], ptr %q
201 define void @promoteMergeExtArgZExt(ptr %p, ptr %q, i16 %b) {
204 %ext = zext i8 %t to i16
205 %add = add nuw i16 %ext, %b
206 %a = icmp slt i8 %t, 20
207 br i1 %a, label %true, label %false
209 %s = zext i16 %add to i32
216 ; Check that we manage to form a sextload after promoting and merging
219 ; OPTALL-LABEL: @promoteMergeExtArgSExt
220 ; OPTALL: [[LD:%[a-zA-Z_0-9-]+]] = load i8, ptr %p
222 ; STRESS-NEXT: [[ZEXTLD:%[a-zA-Z_0-9-]+]] = zext i8 [[LD]] to i32
223 ; STRESS-NEXT: [[ZEXTB:%[a-zA-Z_0-9-]+]] = sext i16 %b to i32
224 ; STRESS-NEXT: [[RES:%[a-zA-Z_0-9-]+]] = add nsw i32 [[ZEXTLD]], [[ZEXTB]]
226 ; NONSTRESS: [[ZEXTLD:%[a-zA-Z_0-9-]+]] = zext i8 [[LD]] to i16
227 ; NONSTRESS: [[ADD:%[a-zA-Z_0-9-]+]] = add nsw i16 [[ZEXTLD]], %b
228 ; NONSTRESS: [[RES:%[a-zA-Z_0-9-]+]] = sext i16 [[ADD]] to i32
230 ; DISABLE: [[ZEXTLD:%[a-zA-Z_0-9-]+]] = zext i8 [[LD]] to i16
231 ; DISABLE: [[ADD:%[a-zA-Z_0-9-]+]] = add nsw i16 [[ZEXTLD]], %b
232 ; DISABLE: [[RES:%[a-zA-Z_0-9-]+]] = sext i16 [[ADD]] to i32
233 ; OPTALL: store i32 [[RES]], ptr %q
235 define void @promoteMergeExtArgSExt(ptr %p, ptr %q, i16 %b) {
238 %ext = zext i8 %t to i16
239 %add = add nsw i16 %ext, %b
240 %a = icmp slt i8 %t, 20
241 br i1 %a, label %true, label %false
243 %s = sext i16 %add to i32
250 ; Check that we manage to catch all the extload opportunities that are exposed
251 ; by the different iterations of codegen prepare.
252 ; Moreover, check that we do not promote more than we need to.
253 ; Here is what is happening in this test (not necessarly in this order):
254 ; 1. We try to promote the operand of %sextadd.
255 ; a. This creates one sext of %ld2 and one of %zextld
256 ; b. The sext of %ld2 can be combine with %ld2, so we remove one sext but
257 ; introduced one. This is fine with the current heuristic: neutral.
258 ; => We have one zext of %zextld left and we created one sext of %ld2.
259 ; 2. We try to promote the operand of %sextaddza.
260 ; a. This creates one sext of %zexta and one of %zextld
261 ; b. The sext of %zexta can be combined with the zext of %a.
262 ; c. The sext of %zextld leads to %ld and can be combined with it. This is
263 ; done by promoting %zextld. This is fine with the current heuristic:
265 ; => We have created a new zext of %ld and we created one sext of %zexta.
266 ; 3. We try to promote the operand of %sextaddb.
267 ; a. This creates one sext of %b and one of %zextld
268 ; b. The sext of %b is a dead-end, nothing to be done.
269 ; c. Same thing as 2.c. happens.
270 ; => We have created a new zext of %ld and we created one sext of %b.
271 ; 4. We try to promote the operand of the zext of %zextld introduced in #1.
272 ; a. Same thing as 2.c. happens.
273 ; b. %zextld does not have any other uses. It is dead coded.
274 ; => We have created a new zext of %ld and we removed a zext of %zextld and
276 ; Currently we do not try to reuse existing extensions, so in the end we have
277 ; 3 identical zext of %ld. The extensions will be CSE'ed by SDag.
279 ; OPTALL-LABEL: @severalPromotions
280 ; OPTALL: [[LD:%[a-zA-Z_0-9-]+]] = load i8, ptr %addr1
281 ; OPT-NEXT: [[ZEXTLD1_1:%[a-zA-Z_0-9-]+]] = zext i8 [[LD]] to i64
282 ; OPT-NEXT: [[ZEXTLD1_2:%[a-zA-Z_0-9-]+]] = zext i8 [[LD]] to i64
283 ; OPT-NEXT: [[LD2:%[a-zA-Z_0-9-]+]] = load i32, ptr %addr2
284 ; OPT-NEXT: [[SEXTLD2:%[a-zA-Z_0-9-]+]] = sext i32 [[LD2]] to i64
285 ; OPT-NEXT: [[ZEXTLD1_3:%[a-zA-Z_0-9-]+]] = zext i8 [[LD]] to i64
286 ; OPT-NEXT: [[RES:%[a-zA-Z_0-9-]+]] = add nsw i64 [[SEXTLD2]], [[ZEXTLD1_3]]
287 ; OPT-NEXT: [[ZEXTLD1_4:%[a-zA-Z_0-9-]+]] = zext i8 %a to i64
288 ; OPT-NEXT: [[RESZA:%[a-zA-Z_0-9-]+]] = add nsw i64 [[ZEXTLD1_4]], [[ZEXTLD1_2]]
289 ; OPT-NEXT: [[SEXTB:%[a-zA-Z_0-9-]+]] = sext i32 %b to i64
290 ; OPT-NEXT: [[RESB:%[a-zA-Z_0-9-]+]] = add nsw i64 [[SEXTB]], [[ZEXTLD1_1]]
292 ; DISABLE: [[ADD:%[a-zA-Z_0-9-]+]] = add nsw i32
293 ; DISABLE: [[RES:%[a-zA-Z_0-9-]+]] = sext i32 [[ADD]] to i64
294 ; DISABLE: [[ADDZA:%[a-zA-Z_0-9-]+]] = add nsw i32
295 ; DISABLE: [[RESZA:%[a-zA-Z_0-9-]+]] = sext i32 [[ADDZA]] to i64
296 ; DISABLE: [[ADDB:%[a-zA-Z_0-9-]+]] = add nsw i32
297 ; DISABLE: [[RESB:%[a-zA-Z_0-9-]+]] = sext i32 [[ADDB]] to i64
299 ; OPTALL: call void @dummy(i64 [[RES]], i64 [[RESZA]], i64 [[RESB]])
301 define void @severalPromotions(ptr %addr1, ptr %addr2, i8 %a, i32 %b) {
302 %ld = load i8, ptr %addr1
303 %zextld = zext i8 %ld to i32
304 %ld2 = load i32, ptr %addr2
305 %add = add nsw i32 %ld2, %zextld
306 %sextadd = sext i32 %add to i64
307 %zexta = zext i8 %a to i32
308 %addza = add nsw i32 %zexta, %zextld
309 %sextaddza = sext i32 %addza to i64
310 %addb = add nsw i32 %b, %zextld
311 %sextaddb = sext i32 %addb to i64
312 call void @dummy(i64 %sextadd, i64 %sextaddza, i64 %sextaddb)
316 declare void @dummy(i64, i64, i64)
318 ; Make sure we do not try to promote vector types since the type promotion
319 ; helper does not support them for now.
320 ; OPTALL-LABEL: @vectorPromotion
321 ; OPTALL: [[SHL:%[a-zA-Z_0-9-]+]] = shl nuw nsw <2 x i32> zeroinitializer, <i32 8, i32 8>
322 ; OPTALL: [[ZEXT:%[a-zA-Z_0-9-]+]] = zext <2 x i32> [[SHL]] to <2 x i64>
324 define void @vectorPromotion() {
326 %a = shl nuw nsw <2 x i32> zeroinitializer, <i32 8, i32 8>
327 %b = zext <2 x i32> %a to <2 x i64>
331 @a = common global i32 0, align 4
332 @c = common global [2 x i32] zeroinitializer, align 4
334 ; Make sure we support promotion of operands that produces a Value as opposed
336 ; This used to cause a crash.
337 ; OPTALL-LABEL: @promotionOfArgEndsUpInValue
338 ; OPTALL: [[LD:%[a-zA-Z_0-9-]+]] = load i16, ptr %addr
340 ; OPT-NEXT: [[SEXT:%[a-zA-Z_0-9-]+]] = sext i16 [[LD]] to i32
341 ; OPT-NEXT: [[SEXT2:%[a-zA-Z_0-9-]+]] = zext i1 icmp ne (ptr getelementptr inbounds ([2 x i32], ptr @c, i64 0, i64 1), ptr @a) to i32
342 ; OPT-NEXT: [[RES:%[a-zA-Z_0-9-]+]] = add nuw nsw i32 [[SEXT]], [[SEXT2]]
344 ; DISABLE-NEXT: [[EXT:%[a-zA-Z_0-9-]+]] = zext i1 icmp ne (ptr getelementptr inbounds ([2 x i32], ptr @c, i64 0, i64 1), ptr @a) to i16
345 ; DISABLE-NEXT: [[ADD:%[a-zA-Z_0-9-]+]] = add nuw nsw i16 [[LD]], [[EXT]]
346 ; DISABLE-NEXT: [[RES:%[a-zA-Z_0-9-]+]] = sext i16 [[ADD]] to i32
348 ; OPTALL-NEXT: ret i32 [[RES]]
349 define i32 @promotionOfArgEndsUpInValue(ptr %addr) {
351 %val = load i16, ptr %addr
352 %ext = zext i1 icmp ne (ptr getelementptr inbounds ([2 x i32], ptr @c, i64 0, i64 1), ptr @a) to i16
353 %add = add nuw nsw i16 %val, %ext
354 %conv3 = sext i16 %add to i32
358 ; Check that we see that one zext can be derived from the other for free.
359 ; OPTALL-LABEL: @promoteTwoArgZextWithSourceExtendedTwice
360 ; OPTALL: [[LD:%[a-zA-Z_0-9-]+]] = load i8, ptr %p
362 ; OPT-NEXT: [[ZEXT64:%[a-zA-Z_0-9-]+]] = zext i8 [[LD]] to i64
363 ; OPT-NEXT: [[ZEXT32:%[a-zA-Z_0-9-]+]] = zext i8 [[LD]] to i32
364 ; OPT-NEXT: [[RES32:%[a-zA-Z_0-9-]+]] = add nuw i32 [[ZEXT32]], %b
365 ; OPT-NEXT: [[RES64:%[a-zA-Z_0-9-]+]] = add nuw i64 [[ZEXT64]], 12
366 ; OPT-NEXT: store i32 [[RES32]], ptr %addr
367 ; OPT-NEXT: store i64 [[RES64]], ptr %q
369 ; DISABLE-NEXT: [[ZEXT32:%[a-zA-Z_0-9-]+]] = zext i8 [[LD]] to i32
370 ; DISABLE-NEXT: [[RES32:%[a-zA-Z_0-9-]+]] = add nuw i32 [[ZEXT32]], %b
371 ; DISABLE-NEXT: [[RES2_32:%[a-zA-Z_0-9-]+]] = add nuw i32 [[ZEXT32]], 12
372 ; DISABLE-NEXT: store i32 [[RES32]], ptr %addr
373 ; DISABLE-NEXT: [[ZEXT64:%[a-zA-Z_0-9-]+]] = zext i32 [[RES2_32]] to i64
374 ; DISABLE-NEXT: store i64 [[ZEXT64]], ptr %q
376 ; OPTALL-NEXT: ret void
377 define void @promoteTwoArgZextWithSourceExtendedTwice(ptr %p, ptr %q, i32 %b, ptr %addr) {
380 %zextt = zext i8 %t to i32
381 %add = add nuw i32 %zextt, %b
382 %add2 = add nuw i32 %zextt, 12
383 store i32 %add, ptr %addr
384 %s = zext i32 %add2 to i64
389 ; Check that we do not increase the cost of the code.
390 ; The input has one free zext and one free sext. If we would have promoted
391 ; all the way through the load we would end up with a free zext and a
392 ; non-free sext (of %b).
393 ; OPTALL-LABEL: @doNotPromoteFreeSExtFromAddrMode
394 ; OPTALL: [[LD:%[a-zA-Z_0-9-]+]] = load i8, ptr %p
396 ; STRESS-NEXT: [[ZEXT64:%[a-zA-Z_0-9-]+]] = zext i8 [[LD]] to i64
397 ; STRESS-NEXT: [[SEXTB:%[a-zA-Z_0-9-]+]] = sext i32 %b to i64
398 ; STRESS-NEXT: [[IDX64:%[a-zA-Z_0-9-]+]] = add nsw i64 [[ZEXT64]], [[SEXTB]]
399 ; STRESS-NEXT: [[RES32:%[a-zA-Z_0-9-]+]] = trunc i64 [[IDX64]] to i32
401 ; NONSTRESS-NEXT: [[ZEXT32:%[a-zA-Z_0-9-]+]] = zext i8 [[LD]] to i32
402 ; NONSTRESS-NEXT: [[RES32:%[a-zA-Z_0-9-]+]] = add nsw i32 [[ZEXT32]], %b
403 ; NONSTRESS-NEXT: [[IDX64:%[a-zA-Z_0-9-]+]] = sext i32 [[RES32]] to i64
405 ; DISABLE-NEXT: [[ZEXT32:%[a-zA-Z_0-9-]+]] = zext i8 [[LD]] to i32
406 ; DISABLE-NEXT: [[RES32:%[a-zA-Z_0-9-]+]] = add nsw i32 [[ZEXT32]], %b
407 ; DISABLE-NEXT: [[IDX64:%[a-zA-Z_0-9-]+]] = sext i32 [[RES32]] to i64
409 ; OPTALL-NEXT: [[GEP:%[a-zA-Z_0-9-]+]] = getelementptr inbounds i32, ptr %addr, i64 [[IDX64]]
410 ; OPTALL-NEXT: store i32 [[RES32]], ptr [[GEP]]
411 ; OPTALL-NEXT: ret void
412 define void @doNotPromoteFreeSExtFromAddrMode(ptr %p, i32 %b, ptr %addr) {
415 %zextt = zext i8 %t to i32
416 %add = add nsw i32 %zextt, %b
417 %idx64 = sext i32 %add to i64
418 %staddr = getelementptr inbounds i32, ptr %addr, i64 %idx64
419 store i32 %add, ptr %staddr
423 ; Check that we do not increase the cost of the code.
424 ; The input has one free zext and one free sext. If we would have promoted
425 ; all the way through the load we would end up with a free zext and a
426 ; non-free sext (of %b).
427 ; OPTALL-LABEL: @doNotPromoteFreeSExtFromAddrMode64
428 ; OPTALL: [[LD:%[a-zA-Z_0-9-]+]] = load i8, ptr %p
430 ; STRESS-NEXT: [[ZEXT64:%[a-zA-Z_0-9-]+]] = zext i8 [[LD]] to i64
431 ; STRESS-NEXT: [[SEXTB:%[a-zA-Z_0-9-]+]] = sext i32 %b to i64
432 ; STRESS-NEXT: [[IDX64:%[a-zA-Z_0-9-]+]] = add nsw i64 [[ZEXT64]], [[SEXTB]]
434 ; NONSTRESS-NEXT: [[ZEXT32:%[a-zA-Z_0-9-]+]] = zext i8 [[LD]] to i32
435 ; NONSTRESS-NEXT: [[RES32:%[a-zA-Z_0-9-]+]] = add nsw i32 [[ZEXT32]], %b
436 ; NONSTRESS-NEXT: [[IDX64:%[a-zA-Z_0-9-]+]] = sext i32 [[RES32]] to i64
438 ; DISABLE-NEXT: [[ZEXT32:%[a-zA-Z_0-9-]+]] = zext i8 [[LD]] to i32
439 ; DISABLE-NEXT: [[RES32:%[a-zA-Z_0-9-]+]] = add nsw i32 [[ZEXT32]], %b
440 ; DISABLE-NEXT: [[IDX64:%[a-zA-Z_0-9-]+]] = sext i32 [[RES32]] to i64
442 ; OPTALL-NEXT: [[GEP:%[a-zA-Z_0-9-]+]] = getelementptr inbounds i64, ptr %addr, i64 [[IDX64]]
443 ; OPTALL-NEXT: store i64 %stuff, ptr [[GEP]]
444 ; OPTALL-NEXT: ret void
445 define void @doNotPromoteFreeSExtFromAddrMode64(ptr %p, i32 %b, ptr %addr, i64 %stuff) {
448 %zextt = zext i8 %t to i32
449 %add = add nsw i32 %zextt, %b
450 %idx64 = sext i32 %add to i64
451 %staddr = getelementptr inbounds i64, ptr %addr, i64 %idx64
452 store i64 %stuff, ptr %staddr
456 ; Check that we do not increase the cost of the code.
457 ; The input has one free zext and one free sext. If we would have promoted
458 ; all the way through the load we would end up with a free zext and a
459 ; non-free sext (of %b).
460 ; OPTALL-LABEL: @doNotPromoteFreeSExtFromAddrMode128
461 ; OPTALL: [[LD:%[a-zA-Z_0-9-]+]] = load i8, ptr %p
463 ; STRESS-NEXT: [[ZEXT64:%[a-zA-Z_0-9-]+]] = zext i8 [[LD]] to i64
464 ; STRESS-NEXT: [[SEXTB:%[a-zA-Z_0-9-]+]] = sext i32 %b to i64
465 ; STRESS-NEXT: [[IDX64:%[a-zA-Z_0-9-]+]] = add nsw i64 [[ZEXT64]], [[SEXTB]]
467 ; NONSTRESS-NEXT: [[ZEXT32:%[a-zA-Z_0-9-]+]] = zext i8 [[LD]] to i32
468 ; NONSTRESS-NEXT: [[RES32:%[a-zA-Z_0-9-]+]] = add nsw i32 [[ZEXT32]], %b
469 ; NONSTRESS-NEXT: [[IDX64:%[a-zA-Z_0-9-]+]] = sext i32 [[RES32]] to i64
471 ; DISABLE-NEXT: [[ZEXT32:%[a-zA-Z_0-9-]+]] = zext i8 [[LD]] to i32
472 ; DISABLE-NEXT: [[RES32:%[a-zA-Z_0-9-]+]] = add nsw i32 [[ZEXT32]], %b
473 ; DISABLE-NEXT: [[IDX64:%[a-zA-Z_0-9-]+]] = sext i32 [[RES32]] to i64
475 ; OPTALL-NEXT: [[GEP:%[a-zA-Z_0-9-]+]] = getelementptr inbounds i128, ptr %addr, i64 [[IDX64]]
476 ; OPTALL-NEXT: store i128 %stuff, ptr [[GEP]]
477 ; OPTALL-NEXT: ret void
478 define void @doNotPromoteFreeSExtFromAddrMode128(ptr %p, i32 %b, ptr %addr, i128 %stuff) {
481 %zextt = zext i8 %t to i32
482 %add = add nsw i32 %zextt, %b
483 %idx64 = sext i32 %add to i64
484 %staddr = getelementptr inbounds i128, ptr %addr, i64 %idx64
485 store i128 %stuff, ptr %staddr
490 ; Check that we do not increase the cost of the code.
491 ; The input has one free zext and one free sext. If we would have promoted
492 ; all the way through the load we would end up with a free zext and a
493 ; non-free sext (of %b).
494 ; OPTALL-LABEL: @promoteSExtFromAddrMode256
495 ; OPTALL: [[LD:%[a-zA-Z_0-9-]+]] = load i8, ptr %p
497 ; OPT-NEXT: [[ZEXT64:%[a-zA-Z_0-9-]+]] = zext i8 [[LD]] to i64
498 ; OPT-NEXT: [[SEXTB:%[a-zA-Z_0-9-]+]] = sext i32 %b to i64
499 ; OPT-NEXT: [[IDX64:%[a-zA-Z_0-9-]+]] = add nsw i64 [[ZEXT64]], [[SEXTB]]
501 ; DISABLE-NEXT: [[ZEXT32:%[a-zA-Z_0-9-]+]] = zext i8 [[LD]] to i32
502 ; DISABLE-NEXT: [[RES32:%[a-zA-Z_0-9-]+]] = add nsw i32 [[ZEXT32]], %b
503 ; DISABLE-NEXT: [[IDX64:%[a-zA-Z_0-9-]+]] = sext i32 [[RES32]] to i64
505 ; OPTALL-NEXT: [[GEP:%[a-zA-Z_0-9-]+]] = getelementptr inbounds i256, ptr %addr, i64 [[IDX64]]
506 ; OPTALL-NEXT: store i256 %stuff, ptr [[GEP]]
507 ; OPTALL-NEXT: ret void
508 define void @promoteSExtFromAddrMode256(ptr %p, i32 %b, ptr %addr, i256 %stuff) {
511 %zextt = zext i8 %t to i32
512 %add = add nsw i32 %zextt, %b
513 %idx64 = sext i32 %add to i64
514 %staddr = getelementptr inbounds i256, ptr %addr, i64 %idx64
515 store i256 %stuff, ptr %staddr
519 ; Check that we do not increase the cost of the code.
520 ; The input has one free zext and one free zext.
521 ; When we promote all the way through the load, we end up with
522 ; a free zext and a non-free zext (of %b).
523 ; However, the current target lowering says zext i32 to i64 is free
524 ; so the promotion happens because the cost did not change and may
525 ; expose more opportunities.
526 ; This would need to be fixed at some point.
527 ; OPTALL-LABEL: @doNotPromoteFreeZExtFromAddrMode
528 ; OPTALL: [[LD:%[a-zA-Z_0-9-]+]] = load i8, ptr %p
530 ; This transformation should really happen only for stress mode.
531 ; STRESS-NEXT: [[ZEXT64:%[a-zA-Z_0-9-]+]] = zext i8 [[LD]] to i64
532 ; STRESS-NEXT: [[ZEXTB:%[a-zA-Z_0-9-]+]] = zext i32 %b to i64
533 ; STRESS-NEXT: [[IDX64:%[a-zA-Z_0-9-]+]] = add nuw i64 [[ZEXT64]], [[ZEXTB]]
534 ; STRESS-NEXT: [[RES32:%[a-zA-Z_0-9-]+]] = trunc i64 [[IDX64]] to i32
536 ; NONSTRESS-NEXT: [[ZEXT32:%[a-zA-Z_0-9-]+]] = zext i8 [[LD]] to i32
537 ; NONSTRESS-NEXT: [[RES32:%[a-zA-Z_0-9-]+]] = add nuw i32 [[ZEXT32]], %b
538 ; NONSTRESS-NEXT: [[IDX64:%[a-zA-Z_0-9-]+]] = zext i32 [[RES32]] to i64
540 ; DISABLE-NEXT: [[ZEXT32:%[a-zA-Z_0-9-]+]] = zext i8 [[LD]] to i32
541 ; DISABLE-NEXT: [[RES32:%[a-zA-Z_0-9-]+]] = add nuw i32 [[ZEXT32]], %b
542 ; DISABLE-NEXT: [[IDX64:%[a-zA-Z_0-9-]+]] = zext i32 [[RES32]] to i64
544 ; OPTALL-NEXT: [[GEP:%[a-zA-Z_0-9-]+]] = getelementptr inbounds i32, ptr %addr, i64 [[IDX64]]
545 ; OPTALL-NEXT: store i32 [[RES32]], ptr [[GEP]]
546 ; OPTALL-NEXT: ret void
547 define void @doNotPromoteFreeZExtFromAddrMode(ptr %p, i32 %b, ptr %addr) {
550 %zextt = zext i8 %t to i32
551 %add = add nuw i32 %zextt, %b
552 %idx64 = zext i32 %add to i64
553 %staddr = getelementptr inbounds i32, ptr %addr, i64 %idx64
554 store i32 %add, ptr %staddr
558 ; OPTALL-LABEL: @doNotPromoteFreeSExtFromShift
559 ; OPTALL: [[LD:%[a-zA-Z_0-9-]+]] = load i8, ptr %p
561 ; STRESS-NEXT: [[ZEXT64:%[a-zA-Z_0-9-]+]] = zext i8 [[LD]] to i64
562 ; STRESS-NEXT: [[SEXTB:%[a-zA-Z_0-9-]+]] = sext i32 %b to i64
563 ; STRESS-NEXT: [[IDX64:%[a-zA-Z_0-9-]+]] = add nsw i64 [[ZEXT64]], [[SEXTB]]
565 ; NONSTRESS-NEXT: [[ZEXT32:%[a-zA-Z_0-9-]+]] = zext i8 [[LD]] to i32
566 ; NONSTRESS-NEXT: [[RES32:%[a-zA-Z_0-9-]+]] = add nsw i32 [[ZEXT32]], %b
567 ; NONSTRESS-NEXT: [[IDX64:%[a-zA-Z_0-9-]+]] = sext i32 [[RES32]] to i64
569 ; DISABLE-NEXT: [[ZEXT32:%[a-zA-Z_0-9-]+]] = zext i8 [[LD]] to i32
570 ; DISABLE-NEXT: [[RES32:%[a-zA-Z_0-9-]+]] = add nsw i32 [[ZEXT32]], %b
571 ; DISABLE-NEXT: [[IDX64:%[a-zA-Z_0-9-]+]] = sext i32 [[RES32]] to i64
573 ; OPTALL-NEXT: [[RES64:%[a-zA-Z_0-9-]+]] = shl i64 [[IDX64]], 12
574 ; OPTALL-NEXT: ret i64 %staddr
575 define i64 @doNotPromoteFreeSExtFromShift(ptr %p, i32 %b) {
578 %zextt = zext i8 %t to i32
579 %add = add nsw i32 %zextt, %b
580 %idx64 = sext i32 %add to i64
581 %staddr = shl i64 %idx64, 12
585 ; Same comment as doNotPromoteFreeZExtFromAddrMode.
586 ; OPTALL-LABEL: @doNotPromoteFreeZExtFromShift
587 ; OPTALL: [[LD:%[a-zA-Z_0-9-]+]] = load i8, ptr %p
589 ; This transformation should really happen only for stress mode.
590 ; STRESS-NEXT: [[ZEXT64:%[a-zA-Z_0-9-]+]] = zext i8 [[LD]] to i64
591 ; STRESS-NEXT: [[ZEXTB:%[a-zA-Z_0-9-]+]] = zext i32 %b to i64
592 ; STRESS-NEXT: [[IDX64:%[a-zA-Z_0-9-]+]] = add nuw i64 [[ZEXT64]], [[ZEXTB]]
594 ; NONSTRESS-NEXT: [[ZEXT32:%[a-zA-Z_0-9-]+]] = zext i8 [[LD]] to i32
595 ; NONSTRESS-NEXT: [[RES32:%[a-zA-Z_0-9-]+]] = add nuw i32 [[ZEXT32]], %b
596 ; NONSTRESS-NEXT: [[IDX64:%[a-zA-Z_0-9-]+]] = zext i32 [[RES32]] to i64
598 ; DISABLE-NEXT: [[ZEXT32:%[a-zA-Z_0-9-]+]] = zext i8 [[LD]] to i32
599 ; DISABLE-NEXT: [[RES32:%[a-zA-Z_0-9-]+]] = add nuw i32 [[ZEXT32]], %b
600 ; DISABLE-NEXT: [[IDX64:%[a-zA-Z_0-9-]+]] = zext i32 [[RES32]] to i64
602 ; OPTALL-NEXT: [[RES64:%[a-zA-Z_0-9-]+]] = shl i64 [[IDX64]], 12
603 ; OPTALL-NEXT: ret i64 %staddr
604 define i64 @doNotPromoteFreeZExtFromShift(ptr %p, i32 %b) {
607 %zextt = zext i8 %t to i32
608 %add = add nuw i32 %zextt, %b
609 %idx64 = zext i32 %add to i64
610 %staddr = shl i64 %idx64, 12
614 ; The input has one free zext and one non-free sext.
615 ; When we promote all the way through to the load, we end up with
616 ; a free zext, a free sext (%ld1), and a non-free sext (of %cst).
617 ; However, we when generate load pair and the free sext(%ld1) becomes
618 ; non-free. So technically, we trade a non-free sext to two non-free
620 ; This would need to be fixed at some point.
621 ; OPTALL-LABEL: @doNotPromoteBecauseOfPairedLoad
622 ; OPTALL: [[LD0:%[a-zA-Z_0-9-]+]] = load i32, ptr %p
623 ; OPTALL: [[GEP:%[a-zA-Z_0-9-]+]] = getelementptr inbounds i32, ptr %p, i64 1
624 ; OPTALL: [[LD1:%[a-zA-Z_0-9-]+]] = load i32, ptr [[GEP]]
626 ; This transformation should really happen only for stress mode.
627 ; OPT-NEXT: [[SEXTLD1:%[a-zA-Z_0-9-]+]] = sext i32 [[LD1]] to i64
628 ; OPT-NEXT: [[SEXTCST:%[a-zA-Z_0-9-]+]] = sext i32 %cst to i64
629 ; OPT-NEXT: [[SEXTRES:%[a-zA-Z_0-9-]+]] = add nsw i64 [[SEXTLD1]], [[SEXTCST]]
631 ; DISABLE-NEXT: [[RES:%[a-zA-Z_0-9-]+]] = add nsw i32 [[LD1]], %cst
632 ; DISABLE-NEXT: [[SEXTRES:%[a-zA-Z_0-9-]+]] = sext i32 [[RES]] to i64
634 ; OPTALL-NEXT: [[ZEXTLD0:%[a-zA-Z_0-9-]+]] = zext i32 [[LD0]] to i64
635 ; OPTALL-NEXT: [[FINAL:%[a-zA-Z_0-9-]+]] = add i64 [[SEXTRES]], [[ZEXTLD0]]
636 ; OPTALL-NEXT: ret i64 [[FINAL]]
637 define i64 @doNotPromoteBecauseOfPairedLoad(ptr %p, i32 %cst) {
638 %ld0 = load i32, ptr %p
639 %idxLd1 = getelementptr inbounds i32, ptr %p, i64 1
640 %ld1 = load i32, ptr %idxLd1
641 %res = add nsw i32 %ld1, %cst
642 %sextres = sext i32 %res to i64
643 %zextLd0 = zext i32 %ld0 to i64
644 %final = add i64 %sextres, %zextLd0
648 define i64 @promoteZextShl(i1 %c, ptr %P) {
650 ; OPTALL-LABEL: promoteZextShl
652 ; OPT: %[[LD:.*]] = load i16, ptr %P
653 ; OPT: %[[EXT:.*]] = zext i16 %[[LD]] to i64
655 ; OPT: shl nsw i64 %[[EXT]], 1
657 ; DISABLE: %r = sext i32 %shl2 to i64
658 %ld = load i16, ptr %P
659 br i1 %c, label %end, label %if.then
661 %z = zext i16 %ld to i32
662 %shl2 = shl nsw i32 %z, 1
663 %r = sext i32 %shl2 to i64