[llvm] [InstCombine] simplify `(X * C0) / (X * C1)` into `C0 / C1`. (PR #73204)
Yingwei Zheng via llvm-commits
llvm-commits at lists.llvm.org
Wed Nov 29 06:54:54 PST 2023
================
@@ -1207,6 +1207,34 @@ Instruction *InstCombinerImpl::commonIDivTransforms(BinaryOperator &I) {
}
}
+ // (X * Y) / (X * Z) --> Y / X (and commuted variants)
+ if (match(Op0, m_Mul(m_Value(X), m_Value(Y)))) {
+ auto OB0HasNSW = cast<OverflowingBinaryOperator>(Op0)->hasNoSignedWrap();
+ auto OB0HasNUW = cast<OverflowingBinaryOperator>(Op0)->hasNoUnsignedWrap();
+
+ auto CreateDivOrNull = [&](Value *A, Value *B) -> Instruction * {
+ auto OB1HasNSW = cast<OverflowingBinaryOperator>(Op1)->hasNoSignedWrap();
+ auto OB1HasNUW =
+ cast<OverflowingBinaryOperator>(Op1)->hasNoUnsignedWrap();
+ const APInt *C;
+ if (IsSigned && OB0HasNSW && OB1HasNSW && (match(B, m_APInt(C))) &&
+ (*C != -1))
+ return BinaryOperator::CreateSDiv(A, B);
+ if (!IsSigned && OB0HasNUW && OB1HasNUW) {
----------------
dtcxzyw wrote:
Looks like the new version of your patch cannot handle the following pattern:
```
define i4 @src(i4 %a) {
%add4 = mul i4 %a, 3
%add5 = mul nuw i4 %a, 6
%div = udiv i4 %add5, %add4
ret i4 %div
}
define i4 @tgt(i4 %a) {
ret i4 2
}
```
Alive2: https://alive2.llvm.org/ce/z/27Ei4q
https://github.com/llvm/llvm-project/pull/73204
More information about the llvm-commits
mailing list