Skip to content
This repository was archived by the owner on Jan 26, 2024. It is now read-only.

Commit f044d18

Browse files
committed
[InstCombine] add tests for ext-of-bool + add/sub; NFC
We should choose one of these as canonical: %z = zext i1 %cmp to i32 %r = sub i32 %x, %z => %s = sext i1 %cmp to i32 %r = add i32 %x, %s The test comments assume that the zext form is better, but we can adjust that if we decide to go the other way. llvm-svn: 352515
1 parent 5d71fc5 commit f044d18

File tree

1 file changed

+87
-0
lines changed

1 file changed

+87
-0
lines changed

llvm/test/Transforms/InstCombine/zext-bool-add-sub.ll

+87
Original file line numberDiff line numberDiff line change
@@ -315,3 +315,90 @@ define i8 @sext_sub_nuw(i8 %x, i1 %y) {
315315
ret i8 %sub
316316
}
317317

318+
define i32 @sextbool_add(i1 %c, i32 %x) {
319+
; CHECK-LABEL: @sextbool_add(
320+
; CHECK-NEXT: [[B:%.*]] = sext i1 [[C:%.*]] to i32
321+
; CHECK-NEXT: [[S:%.*]] = add i32 [[B]], [[X:%.*]]
322+
; CHECK-NEXT: ret i32 [[S]]
323+
;
324+
%b = sext i1 %c to i32
325+
%s = add i32 %b, %x
326+
ret i32 %s
327+
}
328+
329+
define i32 @sextbool_add_commute(i1 %c, i32 %px) {
330+
; CHECK-LABEL: @sextbool_add_commute(
331+
; CHECK-NEXT: [[X:%.*]] = urem i32 [[PX:%.*]], 42
332+
; CHECK-NEXT: [[B:%.*]] = sext i1 [[C:%.*]] to i32
333+
; CHECK-NEXT: [[S:%.*]] = add nsw i32 [[X]], [[B]]
334+
; CHECK-NEXT: ret i32 [[S]]
335+
;
336+
%x = urem i32 %px, 42 ; thwart complexity-based canonicalization
337+
%b = sext i1 %c to i32
338+
%s = add i32 %x, %b
339+
ret i32 %s
340+
}
341+
342+
; Negative test - extra use prevents canonicalization.
343+
344+
declare void @use32(i32)
345+
346+
define i32 @sextbool_add_uses(i1 %c, i32 %x) {
347+
; CHECK-LABEL: @sextbool_add_uses(
348+
; CHECK-NEXT: [[B:%.*]] = sext i1 [[C:%.*]] to i32
349+
; CHECK-NEXT: call void @use32(i32 [[B]])
350+
; CHECK-NEXT: [[S:%.*]] = add i32 [[B]], [[X:%.*]]
351+
; CHECK-NEXT: ret i32 [[S]]
352+
;
353+
%b = sext i1 %c to i32
354+
call void @use32(i32 %b)
355+
%s = add i32 %b, %x
356+
ret i32 %s
357+
}
358+
359+
define <4 x i32> @sextbool_add_vector(<4 x i1> %c, <4 x i32> %x) {
360+
; CHECK-LABEL: @sextbool_add_vector(
361+
; CHECK-NEXT: [[B:%.*]] = sext <4 x i1> [[C:%.*]] to <4 x i32>
362+
; CHECK-NEXT: [[S:%.*]] = add <4 x i32> [[B]], [[X:%.*]]
363+
; CHECK-NEXT: ret <4 x i32> [[S]]
364+
;
365+
%b = sext <4 x i1> %c to <4 x i32>
366+
%s = add <4 x i32> %x, %b
367+
ret <4 x i32> %s
368+
}
369+
370+
define i32 @zextbool_sub(i1 %c, i32 %x) {
371+
; CHECK-LABEL: @zextbool_sub(
372+
; CHECK-NEXT: [[B:%.*]] = zext i1 [[C:%.*]] to i32
373+
; CHECK-NEXT: [[S:%.*]] = sub i32 [[B]], [[X:%.*]]
374+
; CHECK-NEXT: ret i32 [[S]]
375+
;
376+
%b = zext i1 %c to i32
377+
%s = sub i32 %b, %x
378+
ret i32 %s
379+
}
380+
381+
define i32 @zextbool_sub_uses(i1 %c, i32 %x) {
382+
; CHECK-LABEL: @zextbool_sub_uses(
383+
; CHECK-NEXT: [[B:%.*]] = zext i1 [[C:%.*]] to i32
384+
; CHECK-NEXT: call void @use32(i32 [[B]])
385+
; CHECK-NEXT: [[S:%.*]] = sub i32 [[X:%.*]], [[B]]
386+
; CHECK-NEXT: ret i32 [[S]]
387+
;
388+
%b = zext i1 %c to i32
389+
call void @use32(i32 %b)
390+
%s = sub i32 %x, %b
391+
ret i32 %s
392+
}
393+
394+
define <4 x i32> @zextbool_sub_vector(<4 x i1> %c, <4 x i32> %x) {
395+
; CHECK-LABEL: @zextbool_sub_vector(
396+
; CHECK-NEXT: [[B:%.*]] = zext <4 x i1> [[C:%.*]] to <4 x i32>
397+
; CHECK-NEXT: [[S:%.*]] = sub <4 x i32> [[X:%.*]], [[B]]
398+
; CHECK-NEXT: ret <4 x i32> [[S]]
399+
;
400+
%b = zext <4 x i1> %c to <4 x i32>
401+
%s = sub <4 x i32> %x, %b
402+
ret <4 x i32> %s
403+
}
404+

0 commit comments

Comments
 (0)