Skip to content

Commit c9af54b

Browse files
committed
[InstCombine] add more tests for saturated add; NFC
llvm-svn: 354886
1 parent 0d76dc2 commit c9af54b

File tree

1 file changed

+134
-0
lines changed

1 file changed

+134
-0
lines changed

llvm/test/Transforms/InstCombine/saturating-add-sub.ll

Lines changed: 134 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -931,6 +931,140 @@ define i32 @uadd_sat_not_commute_select_ugt_commute_add(i32 %x, i32 %y) {
931931
ret i32 %r
932932
}
933933

934+
; The add may include a 'not' op rather than the cmp.
935+
936+
define i32 @uadd_sat_not(i32 %x, i32 %y) {
937+
; CHECK-LABEL: @uadd_sat_not(
938+
; CHECK-NEXT: [[NOTX:%.*]] = xor i32 [[X:%.*]], -1
939+
; CHECK-NEXT: [[A:%.*]] = add i32 [[NOTX]], [[Y:%.*]]
940+
; CHECK-NEXT: [[C:%.*]] = icmp ult i32 [[X]], [[Y]]
941+
; CHECK-NEXT: [[R:%.*]] = select i1 [[C]], i32 -1, i32 [[A]]
942+
; CHECK-NEXT: ret i32 [[R]]
943+
;
944+
%notx = xor i32 %x, -1
945+
%a = add i32 %notx, %y
946+
%c = icmp ult i32 %x, %y
947+
%r = select i1 %c, i32 -1, i32 %a
948+
ret i32 %r
949+
}
950+
951+
define i32 @uadd_sat_not_commute_add(i32 %xp, i32 %yp) {
952+
; CHECK-LABEL: @uadd_sat_not_commute_add(
953+
; CHECK-NEXT: [[X:%.*]] = srem i32 42, [[XP:%.*]]
954+
; CHECK-NEXT: [[Y:%.*]] = urem i32 42, [[YP:%.*]]
955+
; CHECK-NEXT: [[NOTX:%.*]] = xor i32 [[X]], -1
956+
; CHECK-NEXT: [[A:%.*]] = add nsw i32 [[Y]], [[NOTX]]
957+
; CHECK-NEXT: [[C:%.*]] = icmp ult i32 [[X]], [[Y]]
958+
; CHECK-NEXT: [[R:%.*]] = select i1 [[C]], i32 -1, i32 [[A]]
959+
; CHECK-NEXT: ret i32 [[R]]
960+
;
961+
%x = srem i32 42, %xp ; thwart complexity-based-canonicalization
962+
%y = urem i32 42, %yp ; thwart complexity-based-canonicalization
963+
%notx = xor i32 %x, -1
964+
%a = add i32 %y, %notx
965+
%c = icmp ult i32 %x, %y
966+
%r = select i1 %c, i32 -1, i32 %a
967+
ret i32 %r
968+
}
969+
970+
define i32 @uadd_sat_not_ugt(i32 %x, i32 %y) {
971+
; CHECK-LABEL: @uadd_sat_not_ugt(
972+
; CHECK-NEXT: [[NOTX:%.*]] = xor i32 [[X:%.*]], -1
973+
; CHECK-NEXT: [[A:%.*]] = add i32 [[NOTX]], [[Y:%.*]]
974+
; CHECK-NEXT: [[C:%.*]] = icmp ugt i32 [[Y]], [[X]]
975+
; CHECK-NEXT: [[R:%.*]] = select i1 [[C]], i32 -1, i32 [[A]]
976+
; CHECK-NEXT: ret i32 [[R]]
977+
;
978+
%notx = xor i32 %x, -1
979+
%a = add i32 %notx, %y
980+
%c = icmp ugt i32 %y, %x
981+
%r = select i1 %c, i32 -1, i32 %a
982+
ret i32 %r
983+
}
984+
985+
define <2 x i32> @uadd_sat_not_ugt_commute_add(<2 x i32> %x, <2 x i32> %yp) {
986+
; CHECK-LABEL: @uadd_sat_not_ugt_commute_add(
987+
; CHECK-NEXT: [[Y:%.*]] = sdiv <2 x i32> [[YP:%.*]], <i32 2442, i32 4242>
988+
; CHECK-NEXT: [[NOTX:%.*]] = xor <2 x i32> [[X:%.*]], <i32 -1, i32 -1>
989+
; CHECK-NEXT: [[A:%.*]] = add <2 x i32> [[Y]], [[NOTX]]
990+
; CHECK-NEXT: [[C:%.*]] = icmp ugt <2 x i32> [[Y]], [[X]]
991+
; CHECK-NEXT: [[R:%.*]] = select <2 x i1> [[C]], <2 x i32> <i32 -1, i32 -1>, <2 x i32> [[A]]
992+
; CHECK-NEXT: ret <2 x i32> [[R]]
993+
;
994+
%y = sdiv <2 x i32> %yp, <i32 2442, i32 4242> ; thwart complexity-based-canonicalization
995+
%notx = xor <2 x i32> %x, <i32 -1, i32 -1>
996+
%a = add <2 x i32> %y, %notx
997+
%c = icmp ugt <2 x i32> %y, %x
998+
%r = select <2 x i1> %c, <2 x i32> <i32 -1, i32 -1>, <2 x i32> %a
999+
ret <2 x i32> %r
1000+
}
1001+
1002+
define i32 @uadd_sat_not_commute_select(i32 %x, i32 %y) {
1003+
; CHECK-LABEL: @uadd_sat_not_commute_select(
1004+
; CHECK-NEXT: [[NOTX:%.*]] = xor i32 [[X:%.*]], -1
1005+
; CHECK-NEXT: [[A:%.*]] = add i32 [[NOTX]], [[Y:%.*]]
1006+
; CHECK-NEXT: [[C:%.*]] = icmp ult i32 [[Y]], [[X]]
1007+
; CHECK-NEXT: [[R:%.*]] = select i1 [[C]], i32 [[A]], i32 -1
1008+
; CHECK-NEXT: ret i32 [[R]]
1009+
;
1010+
%notx = xor i32 %x, -1
1011+
%a = add i32 %notx, %y
1012+
%c = icmp ult i32 %y, %x
1013+
%r = select i1 %c, i32 %a, i32 -1
1014+
ret i32 %r
1015+
}
1016+
1017+
define i32 @uadd_sat_not_commute_select_commute_add(i32 %x, i32 %yp) {
1018+
; CHECK-LABEL: @uadd_sat_not_commute_select_commute_add(
1019+
; CHECK-NEXT: [[Y:%.*]] = sdiv i32 42, [[YP:%.*]]
1020+
; CHECK-NEXT: [[NOTX:%.*]] = xor i32 [[X:%.*]], -1
1021+
; CHECK-NEXT: [[A:%.*]] = add i32 [[Y]], [[NOTX]]
1022+
; CHECK-NEXT: [[C:%.*]] = icmp ult i32 [[Y]], [[X]]
1023+
; CHECK-NEXT: [[R:%.*]] = select i1 [[C]], i32 [[A]], i32 -1
1024+
; CHECK-NEXT: ret i32 [[R]]
1025+
;
1026+
%y = sdiv i32 42, %yp ; thwart complexity-based-canonicalization
1027+
%notx = xor i32 %x, -1
1028+
%a = add i32 %y, %notx
1029+
%c = icmp ult i32 %y, %x
1030+
%r = select i1 %c, i32 %a, i32 -1
1031+
ret i32 %r
1032+
}
1033+
1034+
define <2 x i32> @uadd_sat_not_commute_select_ugt(<2 x i32> %xp, <2 x i32> %yp) {
1035+
; CHECK-LABEL: @uadd_sat_not_commute_select_ugt(
1036+
; CHECK-NEXT: [[X:%.*]] = urem <2 x i32> <i32 42, i32 -42>, [[XP:%.*]]
1037+
; CHECK-NEXT: [[Y:%.*]] = srem <2 x i32> <i32 12, i32 412>, [[YP:%.*]]
1038+
; CHECK-NEXT: [[NOTX:%.*]] = xor <2 x i32> [[X]], <i32 -1, i32 -1>
1039+
; CHECK-NEXT: [[A:%.*]] = add <2 x i32> [[Y]], [[NOTX]]
1040+
; CHECK-NEXT: [[C:%.*]] = icmp ugt <2 x i32> [[X]], [[Y]]
1041+
; CHECK-NEXT: [[R:%.*]] = select <2 x i1> [[C]], <2 x i32> [[A]], <2 x i32> <i32 -1, i32 -1>
1042+
; CHECK-NEXT: ret <2 x i32> [[R]]
1043+
;
1044+
%x = urem <2 x i32> <i32 42, i32 -42>, %xp ; thwart complexity-based-canonicalization
1045+
%y = srem <2 x i32> <i32 12, i32 412>, %yp ; thwart complexity-based-canonicalization
1046+
%notx = xor <2 x i32> %x, <i32 -1, i32 -1>
1047+
%a = add <2 x i32> %y, %notx
1048+
%c = icmp ugt <2 x i32> %x, %y
1049+
%r = select <2 x i1> %c, <2 x i32> %a, <2 x i32> <i32 -1, i32 -1>
1050+
ret <2 x i32> %r
1051+
}
1052+
1053+
define i32 @uadd_sat_not_commute_select_ugt_commute_add(i32 %x, i32 %y) {
1054+
; CHECK-LABEL: @uadd_sat_not_commute_select_ugt_commute_add(
1055+
; CHECK-NEXT: [[NOTX:%.*]] = xor i32 [[X:%.*]], -1
1056+
; CHECK-NEXT: [[A:%.*]] = add i32 [[NOTX]], [[Y:%.*]]
1057+
; CHECK-NEXT: [[C:%.*]] = icmp ugt i32 [[X]], [[Y]]
1058+
; CHECK-NEXT: [[R:%.*]] = select i1 [[C]], i32 [[A]], i32 -1
1059+
; CHECK-NEXT: ret i32 [[R]]
1060+
;
1061+
%notx = xor i32 %x, -1
1062+
%a = add i32 %notx, %y
1063+
%c = icmp ugt i32 %x, %y
1064+
%r = select i1 %c, i32 %a, i32 -1
1065+
ret i32 %r
1066+
}
1067+
9341068
define i32 @uadd_sat_constant(i32 %x) {
9351069
; CHECK-LABEL: @uadd_sat_constant(
9361070
; CHECK-NEXT: [[A:%.*]] = add i32 [[X:%.*]], 42

0 commit comments

Comments
 (0)