Skip to content

Commit

Permalink
[InstCombine] add tests for ext-of-bool + add/sub; NFC
Browse files Browse the repository at this point in the history
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
  • Loading branch information
rotateright committed Jan 29, 2019
1 parent 5d71fc5 commit f044d18
Showing 1 changed file with 87 additions and 0 deletions.
87 changes: 87 additions & 0 deletions llvm/test/Transforms/InstCombine/zext-bool-add-sub.ll
Original file line number Diff line number Diff line change
Expand Up @@ -315,3 +315,90 @@ define i8 @sext_sub_nuw(i8 %x, i1 %y) {
ret i8 %sub
}

define i32 @sextbool_add(i1 %c, i32 %x) {
; CHECK-LABEL: @sextbool_add(
; CHECK-NEXT: [[B:%.*]] = sext i1 [[C:%.*]] to i32
; CHECK-NEXT: [[S:%.*]] = add i32 [[B]], [[X:%.*]]
; CHECK-NEXT: ret i32 [[S]]
;
%b = sext i1 %c to i32
%s = add i32 %b, %x
ret i32 %s
}

define i32 @sextbool_add_commute(i1 %c, i32 %px) {
; CHECK-LABEL: @sextbool_add_commute(
; CHECK-NEXT: [[X:%.*]] = urem i32 [[PX:%.*]], 42
; CHECK-NEXT: [[B:%.*]] = sext i1 [[C:%.*]] to i32
; CHECK-NEXT: [[S:%.*]] = add nsw i32 [[X]], [[B]]
; CHECK-NEXT: ret i32 [[S]]
;
%x = urem i32 %px, 42 ; thwart complexity-based canonicalization
%b = sext i1 %c to i32
%s = add i32 %x, %b
ret i32 %s
}

; Negative test - extra use prevents canonicalization.

declare void @use32(i32)

define i32 @sextbool_add_uses(i1 %c, i32 %x) {
; CHECK-LABEL: @sextbool_add_uses(
; CHECK-NEXT: [[B:%.*]] = sext i1 [[C:%.*]] to i32
; CHECK-NEXT: call void @use32(i32 [[B]])
; CHECK-NEXT: [[S:%.*]] = add i32 [[B]], [[X:%.*]]
; CHECK-NEXT: ret i32 [[S]]
;
%b = sext i1 %c to i32
call void @use32(i32 %b)
%s = add i32 %b, %x
ret i32 %s
}

define <4 x i32> @sextbool_add_vector(<4 x i1> %c, <4 x i32> %x) {
; CHECK-LABEL: @sextbool_add_vector(
; CHECK-NEXT: [[B:%.*]] = sext <4 x i1> [[C:%.*]] to <4 x i32>
; CHECK-NEXT: [[S:%.*]] = add <4 x i32> [[B]], [[X:%.*]]
; CHECK-NEXT: ret <4 x i32> [[S]]
;
%b = sext <4 x i1> %c to <4 x i32>
%s = add <4 x i32> %x, %b
ret <4 x i32> %s
}

define i32 @zextbool_sub(i1 %c, i32 %x) {
; CHECK-LABEL: @zextbool_sub(
; CHECK-NEXT: [[B:%.*]] = zext i1 [[C:%.*]] to i32
; CHECK-NEXT: [[S:%.*]] = sub i32 [[B]], [[X:%.*]]
; CHECK-NEXT: ret i32 [[S]]
;
%b = zext i1 %c to i32
%s = sub i32 %b, %x
ret i32 %s
}

define i32 @zextbool_sub_uses(i1 %c, i32 %x) {
; CHECK-LABEL: @zextbool_sub_uses(
; CHECK-NEXT: [[B:%.*]] = zext i1 [[C:%.*]] to i32
; CHECK-NEXT: call void @use32(i32 [[B]])
; CHECK-NEXT: [[S:%.*]] = sub i32 [[X:%.*]], [[B]]
; CHECK-NEXT: ret i32 [[S]]
;
%b = zext i1 %c to i32
call void @use32(i32 %b)
%s = sub i32 %x, %b
ret i32 %s
}

define <4 x i32> @zextbool_sub_vector(<4 x i1> %c, <4 x i32> %x) {
; CHECK-LABEL: @zextbool_sub_vector(
; CHECK-NEXT: [[B:%.*]] = zext <4 x i1> [[C:%.*]] to <4 x i32>
; CHECK-NEXT: [[S:%.*]] = sub <4 x i32> [[X:%.*]], [[B]]
; CHECK-NEXT: ret <4 x i32> [[S]]
;
%b = zext <4 x i1> %c to <4 x i32>
%s = sub <4 x i32> %x, %b
ret <4 x i32> %s
}

0 comments on commit f044d18

Please sign in to comment.