From: Sanjay Patel Date: Thu, 26 Jul 2018 16:14:53 +0000 (+0000) Subject: [InstCombine] add tests for udiv with common factor; NFC X-Git-Url: https://granicus.if.org/sourcecode?a=commitdiff_plain;h=da3868db1ab761c1e73a1f3d83d9c7d6c91273b2;p=llvm [InstCombine] add tests for udiv with common factor; NFC This fold is mentioned in PR38239: https://bugs.llvm.org/show_bug.cgi?id=38239 The general case probably belongs in -reassociate, but given that we do basic reassociation optimizations similar to this in instcombine already, we might as well be consistent within instcombine and handle this pattern? git-svn-id: https://llvm.org/svn/llvm-project/llvm/trunk@338038 91177308-0d34-0410-b5e6-96231b3b80d8 --- diff --git a/test/Transforms/InstCombine/div.ll b/test/Transforms/InstCombine/div.ll index 56e887e5b14..a35ebed319a 100644 --- a/test/Transforms/InstCombine/div.ll +++ b/test/Transforms/InstCombine/div.ll @@ -686,3 +686,85 @@ define <2 x i8> @div_factor_unsigned_vec(<2 x i8> %x, <2 x i8> %y) { ret <2 x i8> %r } +define i8 @udiv_common_factor(i8 %x, i8 %y, i8 %z) { +; CHECK-LABEL: @udiv_common_factor( +; CHECK-NEXT: [[A:%.*]] = mul nuw i8 [[Z:%.*]], [[X:%.*]] +; CHECK-NEXT: [[B:%.*]] = mul nuw i8 [[Z]], [[Y:%.*]] +; CHECK-NEXT: [[C:%.*]] = udiv i8 [[A]], [[B]] +; CHECK-NEXT: ret i8 [[C]] +; + %a = mul nuw i8 %z, %x + %b = mul nuw i8 %z, %y + %c = udiv i8 %a, %b + ret i8 %c +} + +define <2 x i8> @udiv_common_factor_commute1_vec(<2 x i8> %x, <2 x i8> %y, <2 x i8> %z) { +; CHECK-LABEL: @udiv_common_factor_commute1_vec( +; CHECK-NEXT: [[A:%.*]] = mul nuw <2 x i8> [[X:%.*]], [[Z:%.*]] +; CHECK-NEXT: [[B:%.*]] = mul nuw <2 x i8> [[Z]], [[Y:%.*]] +; CHECK-NEXT: [[C:%.*]] = udiv <2 x i8> [[A]], [[B]] +; CHECK-NEXT: ret <2 x i8> [[C]] +; + %a = mul nuw <2 x i8> %x, %z + %b = mul nuw <2 x i8> %z, %y + %c = udiv <2 x i8> %a, %b + ret <2 x i8> %c +} + +define i8 @udiv_common_factor_commute2(i8 %x, i8 %y, i8 %z) { +; CHECK-LABEL: @udiv_common_factor_commute2( +; CHECK-NEXT: [[A:%.*]] = mul nuw i8 [[X:%.*]], [[Z:%.*]] +; CHECK-NEXT: [[B:%.*]] = mul nuw i8 [[Y:%.*]], [[Z]] +; CHECK-NEXT: [[C:%.*]] = udiv i8 [[A]], [[B]] +; CHECK-NEXT: ret i8 [[C]] +; + %a = mul nuw i8 %x, %z + %b = mul nuw i8 %y, %z + %c = udiv i8 %a, %b + ret i8 %c +} + +define i8 @udiv_common_factor_commute3(i8 %x, i8 %y, i8 %z) { +; CHECK-LABEL: @udiv_common_factor_commute3( +; CHECK-NEXT: [[A:%.*]] = mul nuw i8 [[Z:%.*]], [[X:%.*]] +; CHECK-NEXT: [[B:%.*]] = mul nuw i8 [[Y:%.*]], [[Z]] +; CHECK-NEXT: [[C:%.*]] = udiv i8 [[A]], [[B]] +; CHECK-NEXT: ret i8 [[C]] +; + %a = mul nuw i8 %z, %x + %b = mul nuw i8 %y, %z + %c = udiv i8 %a, %b + ret i8 %c +} + +; Negative test: both mul must be 'nuw'. + +define i8 @udiv_common_factor_not_nuw(i8 %x, i8 %y, i8 %z) { +; CHECK-LABEL: @udiv_common_factor_not_nuw( +; CHECK-NEXT: [[A:%.*]] = mul i8 [[Z:%.*]], [[X:%.*]] +; CHECK-NEXT: [[B:%.*]] = mul nuw i8 [[Z]], [[Y:%.*]] +; CHECK-NEXT: [[C:%.*]] = udiv i8 [[A]], [[B]] +; CHECK-NEXT: ret i8 [[C]] +; + %a = mul i8 %z, %x + %b = mul nuw i8 %z, %y + %c = udiv i8 %a, %b + ret i8 %c +} + +; Negative test: both mul must be 'nuw'. + +define <2 x i8> @udiv_common_factor_not_nuw_vec(<2 x i8> %x, <2 x i8> %y, <2 x i8> %z) { +; CHECK-LABEL: @udiv_common_factor_not_nuw_vec( +; CHECK-NEXT: [[A:%.*]] = mul nuw <2 x i8> [[Z:%.*]], [[X:%.*]] +; CHECK-NEXT: [[B:%.*]] = mul <2 x i8> [[Z]], [[Y:%.*]] +; CHECK-NEXT: [[C:%.*]] = udiv <2 x i8> [[A]], [[B]] +; CHECK-NEXT: ret <2 x i8> [[C]] +; + %a = mul nuw <2 x i8> %z, %x + %b = mul <2 x i8> %z, %y + %c = udiv <2 x i8> %a, %b + ret <2 x i8> %c +} +