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

Yingwei Zheng via llvm-commits llvm-commits at lists.llvm.org
Thu Jun 27 20:30:36 PDT 2024


================
@@ -365,6 +365,28 @@ Instruction *InstCombinerImpl::visitMul(BinaryOperator &I) {
       return BinaryOperator::CreateMul(NegOp0, X);
   }
 
+  if (Op0->hasOneUse()) {
+    // (mul (div exact X, C0), C1)
+    //    -> (div exact C0 / C1)
+    // iff C0 % C1 == 0 and C0 / C1 doesn't create UB.
----------------
dtcxzyw wrote:

```suggestion
    // iff C0 % C1 == 0 and X / (C0 / C1) doesn't create UB.
```
Counterexample:
X = INT_MIN, C0 = 2, C1 = -2
C0 / C1 = -1 doesn't create UB, but X / (C0 / C1) does.


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


More information about the llvm-commits mailing list