Skip to content

Commit

Permalink
[InstCombine] add tests for X == 0 ? 0 : X * Y ; NFC
Browse files Browse the repository at this point in the history
These are the tests for D108408 with current baseline results.
  • Loading branch information
fzhinkin authored and rotateright committed Sep 10, 2021
1 parent 2c5590a commit 745f82b
Showing 1 changed file with 150 additions and 0 deletions.
150 changes: 150 additions & 0 deletions llvm/test/Transforms/InstCombine/select.ll
Expand Up @@ -2844,6 +2844,156 @@ define <2 x i1> @partial_false_undef_condval(<2 x i1> %x) {
ret <2 x i1> %r
}

define i32 @mul_select_eq_zero(i32 %x, i32 %y) {
; CHECK-LABEL: @mul_select_eq_zero(
; CHECK-NEXT: [[C:%.*]] = icmp eq i32 [[X:%.*]], 0
; CHECK-NEXT: [[M:%.*]] = mul i32 [[X]], [[Y:%.*]]
; CHECK-NEXT: [[R:%.*]] = select i1 [[C]], i32 0, i32 [[M]]
; CHECK-NEXT: ret i32 [[R]]
;
%c = icmp eq i32 %x, 0
%m = mul i32 %x, %y
%r = select i1 %c, i32 0, i32 %m
ret i32 %r
}

define i32 @mul_select_eq_zero_commute(i32 %x, i32 %y) {
; CHECK-LABEL: @mul_select_eq_zero_commute(
; CHECK-NEXT: [[C:%.*]] = icmp eq i32 [[Y:%.*]], 0
; CHECK-NEXT: [[M:%.*]] = mul i32 [[X:%.*]], [[Y]]
; CHECK-NEXT: [[R:%.*]] = select i1 [[C]], i32 0, i32 [[M]]
; CHECK-NEXT: ret i32 [[R]]
;
%c = icmp eq i32 %y, 0
%m = mul i32 %x, %y
%r = select i1 %c, i32 0, i32 %m
ret i32 %r
}

define i32 @mul_select_eq_zero_copy_flags(i32 %x, i32 %y) {
; CHECK-LABEL: @mul_select_eq_zero_copy_flags(
; CHECK-NEXT: [[C:%.*]] = icmp eq i32 [[X:%.*]], 0
; CHECK-NEXT: [[M:%.*]] = mul nuw nsw i32 [[X]], [[Y:%.*]]
; CHECK-NEXT: [[R:%.*]] = select i1 [[C]], i32 0, i32 [[M]]
; CHECK-NEXT: ret i32 [[R]]
;
%c = icmp eq i32 %x, 0
%m = mul nuw nsw i32 %x, %y
%r = select i1 %c, i32 0, i32 %m
ret i32 %r
}

define i32 @mul_select_ne_zero(i32 %x, i32 %y) {
; CHECK-LABEL: @mul_select_ne_zero(
; CHECK-NEXT: [[C_NOT:%.*]] = icmp eq i32 [[X:%.*]], 0
; CHECK-NEXT: [[M:%.*]] = mul i32 [[X]], [[Y:%.*]]
; CHECK-NEXT: [[R:%.*]] = select i1 [[C_NOT]], i32 0, i32 [[M]]
; CHECK-NEXT: ret i32 [[R]]
;
%c = icmp ne i32 %x, 0
%m = mul i32 %x, %y
%r = select i1 %c, i32 %m, i32 0
ret i32 %r
}

define i32 @mul_select_eq_zero_sel_undef(i32 %x, i32 %y) {
; CHECK-LABEL: @mul_select_eq_zero_sel_undef(
; CHECK-NEXT: [[C:%.*]] = icmp eq i32 [[X:%.*]], 0
; CHECK-NEXT: [[M:%.*]] = mul i32 [[X]], [[Y:%.*]]
; CHECK-NEXT: [[R:%.*]] = select i1 [[C]], i32 undef, i32 [[M]]
; CHECK-NEXT: ret i32 [[R]]
;
%c = icmp eq i32 %x, 0
%m = mul i32 %x, %y
%r = select i1 %c, i32 undef, i32 %m
ret i32 %r
}

define i32 @mul_select_eq_zero_multiple_users(i32 %x, i32 %y) {
; CHECK-LABEL: @mul_select_eq_zero_multiple_users(
; CHECK-NEXT: [[M:%.*]] = mul i32 [[X:%.*]], [[Y:%.*]]
; CHECK-NEXT: call void @use_i32(i32 [[M]])
; CHECK-NEXT: [[C:%.*]] = icmp eq i32 [[X]], 0
; CHECK-NEXT: [[R:%.*]] = select i1 [[C]], i32 0, i32 [[M]]
; CHECK-NEXT: call void @use_i32(i32 [[M]])
; CHECK-NEXT: call void @use_i32(i32 [[R]])
; CHECK-NEXT: ret i32 [[R]]
;
%m = mul i32 %x, %y
call void @use_i32(i32 %m)
%c = icmp eq i32 %x, 0
%r = select i1 %c, i32 0, i32 %m
call void @use_i32(i32 %m)
call void @use_i32(i32 %r)
ret i32 %r
}

define i32 @mul_select_eq_zero_unrelated_condition(i32 %x, i32 %y, i32 %z) {
; CHECK-LABEL: @mul_select_eq_zero_unrelated_condition(
; CHECK-NEXT: [[C:%.*]] = icmp eq i32 [[Z:%.*]], 0
; CHECK-NEXT: [[M:%.*]] = mul i32 [[X:%.*]], [[Y:%.*]]
; CHECK-NEXT: [[R:%.*]] = select i1 [[C]], i32 0, i32 [[M]]
; CHECK-NEXT: ret i32 [[R]]
;
%c = icmp eq i32 %z, 0
%m = mul i32 %x, %y
%r = select i1 %c, i32 0, i32 %m
ret i32 %r
}

define <4 x i32> @mul_select_eq_zero_vector(<4 x i32> %x, <4 x i32> %y) {
; CHECK-LABEL: @mul_select_eq_zero_vector(
; CHECK-NEXT: [[C:%.*]] = icmp eq <4 x i32> [[X:%.*]], zeroinitializer
; CHECK-NEXT: [[M:%.*]] = mul <4 x i32> [[X]], [[Y:%.*]]
; CHECK-NEXT: [[R:%.*]] = select <4 x i1> [[C]], <4 x i32> zeroinitializer, <4 x i32> [[M]]
; CHECK-NEXT: ret <4 x i32> [[R]]
;
%c = icmp eq <4 x i32> %x, zeroinitializer
%m = mul <4 x i32> %x, %y
%r = select <4 x i1> %c, <4 x i32> zeroinitializer, <4 x i32> %m
ret <4 x i32> %r
}

define <2 x i32> @mul_select_eq_undef_vector(<2 x i32> %x, <2 x i32> %y) {
; CHECK-LABEL: @mul_select_eq_undef_vector(
; CHECK-NEXT: [[C:%.*]] = icmp eq <2 x i32> [[X:%.*]], <i32 0, i32 undef>
; CHECK-NEXT: [[M:%.*]] = mul <2 x i32> [[X]], [[Y:%.*]]
; CHECK-NEXT: [[R:%.*]] = select <2 x i1> [[C]], <2 x i32> <i32 0, i32 42>, <2 x i32> [[M]]
; CHECK-NEXT: ret <2 x i32> [[R]]
;
%c = icmp eq <2 x i32> %x, <i32 0, i32 undef>
%m = mul <2 x i32> %x, %y
%r = select <2 x i1> %c, <2 x i32> <i32 0, i32 42>, <2 x i32> %m
ret <2 x i32> %r
}

define <2 x i32> @mul_select_eq_zero_sel_undef_vector(<2 x i32> %x, <2 x i32> %y) {
; CHECK-LABEL: @mul_select_eq_zero_sel_undef_vector(
; CHECK-NEXT: [[C:%.*]] = icmp eq <2 x i32> [[X:%.*]], zeroinitializer
; CHECK-NEXT: [[M:%.*]] = mul <2 x i32> [[X]], [[Y:%.*]]
; CHECK-NEXT: [[R:%.*]] = select <2 x i1> [[C]], <2 x i32> <i32 0, i32 undef>, <2 x i32> [[M]]
; CHECK-NEXT: ret <2 x i32> [[R]]
;
%c = icmp eq <2 x i32> %x, zeroinitializer
%m = mul <2 x i32> %x, %y
%r = select <2 x i1> %c, <2 x i32> <i32 0, i32 undef>, <2 x i32> %m
ret <2 x i32> %r
}

define <2 x i32> @mul_select_eq_undef_vector_not_merging_to_zero(<2 x i32> %x, <2 x i32> %y) {
; CHECK-LABEL: @mul_select_eq_undef_vector_not_merging_to_zero(
; CHECK-NEXT: [[C:%.*]] = icmp eq <2 x i32> [[X:%.*]], <i32 0, i32 undef>
; CHECK-NEXT: [[M:%.*]] = mul <2 x i32> [[X]], [[Y:%.*]]
; CHECK-NEXT: [[R:%.*]] = select <2 x i1> [[C]], <2 x i32> <i32 1, i32 0>, <2 x i32> [[M]]
; CHECK-NEXT: ret <2 x i32> [[R]]
;
%c = icmp eq <2 x i32> %x, <i32 0, i32 undef>
%m = mul <2 x i32> %x, %y
%r = select <2 x i1> %c, <2 x i32> <i32 1, i32 0>, <2 x i32> %m
ret <2 x i32> %r
}

declare void @use(i1)
declare void @use_i8(i8)
declare void @use_i32(i32)
declare i32 @llvm.cttz.i32(i32, i1 immarg)

0 comments on commit 745f82b

Please sign in to comment.