From 0dd67ed46245760129ed7741f4b0986d22967214 Mon Sep 17 00:00:00 2001 From: Sanjay Patel Date: Tue, 26 Mar 2019 16:19:13 +0000 Subject: [PATCH] [InstCombine] add tests for uaddsat using min; NFC llvm-svn: 357005 --- .../InstCombine/saturating-add-sub.ll | 86 +++++++++++++++++++ 1 file changed, 86 insertions(+) diff --git a/llvm/test/Transforms/InstCombine/saturating-add-sub.ll b/llvm/test/Transforms/InstCombine/saturating-add-sub.ll index 19b1e5943ef7..c817b18abfc0 100644 --- a/llvm/test/Transforms/InstCombine/saturating-add-sub.ll +++ b/llvm/test/Transforms/InstCombine/saturating-add-sub.ll @@ -1248,3 +1248,89 @@ define <4 x i32> @uadd_sat_constant_vec_commute_undefs(<4 x i32> %x) { ret <4 x i32> %r } +declare i32 @get_i32() +declare <2 x i8> @get_v2i8() + +define i32 @unsigned_sat_variable_using_min_add(i32 %x) { +; CHECK-LABEL: @unsigned_sat_variable_using_min_add( +; CHECK-NEXT: [[Y:%.*]] = call i32 @get_i32() +; CHECK-NEXT: [[NOTY:%.*]] = xor i32 [[Y]], -1 +; CHECK-NEXT: [[C:%.*]] = icmp ugt i32 [[NOTY]], [[X:%.*]] +; CHECK-NEXT: [[S:%.*]] = select i1 [[C]], i32 [[X]], i32 [[NOTY]] +; CHECK-NEXT: [[R:%.*]] = add i32 [[S]], [[Y]] +; CHECK-NEXT: ret i32 [[R]] +; + %y = call i32 @get_i32() ; thwart complexity-based canonicalization + %noty = xor i32 %y, -1 + %c = icmp ult i32 %x, %noty + %s = select i1 %c, i32 %x, i32 %noty + %r = add i32 %s, %y + ret i32 %r +} + +define i32 @unsigned_sat_variable_using_min_commute_add(i32 %x) { +; CHECK-LABEL: @unsigned_sat_variable_using_min_commute_add( +; CHECK-NEXT: [[Y:%.*]] = call i32 @get_i32() +; CHECK-NEXT: [[NOTY:%.*]] = xor i32 [[Y]], -1 +; CHECK-NEXT: [[C:%.*]] = icmp ugt i32 [[NOTY]], [[X:%.*]] +; CHECK-NEXT: [[S:%.*]] = select i1 [[C]], i32 [[X]], i32 [[NOTY]] +; CHECK-NEXT: [[R:%.*]] = add i32 [[Y]], [[S]] +; CHECK-NEXT: ret i32 [[R]] +; + %y = call i32 @get_i32() ; thwart complexity-based canonicalization + %noty = xor i32 %y, -1 + %c = icmp ult i32 %x, %noty + %s = select i1 %c, i32 %x, i32 %noty + %r = add i32 %y, %s + ret i32 %r +} + +define <2 x i8> @unsigned_sat_variable_using_min_commute_select(<2 x i8> %x) { +; CHECK-LABEL: @unsigned_sat_variable_using_min_commute_select( +; CHECK-NEXT: [[Y:%.*]] = call <2 x i8> @get_v2i8() +; CHECK-NEXT: [[NOTY:%.*]] = xor <2 x i8> [[Y]], +; CHECK-NEXT: [[C:%.*]] = icmp ult <2 x i8> [[NOTY]], [[X:%.*]] +; CHECK-NEXT: [[S:%.*]] = select <2 x i1> [[C]], <2 x i8> [[NOTY]], <2 x i8> [[X]] +; CHECK-NEXT: [[R:%.*]] = add <2 x i8> [[S]], [[Y]] +; CHECK-NEXT: ret <2 x i8> [[R]] +; + %y = call <2 x i8> @get_v2i8() ; thwart complexity-based canonicalization + %noty = xor <2 x i8> %y, + %c = icmp ult <2 x i8> %noty, %x + %s = select <2 x i1> %c, <2 x i8> %noty, <2 x i8> %x + %r = add <2 x i8> %s, %y + ret <2 x i8> %r +} + +define <2 x i8> @unsigned_sat_variable_using_min_commute_add_select(<2 x i8> %x) { +; CHECK-LABEL: @unsigned_sat_variable_using_min_commute_add_select( +; CHECK-NEXT: [[Y:%.*]] = call <2 x i8> @get_v2i8() +; CHECK-NEXT: [[NOTY:%.*]] = xor <2 x i8> [[Y]], +; CHECK-NEXT: [[C:%.*]] = icmp ult <2 x i8> [[NOTY]], [[X:%.*]] +; CHECK-NEXT: [[S:%.*]] = select <2 x i1> [[C]], <2 x i8> [[NOTY]], <2 x i8> [[X]] +; CHECK-NEXT: [[R:%.*]] = add <2 x i8> [[Y]], [[S]] +; CHECK-NEXT: ret <2 x i8> [[R]] +; + %y = call <2 x i8> @get_v2i8() ; thwart complexity-based canonicalization + %noty = xor <2 x i8> %y, + %c = icmp ult <2 x i8> %noty, %x + %s = select <2 x i1> %c, <2 x i8> %noty, <2 x i8> %x + %r = add <2 x i8> %y, %s + ret <2 x i8> %r +} + +; If we have a constant operand, there's no commutativity variation. + +define i32 @unsigned_sat_constant_using_min(i32 %x) { +; CHECK-LABEL: @unsigned_sat_constant_using_min( +; CHECK-NEXT: [[C:%.*]] = icmp ult i32 [[X:%.*]], 42 +; CHECK-NEXT: [[S:%.*]] = select i1 [[C]], i32 [[X]], i32 42 +; CHECK-NEXT: [[R:%.*]] = add nsw i32 [[S]], -43 +; CHECK-NEXT: ret i32 [[R]] +; + %c = icmp ult i32 %x, 42 + %s = select i1 %c, i32 %x, i32 42 + %r = add i32 %s, -43 + ret i32 %r +} +