[llvm] [InstCombine] Fold `(mul (div exact X, C0), C1)` -> `(div exact X, C0/C1)` (PR #96915)
via llvm-commits
llvm-commits at lists.llvm.org
Thu Jun 27 20:56:16 PDT 2024
================
@@ -336,3 +336,57 @@ 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: [[X_FR:%.*]] = freeze i8 [[X:%.*]]
+; CHECK-NEXT: [[TMP1:%.*]] = srem i8 [[X_FR]], 6
----------------
goldsteinn wrote:
Yeah, needs exact.
https://github.com/llvm/llvm-project/pull/96915
More information about the llvm-commits
mailing list