[llvm] [InstCombine] Fold `(mul (div exact X, C0), C1)` -> `(div exact X, C0/C1)` (PR #96915)

Nikita Popov via llvm-commits llvm-commits at lists.llvm.org
Fri Jun 28 00:00:45 PDT 2024


================
@@ -336,3 +336,55 @@ define <2 x i1> @sdiv_icmp6_vec(<2 x i64> %X) {
   ret <2 x i1> %B
 }
 
+define i8 @mul_of_udiv(i8 %x) {
+; CHECK-LABEL: @mul_of_udiv(
+; CHECK-NEXT:    [[MUL1:%.*]] = lshr exact i8 [[X:%.*]], 1
+; CHECK-NEXT:    ret i8 [[MUL1]]
+;
+  %div = udiv exact i8 %x, 12
+  %mul = mul i8 %div, 6
+  ret i8 %mul
+}
+
+define i8 @mul_of_sdiv(i8 %x) {
+; CHECK-LABEL: @mul_of_sdiv(
+; CHECK-NEXT:    [[MUL_NEG:%.*]] = ashr exact i8 [[X:%.*]], 1
+; CHECK-NEXT:    [[MUL:%.*]] = sub nsw i8 0, [[MUL_NEG]]
+; CHECK-NEXT:    ret i8 [[MUL]]
+;
+  %div = sdiv exact i8 %x, 12
+  %mul = mul i8 %div, -6
+  ret i8 %mul
+}
+
+define i8 @mul_of_sdiv_fail_missing_exact(i8 %x) {
+; CHECK-LABEL: @mul_of_sdiv_fail_missing_exact(
+; CHECK-NEXT:    [[DIV:%.*]] = sdiv i8 [[X:%.*]], 12
+; CHECK-NEXT:    [[MUL:%.*]] = mul i8 [[DIV]], -6
+; CHECK-NEXT:    ret i8 [[MUL]]
+;
+  %div = sdiv i8 %x, 12
+  %mul = mul i8 %div, -6
+  ret i8 %mul
+}
+
+define i8 @mul_of_udiv_fail_bad_remainder(i8 %x) {
+; CHECK-LABEL: @mul_of_udiv_fail_bad_remainder(
+; CHECK-NEXT:    [[DIV:%.*]] = udiv exact i8 [[X:%.*]], 11
+; CHECK-NEXT:    [[MUL:%.*]] = mul nuw i8 [[DIV]], 6
+; CHECK-NEXT:    ret i8 [[MUL]]
+;
+  %div = udiv exact i8 %x, 11
+  %mul = mul i8 %div, 6
+  ret i8 %mul
+}
+
+define i8 @mul_of_sdiv_fail_ub(i8 %x) {
+; CHECK-LABEL: @mul_of_sdiv_fail_ub(
+; CHECK-NEXT:    [[MUL:%.*]] = sub i8 0, [[X:%.*]]
+; CHECK-NEXT:    ret i8 [[MUL]]
+;
+  %div = sdiv exact i8 %x, 6
+  %mul = mul i8 %div, -6
+  ret i8 %mul
+}
----------------
nikic wrote:

As you went out of your way to add non-splat vector support, can you please also add a test for it?

https://github.com/llvm/llvm-project/pull/96915


More information about the llvm-commits mailing list